Troubleshooting "Can't Connect to Local MySQL Server Through Socket" Error
When attempting to establish a connection to a local MySQL server during testing, a user encountered an OperationalError with the following message:
OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
Despite the error, the user was able to connect successfully using the mysql command line program. As the server appeared to be running and the socket existed, the cause remained elusive.
Investigation
The issue exhibited intermittent behavior, sometimes affecting the test suite and other times not. Switching to use the host 127.0.0.1 resulted in a different error:
DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)
Solution
The suggested solution was to execute the following command as a superuser:
sudo /usr/local/mysql/support-files/mysql.server start
This command successfully started the MySQL server and allowed the user to establish a connection without encountering the previous error.
Additional Considerations
If the above solution does not resolve the issue, it is advisable to verify that mysqld is running and then attempt to connect to the server.
The above is the detailed content of Why Can't I Connect to My Local MySQL Server Through Socket, But the Command Line Works?. For more information, please follow other related articles on the PHP Chinese website!