django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

Viewed 34940

when I used command "python manage.py makemigrations"(the same with typing "python manage.py runserver"),it threw this error.And then I checked the authority of the users. The code and result are as fllows.

mysql> select host,user from mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| %         | zhuxin        |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
| localhost | zhuxin        |
+-----------+---------------+


mysql> show grants for 'zhuxin'@'%'; 
+---------------------------------------------------------------+ 
| Grants for zhuxin@%                                           |
+---------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhuxin'@'%' WITH GRANT OPTION | 
| GRANT ALL PRIVILEGES ON `blogdb`.* TO 'zhuxin'@'%'            |  
+---------------------------------------------------------------+ 

mysql> show grants for 'root'@'%';
+--------------------------------------------------+
| Grants for root@%                                | 
+--------------------------------------------------+ 
| GRANT USAGE ON *.* TO 'root'@'%'                 | 
| GRANT ALL PRIVILEGES ON `blogdb`.* TO 'root'@'%' |  
+--------------------------------------------------+

I have tried everything to solve it, but for no use.

6 Answers

You need to make changes in project settings.py. Provide USER and PASSWORD for your database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'root',
        'PASSWORD': 'rootpassword',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Check Your Database Username and Password. many time, we do typing mistake. it doesn't match from the database username and password so occur this error. I faced this error and trust me, it is my very bad experience.

MySQL DB

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your database name',
        'HOST': 'localhost',
        'PASSWORD': 'your database password',
        'USER': 'root'
    }
}

I tried by just removing the whole password and it worked so what I concluded is that the password which I was entering was wrong and so it showed that error and since there was no password set by me so it worked when I removed the password

I had the same error and changing the HOST to 127.0.0.1 and removing the password worked for me. You can try it out and see if it will work for you as well.

I saw this error message after copying my project onto a new machine from a backup file. An additional error message explained the problem:

[Warning] World-writable config file '/home/myuser/myproject/database.cnf' is ignored.

The permissions of my database configuration file had been changed and it was not being loaded, therefore Django was trying to connect with the wrong user. I fixed this by running:

chmod 600 database.cnf
Related