Home  >  Article  >  Database  >  Why Can't I Connect to My Local MySQL Server Through the Socket '/tmp/mysql.sock' in Python, Despite the Server Running?

Why Can't I Connect to My Local MySQL Server Through the Socket '/tmp/mysql.sock' in Python, Despite the Server Running?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-10 18:38:02706browse

Why Can't I Connect to My Local MySQL Server Through the Socket '/tmp/mysql.sock' in Python, Despite the Server Running?

Troubleshooting MySQL Connection Error: "Can't Connect to Local MySQL Server Through Socket '/tmp/mysql.sock'"

Despite being able to establish connections to the MySQL server using the command line, an attempt to connect from a test suite in Python fails with the error:

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

Verifications such as running 'ps aux | grep mysql' to confirm server process existence and 'stat /tmp/mysql.sock' to check for the socket's presence do not reveal any issues. Furthermore, a debugger opened under the exception clause allows successful connections using the same parameters.

The problem manifests intermittently and switching to '127.0.0.1' as host results in a different error:

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

Solution:

Executing the following command resolved the issue:

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

Additional Considerations:

  • If the above command fails, ensure that mysqld is running before attempting the connection.
  • The fact that the client code is in Python does not appear to be relevant to the issue.

The above is the detailed content of Why Can't I Connect to My Local MySQL Server Through the Socket '/tmp/mysql.sock' in Python, Despite the Server Running?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn