开发者社区> 问答> 正文

python3连接Mysql错误

环境:win10-x64/python3.4.3/MySql5.7.10

python连接MySql服务器时,无论使用pymysql还是mysql-connector都无法正常连接,提示 Unknown database

如下

conn = pymysql.connect(user='root', password='password', database='test')
Traceback (most recent call last):
File "", line 1, in
File "C:Python34libsite-packagespymysql__init__.py", line 88, in Connect

return Connection(*args, **kwargs)

File "C:Python34libsite-packagespymysqlconnections.py", line 678, in init

self.connect()

File "C:Python34libsite-packagespymysqlconnections.py", line 890, in connect

self._request_authentication()

File "C:Python34libsite-packagespymysqlconnections.py", line 1097, in _request_authentication

auth_packet = self._read_packet()

File "C:Python34libsite-packagespymysqlconnections.py", line 965, in _read_packet

packet.check_error()

File "C:Python34libsite-packagespymysqlconnections.py", line 394, in check_error

err.raise_mysql_exception(self._data)

File "C:Python34libsite-packagespymysqlerr.py", line 120, in raise_mysql_exception

_check_mysql_exception(errinfo)

File "C:Python34libsite-packagespymysqlerr.py", line 115, in _check_mysql_exception

raise InternalError(errno, errorvalue)

pymysql.err.InternalError: (1049, "Unknown database 'test'")
全选复制放进笔记conn = mysql.connector.connect(user='root', password='password', database='test')
Traceback (most recent call last):
File "", line 1, in
File "C:Python34libsite-packagesmysqlconnector__init__.py", line 179, in connect

return MySQLConnection(*args, **kwargs)

File "C:Python34libsite-packagesmysqlconnectorconnection.py", line 95, in init

self.connect(**kwargs)

File "C:Python34libsite-packagesmysqlconnectorabstracts.py", line 719, in connect

self._open_connection()

File "C:Python34libsite-packagesmysqlconnectorconnection.py", line 210, in _open_connection

self._ssl)

File "C:Python34libsite-packagesmysqlconnectorconnection.py", line 144, in _do_auth

self._auth_switch_request(username, password)

File "C:Python34libsite-packagesmysqlconnectorconnection.py", line 177, in _auth_switch_request

raise errors.get_exception(packet)

mysql.connector.errors.ProgrammingError: 1049 (42000): Unknown database 'test'

展开
收起
我的中国 2016-02-08 19:42:37 7613 0
1 条回答
写回答
取消 提交回答
  • R&S网络资深工程师 ,阿里云论坛官方版主,阿里云云计算ACP,春考教学网站长,IT技术晋级之路专辑作者

    pymysql.connect只负责连接数据库,它不会帮你创建数据库的,你要先创建一个
    1049 (42000): Unknown database 'test' 说明你没有test这个数据库
    你手动在mysql里面创建一个再试试

    2019-07-17 18:28:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载