Python MySQLdb – 错误1045:拒绝用户访问
作者:互联网
我有一个类似的python代码
import MySQLdb
import sys
try:
con = MySQLdb.connect(host = 'localhost',user = 'crawler',passwd = 'crawler', db = 'real_estate_analytics')
#... rest of code ...
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
问题是我收到以下错误:
错误1045:访问被拒绝用户’crawler’@’localhost'(使用密码:YES)
如果我在终端mysql -u crawler -pcrawler上的mysql我可以访问数据库没有问题.
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| AL |
| cloversoup |
| codebar |
| mysql |
| performance_schema |
| real_estate_analytics |
| teste |
+-----------------------+
8 rows in set (0.00 sec)
我还删除了匿名用户以避免冲突.我的用户是
mysql> select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| root | % |
| crawler | localhost |
| root | localhost |
+---------+-----------+
3 rows in set (0.00 sec)
我已经给予爬虫(和刷新的权限)所有授权,所以这不应该是问题:
授予所有特权*.* to crawler @ localhost IDENTIFIED BY’criverler’WITH GRANT OPTION;
FLUSH特权;
事实上:
mysql> show grants;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for crawler@localhost |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'crawler'@'localhost' IDENTIFIED BY PASSWORD '*A594DECC46D378FDA13D7843740CBF4985E6969B' WITH GRANT OPTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
我还尝试使用root用户进行连接
con = MySQLdb.connect(host =’localhost’,user =’root’,passwd =’my_root_password’,db =’real_estate_analytics’)
但它也不起作用(相同的错误).
这件事让我抓狂.有没有人能够了解问题所在?
OBS:我知道在SO上有类似的问题,但是我已经全部阅读了它们并没有解决我的问题.这就是我在这里发布的原因
解决方法:
‘localhost’是并拥有always been special with MySQL.在您的情况下,您授予crawler @ localhost一些权限,这将意味着“通过UNIX套接字连接的用户爬网程序”.而且,我很确定MySQL服务器配置了–skip-networking.
这可以通过明确来解决.使用数据库驱动程序的unix_socket connection argument,它将强制使用UNIX套接字. (无耻地链接到MySQL Connector / Python文档,因为我是该驱动程序的维护者).
标签:python,mysql,mysql-python 来源: https://codeday.me/bug/20191007/1869423.html