Windows10下Python连接MySQL报错:Authentication plugin 'auth_gssapi_client'无法加载求解决
解决Python连接MySQL时的
auth_gssapi_client加载错误 以下是几个有效的解决方案,按优先级尝试:
修改连接代码,指定认证插件
Windows本地环境下root用户通常使用mysql_native_password而非GSSAPI认证,在连接参数中明确指定即可,同时修复原代码中连接失败时finally块的报错问题:import mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect( host='localhost', database='quiz', user='root', password='1234', auth_plugin='mysql_native_password' # 添加认证插件指定 ) if connection.is_connected(): print("Connected to MySQL successfully") except Error as e: print("Error while connecting to MySQL", e) finally: if 'connection' in locals() and connection.is_connected(): connection.close() print("MySQL connection is closed")检查并修改MySQL用户的认证插件
登录MySQL命令行或Workbench,执行命令查看root用户的认证插件:SELECT user, plugin FROM mysql.user WHERE user='root';如果结果中
plugin列不是mysql_native_password,执行以下命令修改:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234'; FLUSH PRIVILEGES;替换
1234为你的root用户实际密码。重装兼容版本的mysql-connector-python
部分新版本连接器在Windows环境下存在兼容问题,尝试卸载后安装稳定版本:pip uninstall mysql-connector-python -y pip install mysql-connector-python==8.0.30将MySQL的bin目录加入系统PATH
右键「此电脑」→ 属性 → 高级系统设置 → 环境变量,在系统变量的PATH中添加MySQL安装目录下的bin路径(例如C:\Program Files\MySQL\MySQL Server 8.0\bin),添加后重启命令行或IDE。
内容的提问来源于stack exchange,提问作者Lenntox
相关产品推荐
相关产品推荐

