python连接mysql数据库报错pymysql.err.OperationalError

报错如下:

历史工程使用的是python3.6+pymysql+mysql8.0

原因分析:

1、可能密码错误,通过navicat可以正常连接, 可排除

2、可能新版mysql默认使用的caching_sha2_password认证方式,换成mysql_native_password就可以

解决方法:

1、查看用户相关信息

select host
,user,plugin,authentication_string from user where user='root';

如果authentication_string为caching_sha2_password,则进行修改,这里需要修改

2、进入msql服务,执行mysql -u root -p 回车,输入密码,也可以通过navicat/DBeaver连接数据库

3、修改

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
4、刷新内存,重新加载权限信息

flush privileges;

通过上述一泼操作,即可解决

优化思考,一般程序里头不会直接使用root,权限范围过大,这里新建用户,并授予特定数据库权限,供ai服务使用

1、创建账号:ai_user,密码:pw@666的用户

create user 'ai_user'@'%' identified by 'pw@666'; 

2、授予数据库权限(对用户ai_user授予操作数据库ai_server的所有权限)

GRANT ALL PRIVILEGES ON ai_server.* TO 'ai_user'@'%';

3、刷新内存,重新加载权限信息

flush privileges;

你可能感兴趣的:(mysql,python,pymysql)