pythonmysqldjango

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


After I installed Mysql 8.0 in mac, and changed the default root password, I got the error django.db.utils.OperationalError: (1045:Access denied for user 'root'@'localhost' (using password: NO) when I ran python3 manage.py migrate to connect MySql and init database tables, while I can login to mysql with root/new password in console without any error.

And it's using pymysql to work with Mysql, I did lots of google and also in stackorverflow, finally I found the solution in a CSDN tech blog

Just want to share it in stackoverflow, if you also encountered this problem, you may want to try the way in the answer below.


Solution

  • Run the following in mysql console, to change the password encryption method to the old version in Mysql(it is changed to use cha2 in Mysql 8.0)

    mysql -u root -p
    use mysql;
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';  
    FLUSH PRIVILEGES;
    

    Then you should be free to run python manage.py migrate.