Python mysqldb connection MySQL failed

Source: Internet
Author: User
Tags unix domain socket

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

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.