Python mysqldb connection MySQL failed (reprint)

Source: Internet
Author: User
Tags unix domain socket

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 ')
      • 4. References

(1) Unix Domain Socket
(2) MySQL support remote access

Reference article: http://www.wuzesheng.com/?p=2234

Python mysqldb connection MySQL failed (reprint)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.