Recently learned about Django, the database chose MySQL, in the process of connecting the database, encountered a small problem, here to record, I hope to meet the same problem of friends to help, less go some detours. For Django, here's an extra word to say. Django is a very good Python-based web development framework, and for background programmers with Python basics, Django is highly recommended if there is a need to do some foreground work. Down to the bottom.
- 1. The phenomenon of the problem
Here is the code for connecting to the database, using the Python mysqldb module:
12345 |
db = MySQLdb.connect (host= ' localhost ', port=3306, user= ' root ', passwd= ' root98 ', db= ' MySite ') |
Here are the errors that are reported when running:
1234567 |
Traceback (most recent): file "./test_db.py", line A, in < module> db= ' mysite ') file "build/ bdist.linux-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)") |
- 2. Problem cause Analysis
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:
123456789101112 |
# The following options would be passed to all MySQL clients[client] #password = your_passwordport = 3306socket =/tmp/mysql.sock # the MySQL server[mysqld]bind-address = 10.12.22.98port = 3306socket =/tmp/mysql.sock# ... |
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.connect (host= ' localhost ', port=3306, user= ' root ', passwd= ' root98 ', db= ' MySite ', unix_socket= '/tmp/mysql.sock ') |
(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 = 3306socket =/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.connect (host= ' 10.12.22.98 ', port=3306, user= ' root ', passwd= ' root98 ', db= ' MySite ') |
(1) Unix Domain Socket
(2) MySQL support remote access
Reference article: http://www.wuzesheng.com/?p=2234
Python mysqldb connection MySQL failed (reprint)