MySQL-python接続socketの問題

1943 ワード

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ファイルのパスを指定します