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文件的路径