MySQL-python连接socket问题

Python中使用MySQLdb连接数据库时出现以下错误:

Traceback (most recent call last):
File "/home/monitor_user/serverMonitor.py", line 206, in <module>
ServerList = sqlQuery(getMngConn(), "select b.db_name, a.ip_inside, a.port from machine a inner join dbinfo b on b.machine_id = a.machine_id")
File "/home/monitor_user/serverMonitor.py", line 39, in getMngConn
return MySQLdb.connect(host='localhost', user='monitor_user', passwd='monitor.user', db='dbmanage')
File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

其原因是我将mysql的sock文件已经调整到/tmp/mysql.sock,所以必须在mysqldb中指定sock的路径(这个还是很不智能的,万一我有很多mysql服务器,而每台服务器的sock路径都有差异呢?)

return MySQLdb.connect(host='localhost', user='user', passwd='user', db='dbname', unix_socket='/tmp/mysql.sock' )

unix_socket参数指定了sock文件的路径

posted on   trams  阅读(4494)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
< 2012年1月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

统计

点击右上角即可分享
微信分享提示