Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • MySql version: 8.0 <-- the key to this problem actually
  • OS: macOS 10.13.4
  • Python: 3.6
  • Django: 1.10.3

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.

like image 975
Reed_Xia Avatar asked Oct 25 '25 14:10

Reed_Xia


1 Answers

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.

like image 183
Reed_Xia Avatar answered Oct 27 '25 05:10

Reed_Xia