title: Python MySQLdb - Error 1045: Access denied for user
body: <p>I have a python code that goes like</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The problem is that I'm getting the following error:</p>
  
  <p><code>Error 1045: Access denied for user 'crawler'@'localhost' (using password: YES)</code></p>
  
  <p>If I mysql on the terminal <code>mysql -u crawler -pcrawler</code> I can access the databases with no problem.</p>
  
  <pre><code>mysql&gt; show databases;
  +-----------------------+
  | Database              |
  +-----------------------+
  | information_schema    |
  | AL                    |
  | cloversoup            |
  | codebar               |
  | mysql                 |
  | performance_schema    |
  | real_estate_analytics |
  | teste                 |
  +-----------------------+
  8 rows in set (0.00 sec)
  </code></pre>
  
  <p>I have also deleted the anonymous user to avoid collisions. My users are</p>
  
  <pre><code>mysql&gt; select user,host from mysql.user;
  +---------+-----------+
  | user    | host      |
  +---------+-----------+
  | root    | %         |
  | crawler | localhost |
  | root    | localhost |
  +---------+-----------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>I have given all grants to crawler (and flushed privileges), so that should not be the problem:</p>
  
  <p><code>GRANT ALL PRIVILEGES ON *.* TO crawler@localhost IDENTIFIED BY 'crawler' WITH GRANT OPTION;</code></p>
  
  <p><code>FLUSH PRIVILEGES;</code></p>
  
  <p>as a matter of fact:</p>
  
  <pre><code>mysql&gt; 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)
  </code></pre>
  
  <p>I have also tried to connect using the <code>root</code> user, by doing </p>
  
  <p><code>con = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'my_root_password', db = 'real_estate_analytics')</code></p>
  
  <p>but it doesn't work either (same error).</p>
  
  <p>This thing is driving me crazy. Does anyone have an insight on what the problem could be?</p>
  
  <p>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</p>