Traceback (most recent): File "./test_db.py", line A, in < module> db= ' mysite ') file "Build/bdist.li nux-x86_64/egg/mysqldb/__init__.py ", line Bayi, 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)")
This is mainly because when we connect MySQL, host uses localhost, the Unix Domain Socket is actually used (see reference (1)) to communicate. We know that the address of the UNIX Domain socket is the path of a socket-type file in the file system, and if the path does not exist, the connection will fail. The reason for the error above is that "can ' t connect to local MySQL server through socket '/var/lib/mysql/mysql.sock ' (2)", literally, is said to be unable to pass '/var/lib /mysql/mysql.sock ' This socket to connect to the local MySQL sever, this time the problem is relatively obvious, should be the MySQL configuration of the local connection socket is not '/var/lib/mysql/mysql.sock ' The reason for this path. Next we are going to verify our idea of opening the MySQL configuration file (/etc/my.cnf) and we see the following:
As we can see, the UNIX Domain socket configured by the local MySQL server is/tmp/mysql.sock, not the same as the Python mysqldb above, which confirms our previous conjecture and finds the cause of the problem.
3. How to solve the problem
Knowing the problem, we can be the right remedy, here are a few solutions to solve the problem:
(1) When the Python mysqldb is connected, specify the Unix_socket
123456 |
DB mysqldb.host Port passwd db Unix_socket |
(2) Modify the Unix Domain Socket of the local MySQL server
123456789101112 |
# The following options would be passed to all MySQL clients[client] #password = Your_passwordport = 3306so Cket =/var/lib/mysql/mysql.sock # the mysql server[mysqld]bind-address = 10.12.22.98port = 3306socket =/var/lib/mysql/mysql.sock# ... |
(3) Modify the local MySQL server to support remote access (see Reference (2)), connect using a common socket
12345 |
DB mysqldb.host Port passwd db |
4. References
(1) Unix Domain Socket
(2) MySQL support remote access
Python mysqldb connection MySQL failed