[英]Python MySQLdb - Error 1045: Access denied for user
I have a python code that goes like 我有一个类似的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)
The problem is that I'm getting the following error: 问题是我收到以下错误:
Error 1045: Access denied for user 'crawler'@'localhost' (using password: YES)
If I mysql on the terminal mysql -u crawler -pcrawler
I can access the databases with no problem. 如果我在终端
mysql -u crawler -pcrawler
上的mysql -u crawler -pcrawler
我可以访问数据库没有问题。
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| AL |
| cloversoup |
| codebar |
| mysql |
| performance_schema |
| real_estate_analytics |
| teste |
+-----------------------+
8 rows in set (0.00 sec)
I have also deleted the anonymous user to avoid collisions. 我还删除了匿名用户以避免冲突。 My users are
我的用户是
mysql> select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| root | % |
| crawler | localhost |
| root | localhost |
+---------+-----------+
3 rows in set (0.00 sec)
I have given all grants to crawler (and flushed privileges), so that should not be the problem: 我已经给予爬虫(和刷新的权限)所有授权,所以这不应该是问题:
GRANT ALL PRIVILEGES ON *.* TO crawler@localhost IDENTIFIED BY 'crawler' WITH GRANT OPTION;
FLUSH PRIVILEGES;
as a matter of fact: 事实上:
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)
I have also tried to connect using the root
user, by doing 我还尝试使用
root
用户进行连接
con = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'my_root_password', db = 'real_estate_analytics')
but it doesn't work either (same error). 但它也不起作用(相同的错误)。
This thing is driving me crazy. 这件事让我抓狂。 Does anyone have an insight on what the problem could be?
有没有人能够了解问题所在?
OBS: I know there are similar questions on SO, but I've read them all and it didn't solve my problem. OBS:我知道在SO上有类似的问题,但是我已经全部阅读了它们并没有解决我的问题。 That's why I'm posting it here
这就是我在这里发布的原因
'localhost' is and has always been special with MySQL . 'localhost' 对MySQL来说一直很特别 。 In your case, you grant crawler@localhost some privileges and this would mean 'the user crawler connecting through UNIX socket'.
在您的情况下,您授予crawler @ localhost一些权限,这将意味着“通过UNIX套接字连接的用户爬网程序”。 And, I'm pretty sure the MySQL server is configured with --skip-networking .
而且,我很确定MySQL服务器配置了--skip-networking 。
This can be fixed by being explicit. 这可以通过明确来解决。 Using the unix_socket connection argument of your database driver, it would force the use of the UNIX socket.
使用数据库驱动程序的unix_socket连接参数 ,它将强制使用UNIX套接字。 (Shamelessly linking to MySQL Connector/Python docs, as I'm the maintainer of that driver).
(无耻地链接到MySQL Connector / Python文档,因为我是该驱动程序的维护者)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.