Python MySQLdb - Error 1045: Access Denied for User

I have python code that looks 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 get 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 easily access the databases.

 mysql> show databases; +-----------------------+ | Database | +-----------------------+ | information_schema | | AL | | cloversoup | | codebar | | mysql | | performance_schema | | real_estate_analytics | | teste | +-----------------------+ 8 rows in set (0.00 sec) 

I also deleted an anonymous user to avoid collisions. My users

 mysql> select user,host from mysql.user; +---------+-----------+ | user | host | +---------+-----------+ | root | % | | crawler | localhost | | root | localhost | +---------+-----------+ 3 rows in set (0.00 sec) 

I have provided all grants for the crawler (and privileges dropped), so this should not be a problem:

GRANT ALL PRIVILEGES ON *.* TO crawler@localhost IDENTIFIED BY 'crawler' WITH GRANT OPTION;

FLUSH PRIVILEGES;

in 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 also tried connecting using the root by doing

con = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'my_root_password', db = 'real_estate_analytics')

but it does not work (same error).

This thing is driving me crazy. Does anyone know what might be the problem?

OBS: I know there are similar questions on SO, but I read all of them, and this did not solve my problem. That is why I post it here

+7
python mysql mysql-python
source share
1 answer

"localhost" is and always has been special with MySQL . In your case, you grant crawler @localhost some privileges, and that will mean "user crawler connecting via a UNIX socket." And, I'm 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 forces you to use a UNIX socket. (Shamelessly linking to MySQL Connector / Python docs, as I am the custodian of this driver).

+7
source share

All Articles