Python2.7连接MySQL8.0

import MySQLdb

conn = MySQLdb.connect(host='192.168.3.94',
                       user='root', 
                       passwd='123456', 
                       port=3306,
                       charset='utf8')

conn.select_db('test')
cur = conn.cursor()

a = cur.execute('select * from users;')

print(a)

conn.close()

报错:

_mysql_exceptions.OperationalError: (1251, 'Client does not support authentication protocol requested by server; consider upgrading MySQL client')

经搜索,是保存密码的加密方式不同导致,改为以前的加密方式即可解决:

mysql -u root -p
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> FLUSH PRIVILEGES;

这里的'root'@'%',是经过我修改后的,默认应该为'root'@'localhost'
可以输入SQL命令查看:

mysql> use mysql;
mysql> select host,user from user;

想要将它从localhost改为%只需要:

mysql> update user set host = '%' where user = 'root';
mysql> flush privileges;

你可能感兴趣的:(Python2.7连接MySQL8.0)