问题背景
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;
结果:问题解决