安装mysql-connector后执行程序出现Bad handshake错误求助
解决MySQL Connector连接时的Bad handshake错误
以下是针对1043 (08S01): Bad handshake错误的常见排查和解决步骤:
1. 版本兼容性问题
MySQL服务器版本与mysql-connector版本不匹配是最常见的原因:
- 如果你的MySQL是8.0及以上版本,需要使用8.0+版本的mysql-connector-python(旧版
mysql-connector已停止维护)。执行以下命令升级或安装正确的驱动:pip uninstall mysql-connector pip install --upgrade mysql-connector-python - 若使用的是MySQL 5.x版本,可安装适配的低版本驱动,例如:
pip install mysql-connector-python==8.0.28
2. 认证加密方式不兼容
MySQL 8.0默认使用caching_sha2_password认证插件,旧版驱动无法支持该加密方式:
- 方式一:升级驱动到8.0+版本(推荐)
- 方式二:修改MySQL用户的认证插件为旧版兼容的
mysql_native_password:
登录MySQL执行以下SQL:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'tiger'; FLUSH PRIVILEGES;
3. 检查连接参数与服务状态
- 确认MySQL服务已正常启动,可通过系统服务列表或命令
net start mysql(Windows)/systemctl start mysql(Linux)启动服务。 - 连接时指定正确的端口号(默认3306),修改代码:
import mysql.connector as c con=c.connect(host="localhost",user="root",passwd='tiger',database='employee', port=3306) if con.is_connected(): print("success") else: print("connectivity issue")
4. 修复驱动安装异常
如果驱动安装不完整或损坏,可彻底卸载后重新安装:
pip uninstall -y mysql-connector mysql-connector-python pip install mysql-connector-python
内容的提问来源于stack exchange,提问作者Sahil Panhalkar
相关产品推荐
相关产品推荐

