工具:python3.6+mysql8.0+pymysql
注意:python2.7以后的版本不再支持 MySQLdb这个模块。需要用pymysql,在命令行下安装的方法 输入pip install pymysql。
import pymysqlconn = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='root', db='imooc', charset='utf8')cursor = conn.cursor()sql = 'select * from students'# print(cursor)cursor.execute(sql)print(cursor.fetchall())cursor.close()conn.close()执行这段代码出错 : pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: No)")
新版mysql使用的caching_sha2_password认证方式,换成mysql_native_password就可以。
步骤是在cmd命令行连接mysql,
然后输入ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
来源:https://www.cnblogs.com/Abelte/p/9138121.html