我正在尝试使用简单的 python 脚本连接到我的 sql 服务器。
import MySQLdb
db = MySQLdb.connect(host="192.168.0.156", user="root",
passwd="Imnottellingyoumypassword", db="mydatabase") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM customers")
for row in cur.fetchall() :
print row[0]
但是当我运行脚本时出现问题:
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user
'root'@'192.168.0.107' (using password: YES)")
为什么他试图连接到我笔记本电脑的 IP 地址而不是 SQL Server?
最佳答案
该消息来自 MySQL 服务器而不是 MySQL 驱动程序。服务器告诉您的是,不允许以 root 身份从笔记本电脑的 IP 进行连接。
关于Python SQL 连接到错误的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23227450/