这是我的主角。
我正在尝试从 Python 连接到本地 MySQL 8.0.11.0 安装的数据库。
以下是我使用的代码:
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
Python正在返回以下内容:
Traceback (most recent call last):
File "D:\Python\FileCheck.py", line 38, in <module>
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
File "C:\Program Files\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 704, in __init__
self.connect()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 974, in connect
self._request_authentication()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1203, in _request_authentication
auth_packet = self._read_packet()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1059, in _read_packet
packet.check_error()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "C:\Program Files\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
我已确认通过 MySQL Workbench登录时可以访问数据库。奇怪的是,即使我发送了密码,Python也在告诉我"using password: NO"
。
我已尝试在脚本中将passwd
更改为"password"
并创建具有相应权限的新用户。两者都没有效果。
不确定接下来要检查什么。
编辑:以下是root的补助:
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION
GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *....
GRANT ALL PRIVILEGES ON `customerinfo`.* TO `root`@`localhost` WITH GRANT OPTION
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION
编辑2: 在connections.py中添加了 connect 和 _request_authentication 的调试行。
这是最新的:
C:\Users\Paul Miller>python.exe D:\Python\FileCheck.py
** DEBUG 1 **
connect, line 973
host= localhost
user= root
password= placeholder
** DEBUG 2 **
_request_authentication line 1172
user= root
password= placeholder
Traceback (most recent call last):
File "D:\Python\FileCheck.py", line 38, in <module>
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
File "C:\Program Files\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 704, in __init__
self.connect()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 982, in connect
self._request_authentication()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1218, in _request_authentication
auth_packet = self._read_packet()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1067, in _read_packet
packet.check_error()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "C:\Program Files\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
编辑3 :启用日志记录。日志中没有任何惊喜,但这是最新尝试产生的内容:
2018-05-15T10:27:15.197445Z 43 Connect root@localhost on CustomerInfo using TCP/IP
2018-05-15T10:27:15.197540Z 43 Connect Access denied for user 'root'@'localhost' (using password: NO)
编辑4:发现问题。我添加了一些调试语句如下:
if self._auth_plugin_name in ('', 'mysql_native_password'):
print("** DEBUG 3 **")
print(self.password)
print("\n")
authresp = _scramble(self.password.encode('latin1'), self.salt)
问题是这个IF块失败了...程序流程没有进入&#34; authresp&#34;声明。当我的同伴运行同一个程序时,他的密码会在控制台中打印出来。
所以,现在我只需要弄清楚为什么我不打算离开这个分支。
答案 0 :(得分:5)
将MySQL从8.0.11.0降级到5.7.22之后解决了这个问题。
除了我在原始问题中列出的内容外,我还尝试了以下内容:
如果以上都没有,我将MySQL降级到v5.7,这是我的同行之一使用的。这解决了错误。
答案 1 :(得分:0)
这很奇怪。我会在虚拟环境中安装pymysql,然后在这个函数中删除一些调试行:
https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py#L940
这应该显示它实际用于连接的变量,并明确错误。
答案 2 :(得分:0)
此问题的一个可能原因是 PyMySQL
版本 0.8.x
或更低版本。如果是这种情况,问题可以通过在 pip 中升级到 PyMySQL==0.9.3
来解决。