8

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

4
  • 1
    1) try the unix_socket='' connection argument for MySQLdb; 2) Are you sure you connect to the correct MySQL server? 3) GRANT .. TO 'crawler'@'127.0.0.1' and try that. Commented Mar 8, 2014 at 19:17
  • @geertjanvdk it worked with the unix_socket argument! Thanks a lot! If you want to write an answer with that, I'll accept and upvote it, so you can have your deserved points :) Commented Mar 8, 2014 at 20:59
  • Just did, thanks! Additional note: do not give such privileges to the 'crawler' user. GRANT ALL ON real_estate_analytics.* will be enough and definitely without WITH GRANT. Commented Mar 9, 2014 at 7:15
  • awesome, thanks! And the entire limitless privileges was only for debugging purposes :) Commented Mar 9, 2014 at 17:08

1 Answer 1

9

'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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.