[Fixed]-Django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

19👍

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': '',
    }
}

2👍

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.

2👍

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.

👤franck

0👍

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

0👍

I accidentally used USERNAME:

'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'inventory',
    'HOST': '127.0.0.1',
    'USERNAME': 'root',
    'PORT': '',
    'PASSWORD': '',  # Your Password
}

Just change it to USER

0👍

Remove the password field if not set.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "reservations",
        "HOST": "127.0.0.1",
        "PORT": "3306",
        "USER": "root",
    }
}

Add this snippet of code.

0👍

In My case I was using the wrong port number, I have configured the MySql on 3307 and was using 3306 in the settings.py file

DATABASES = {
"default": {
    "ENGINE": "django.db.backends.mysql",
    "NAME": "reservations",
    "HOST": "127.0.0.1",
    "PORT": "3307",
    "USER": "root",
}

}

-1👍

I had the same problem and I learn from linuxnightly.com that it is caused by auth socket plugin, once you disable it the problem is solved.

Try running the following commands after you are logged into mysql as root:

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password_here';
mysql> exit;
$ sudo systemctl restart mysql

To make this work, you have to change root’s password. This is done by the UPDATE command. You have to restart mysql at the end of the process with the systemctl command at the end.

source: https://linuxnightly.com/error-1698-28000-access-denied-for-user-rootlocalhost/

Leave a comment