MySQL数据库未连接到Python

时间:2018-12-02 05:57:54

标签: python mysql connection

我正在尝试通过此代码将python连接到mysql- (我正在使用mysql.connect库)

import mysql.connector

cnx = mysql.connector.connect(user='root', 
                          password='password',
                          host='127.0.0.1',
                          database='db')
print(cnx)
cnx.close()

但是它继续抛出错误-

Traceback (most recent call last):
File "D:/ted/main.py", line 5, in <module>
database='db')
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\__init__.py", 
line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py", 
line 94, in __init__
self.connect(**kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\abstracts.py", 
line 722, in connect
self._open_connection()
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py", 
line 211, in _open_connection
self._ssl)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py", 
 line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py", 
line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py", 
line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
 File "C:\Program Files (x86)\Python36- 
32\lib\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 
'caching_sha2_password' is not supported

Process finished with exit code 1

我已经在工作台中启动了mysql服务器,并且还检查了状态。 还有名为“ db”的数据库 还检查主机是否正确。 ->也删除了ssl。

1 个答案:

答案 0 :(得分:0)

似乎您的MySQL Server版本是8.x,在这种情况下,默认的MySQL连接器是caching_sha2_password。另一方面,您的错误可能是因为您的python客户端连接器不支持此身份验证插件,因此您应将身份验证插件明确更改为旧的(mysql_native_password)。

cnx = mysql.connector.connect(user='root', password='password',
                          host='127.0.0.1', database='db',
                          auth_plugin='mysql_native_password')