0

I have a database in mysql and I want to connect to it. I am trying to use this module from Python called MySQLdb. I created an user (called abc) and password (abc) for this database (abc) that has one table and it is connecting ok (when I connect by mysql command line).

But when I run my python script there is an error in the connection. My script is:

#!/usr/bin/python
import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","abc","abc","abc")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data

# disconnect from server
db.close()

My error is:

    Traceback (most recent call last):
    File "./test.py", line 8, in <module>
    db = MySQLdb.connect("localhost","abc","abc","abc")
    File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
        return Connection(*args, **kwargs)
     File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
        super(Connection, self).__init__(*args, **kwargs2)
 _mysql_exceptions.OperationalError: (1044, "Access denied for user 'abc'@'localhost' to database 'abc'")

What is wrong? My script or something in my mysql? I changed my localhost to 127.0.0.1 (as suggested in another post, but did not solve my issue.

I also checked my permissions for this mysql user:

SHOW GRANTS;
+------------------------------------------------------------------------------+
| Grants for abc@localhost                                                                              |
+------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'abc'@'localhost'                                                               |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, CREATE VIEW ON `abc`.* TO 'abc'@'localhost' |
+------------------------------------------------------------------------------+

1 Answer 1

1

You might need to restart the mysql daemon for the privileges to take affect. Or use the flush privileges command. https://dev.mysql.com/doc/refman/5.7/en/privilege-changes.html

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

1 Comment

thanks. I created my database again in mysql, used flush, and also I used an extra command that seems helped that was: grant usage on . to abc@localhost identified by 'abc';

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.