Authentication method 'caching_sha2_password' failed
时间: 2024-01-19 16:17:12 浏览: 149
根据提供的引用内容,当使用caching_sha2_password认证方法时,如果cache中没有记录或者认证失败,会出现"Authentication method 'caching_sha2_password' failed"的错误。
以下是一个演示如何处理这个错误的示例代码:
```python
import mysql.connector
try:
# 建立数据库连接
cnx = mysql.connector.connect(user='username', password='password',
host='localhost',
database='database_name',
auth_plugin='caching_sha2_password')
# 执行数据库操作
cursor = cnx.cursor()
query = "SELECT * FROM table_name"
cursor.execute(query)
result = cursor.fetchall()
# 关闭数据库连接
cursor.close()
cnx.close()
# 处理查询结果
for row in result:
print(row)
except mysql.connector.Error as err:
if err.errno == mysql.connector.errorcode.ER_ACCESS_DENIED_ERROR:
print("Authentication method 'caching_sha2_password' failed")
else:
print("An error occurred:", err)
```
请注意,上述示例代码中的用户名、密码、主机和数据库名称应根据实际情况进行替换。
阅读全文
相关推荐

















