![MySql 错误:拒绝用户“root”@“localhost”访问(使用密码:否)](https://linux22.com/image/1599511/MySql%20%E9%94%99%E8%AF%AF%EF%BC%9A%E6%8B%92%E7%BB%9D%E7%94%A8%E6%88%B7%E2%80%9Croot%E2%80%9D%40%E2%80%9Clocalhost%E2%80%9D%E8%AE%BF%E9%97%AE%EF%BC%88%E4%BD%BF%E7%94%A8%E5%AF%86%E7%A0%81%EF%BC%9A%E5%90%A6%EF%BC%89.png)
我正在尝试从 MySQL 检索数据以用于 python。但它打印出TypeError: 'NoneType' object is not iterable
服务器日志显示Access denied for user 'root'@'localhost' (using password: NO)
这是服务器日志。 提前致谢!
编辑:这是python代码
import pandas as pd
user, pw, host, db = 'root', '12345', '127.0.0.1', 'ca2database'
cnx = mysql.connector.connect(user=user, password=pw, host=host, database=db)
cursor = cnx.cursor()
df= pd.read_sql('SELECT * FROM student', con=cnx)
print(df)
cursor.close()
cnx.close()
这是SQL 中的数据
答案1
'localhost'、'%' 和 '127.0.0.1' 之间存在一些差异。似乎 Python 使用套接字连接数据库,并且 '%' 是首选。
添加以下根变体(或仅授予您的数据库):
CREATE USER 'root'@'%' IDENTIFIED BY '<your password>';
GRANT ALL PRIVILEGES on *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;