且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过套接字'/tmp/mysql.sock连接到本地MySQL服务器

更新时间:2023-02-04 21:30:49

  sudo / usr / local / mysql / support -files / mysql.server start 

这对我有用。但是,如果这不行,请确保mysqld正在运行并尝试连接。


When I attempted to connect to a local MySQL server during my test suite, it fails with the error:

OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

However, I'm able to at all times, connect to MySQL by running the command line mysql program. A ps aux | grep mysql shows the server is running, and stat /tmp/mysql.sock confirm that the socket exists. Further, if I open a debugger in except clause of that exception, I'm able to reliably connect with the exact same parameters.

This issue reproduces fairly reliably, however it doesn't appear to be 100%, because every once in a blue moon, my test suite does in fact run without hitting this error. When I attempted to run with sudo dtruss it did not reproduce.

All the client code is in Python, though I can't figure how that'd be relevant.

Switching to use host 127.0.0.1 produces the error:

DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)

sudo /usr/local/mysql/support-files/mysql.server start 

This worked for me. However, if this doesnt work then make sure that mysqld is running and try connecting.