gpt4 book ai didi

python - Mysql数据库无法连接到python

转载 作者:行者123 更新时间:2023-11-29 16:36:28 29 4
gpt4 key购买 nike

我正在尝试通过这段代码将 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。

最佳答案

您的 MySQL 服务器版本似乎是 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')

关于python - Mysql数据库无法连接到python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53577820/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com