【踩坑】Django django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password:


問題背景

Django項目初始化,在MySQL數據庫中創建表報錯

python  manage.py   migrate

報錯如下:

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

解決過程

  • 嘗試1:bind-address = 0.0.0.0
    my.cnf配置文件中注釋掉bind-address = 127.0.0.1,新增配置bind-address = 0.0.0.0,重啟MySQL服務
[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1
bind-address = 0.0.0.0

結果:同樣報錯

  • 嘗試2:修改root用戶的權限,刷新權限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;

結果:同樣報錯

  • 嘗試3:修改root的密碼
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

結果:同樣報錯

*** 崩潰的分割線 ***

  • 嘗試4:嘗試在本地連接數據庫

import mysql.connector

conn = mysql.connector.connect(host="127.0.0.1",user='root',database='website',password='YourPassword')

# 獲游標標對象

print(conn)

報錯:Authentication method 'caching_sha2_password' is not supported,原因在MySQL 8.X中 caching_sha2_password is the default authentication plugin rather than mysql_native_password.

解決辦法:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';
FLUSH PRIVILEGES;

結果:問題解決

參考鏈接:https://stackoverflow.com/questions/50557234/authentication-plugin-caching-sha2-password-is-not-supported


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM