You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 03:53:07