@@ -64,9 +64,28 @@ def get_db_config():
6464 'db' : config ['database' ], # 'database' -> 'db'
6565 'port' : config ['port' ],
6666 'connect_timeout' : config .get ('connection_timeout' , 5 ), # 'connection_timeout' -> 'connect_timeout'
67- # auth_plugin在aiomysql中不直接支持,忽略此参数
67+ 'charset' : 'utf8mb4' , # 确保字符集支持
6868 }
6969
70+ # MySQL 8.0 认证插件支持
71+ # 如果指定了认证插件且不是默认的mysql_native_password,则添加到配置中
72+ auth_plugin = config .get ('auth_plugin' , 'mysql_native_password' )
73+ if auth_plugin != 'mysql_native_password' :
74+ # 对于caching_sha2_password等现代认证插件,需要确保cryptography包可用
75+ try :
76+ import cryptography
77+ # 添加认证插件配置以支持caching_sha2_password
78+ aiomysql_config .update ({
79+ 'auth_plugin' : auth_plugin
80+ })
81+ logger .debug (f"使用认证插件: { auth_plugin } (已检测到 cryptography 包)" )
82+ except ImportError :
83+ logger .warning (f"检测到认证插件 { auth_plugin } ,但未安装 cryptography 包" )
84+ logger .warning ("将回退到 mysql_native_password 认证方式" )
85+ logger .warning ("要完全支持 MySQL 8.0 认证,请安装: pip install cryptography" )
86+ else :
87+ logger .debug (f"使用认证插件: { auth_plugin } " )
88+
7089 return aiomysql_config
7190
7291# 自定义异常类,细化错误处理
@@ -176,7 +195,17 @@ def _finalizer(p=pool, lid=loop_id):
176195 elif "Can't connect" in error_msg or "Connection refused" in error_msg :
177196 raise MySQLServerError ("无法连接到MySQL服务器,请检查服务是否启动" )
178197 elif "Authentication plugin" in error_msg :
179- raise MySQLAuthPluginError (f"认证插件问题: { error_msg } ,请尝试修改用户认证方式为mysql_native_password" )
198+ current_auth = DatabaseConfig .AUTH_PLUGIN
199+ error_detail = f"认证插件问题: { error_msg } "
200+ if current_auth == 'caching_sha2_password' :
201+ error_detail += "\n 解决方案:"
202+ error_detail += "\n 1. 确保已安装 cryptography 包: pip install cryptography"
203+ error_detail += "\n 2. 或者修改用户认证方式为 mysql_native_password"
204+ error_detail += "\n 3. 或者在 .env 中设置 DB_AUTH_PLUGIN=mysql_native_password"
205+ else :
206+ error_detail += f"\n 当前认证插件配置: { current_auth } "
207+ error_detail += "\n 请检查 MySQL 用户的认证插件设置是否匹配"
208+ raise MySQLAuthPluginError (error_detail )
180209 else :
181210 raise MySQLConnectionError (f"数据库连接失败: { error_msg } " )
182211 except Exception as e :
@@ -239,7 +268,17 @@ async def get_db_connection(require_database: bool = True):
239268 elif "Can't connect" in error_msg or "Connection refused" in error_msg :
240269 raise MySQLServerError ("无法连接到MySQL服务器,请检查服务是否启动" )
241270 elif "Authentication plugin" in error_msg :
242- raise MySQLAuthPluginError (f"认证插件问题: { error_msg } ,请尝试修改用户认证方式为mysql_native_password" )
271+ current_auth = DatabaseConfig .AUTH_PLUGIN
272+ error_detail = f"认证插件问题: { error_msg } "
273+ if current_auth == 'caching_sha2_password' :
274+ error_detail += "\n 解决方案:"
275+ error_detail += "\n 1. 确保已安装 cryptography 包: pip install cryptography"
276+ error_detail += "\n 2. 或者修改用户认证方式为 mysql_native_password"
277+ error_detail += "\n 3. 或者在 .env 中设置 DB_AUTH_PLUGIN=mysql_native_password"
278+ else :
279+ error_detail += f"\n 当前认证插件配置: { current_auth } "
280+ error_detail += "\n 请检查 MySQL 用户的认证插件设置是否匹配"
281+ raise MySQLAuthPluginError (error_detail )
243282 else :
244283 raise MySQLConnectionError (f"数据库连接失败: { error_msg } " )
245284 except Exception as e :
0 commit comments