gpt4 book ai didi

python - PyMySQL 访问被拒绝 "using password (no") 但使用密码

转载 作者:可可西里 更新时间:2023-11-01 07:57:57 28 4
gpt4 key购买 nike

我这里是 Headscratcher。

我正在尝试通过 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 block 失败了……程序流没有到达“authresp”语句。当我的一个同事运行同一个程序时,他的密码会打印在控制台上。

所以,现在我只需要弄清楚为什么我不走这条路。

最佳答案

解决了 MySQL 从 8.0.11.0 降级到 5.7.22 后的问题。

除了我在原始问题中列出的内容外,我还尝试了以下内容:

  • 安装了较低版本的 PyMySQL(在我的例子中是 0.8.1 到 0.8.0)
  • 完全删除 PyMySQL 的两个版本并安装 v0.8.0
  • 删除 MySQL 8.0 并重新安装

当以上方法均无效时,我将 MySQL 降级到 v5.7,这是我的一个同事正在使用的版本。这解决了错误。

关于python - PyMySQL 访问被拒绝 "using password (no") 但使用密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50336458/

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