问题
I have a python code that goes like
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> 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
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. That's why I'm posting it here
回答1:
'localhost' is and has always been special with MySQL. In your case, you grant crawler@localhost some privileges and this would mean 'the user crawler connecting through UNIX socket'. And, I'm pretty sure the MySQL server is configured with --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. (Shamelessly linking to MySQL Connector/Python docs, as I'm the maintainer of that driver).
来源:https://stackoverflow.com/questions/22267114/python-mysqldb-error-1045-access-denied-for-user