转载:https://www.cnblogs.com/heymonkey/p/11715295.html
mysql连接错误:mysql.connector.errors.NotSupportedError) Authentication plugin ‘caching_sha2_password’ is not supported
def mysqlexecute(sql):
# 获取当前文件路径
filePath = os.path.dirname(__file__)
# 获取当前文件的
fileNamePath = os.path.split(os.path.realpath(__file__))[0]
# 获取配置文件的路径
yamlPath = os.path.join(fileNamePath, 'mysqltest.yml')
filemysql = open(yamlPath, 'r', encoding='utf-8')
f=filemysql.read()
con=yaml.load(f)
list=con.get('mysql.connector.connect')
print(list)
# conn = con.get('mysql.connector.connect')
conn=mysql.connector.connect(
host=list.get('host')
,user =list.get('user') # 用户名
,passwd = list.get('passwd') # 密码
,port =list.get('port') # 端口,默认为3306
,db = list.get('db') # 数据库名称
,charset = list.get('charset') # 字符编码
)
print(conn)
cur=conn.cursor()
cur.execute(sql)
date=cur.fetchall()
print(date)
# for i in date[i]:
# print(i)
cur.close()
conn.close()
return date
运行时报错:错误描述
mysql.connector.errors.NotSupportedError) Authentication plugin ‘caching_sha2_password’ is not supported
发现是:
conn=mysql.connector.connect(
host=list.get('host')
,user =list.get('user') # 用户名
,passwd = list.get('passwd') # 密码
,port =list.get('port') # 端口,默认为3306
,db = list.get('db') # 数据库名称
,charset = list.get('charset') # 字符编码
)
这里报错:
排查后发现是缺少MySQL-connector-python
安装
pip install MySQL-connector-python
就正常访问