Max Kim
Max Kim

Reputation: 1124

Python MySQL OperationalError: 1045, "Access denied for user root@'localhost'

I was trying to access the database from my python program by:

db = mysql.connect(host = 'localhost', user = 'Max', passwd = 'maxkim', db = 'TESTDB')
cursor = db.cursor()

But, I get an error message in the first line of code.

OperationalError: (1045, "Access denied for user 'Max'@'localhost' (using password: YES)")

To remedy the situation, I did the following:

$ mysql -u Max-p
Enter password: maxkim

mysql> create database TESTDB;
mysql> grant usage on *.* to Max@localhost identified by ‘maxkim’;
mysql> grant all privileges on TESTDB.* to Max@localhost ;
mysql> exit

If I have granted all access to the the database for the user "Max" (me), why can't I still connect in python?

Upvotes: 5

Views: 37676

Answers (2)

Noam Rathaus
Noam Rathaus

Reputation: 5598

I suggest to everyone to always use mysql_setpermission rather than direct MySQL statements, mysql_setpermission does everything for you including FLUSHing. Any other way is prone to mistakes.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213261

You can try adding GRANT OPTION at the end:

GRANT all privileges on TESTDB.* to 'Max'@'localhost' IDENTIFIED BY 'maxkim' WITH GRANT OPTION;

You can also use the below command to find the grants for a user:

SHOW GRANTS FOR 'Max'@'localhost';

Also See:

Upvotes: 8

Related Questions