You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python连接MySQL数据库报错OperationalError(2059)求助

解决Python中MySQLdb连接报错(2059, )的问题

我之前也碰到过这个一模一样的问题,折腾了好一会儿才找到根源,分享给你:

问题重现

在Python Shell中执行以下MySQL连接代码:

import MySQLdb
Con = MySQLdb.Connect(user='testuser', password='password', db='test')

直接抛出了无描述信息的报错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2059, <NULL>)

这个报错连个提示信息都没有,刚开始真的有点摸不着头脑。

问题原因与解决方法

后来排查清楚了,这个错误和MySQL的新身份验证插件以及它的「Strong Password Encryption for Authentication」特性直接相关。旧版本的MySQLdb库不支持MySQL默认的caching_sha2_password验证方式,所以只需要把MySQL Server的身份验证配置切换为旧的old_password_plugin(也就是Legacy Authentication Method),重新尝试连接就能解决问题了。

内容的提问来源于stack exchange,提问作者Nickey Production

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:38:20