Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Encountering "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" could mean that MySQL is not running or there is a misconfigured socket. Here are some initial steps to try:
- Start MySQL using
sudo service mysql start
. - Confirm the socket path in
my.cnf
, or use the--socket
flag with the MySQL client. - Connect using socket path if non-default:
mysql -u user -p --socket=/path/to/mysql.sock
.
Checking MySQL service status and ensuring the correctness of socket path might help. It's also wise to plan for OS updates, which can stop the service—thus necessitating a service restart.
Troubleshooting basic server issues
Ensuring that MySQL server is running
Let's not assume. Issues could be as simple as the MySQL server not running:
Verifying socket availability
The /tmp/mysql.sock
file should physically exist. If it doesn't, we need to find it or consider creating it:
Permanent symbolic links
To persist symbolic links, consider these options:
- Edit
.profile
: Add symbolic link creation command. - Create alias: Add an alias to your shell profile, like
.bashrc
or.zshrc
:alias fixmysql='ln -s /actual/mysql.sock /tmp/mysql.sock'
.
Dealing with non-standard issues
Adjust client configuration
my.cnf
or .cnf
might harbor the wrong client settings. Ensure they're in sync with the server:
Socket-less connection
TCP/IP can be your friend if the socket file is acting up:
Try MAMP
Mac users, MAMP could spare you the headache by providing a MySQL-friendly environment.
Maintain connections and configurations
Modifying server path
Change socket path if MySQL uses an unusual one:
Symbolic links and system startup
Ensure links persist across reboots via cron
or systemd
. Also, ensure MySQL is part of the startup services.
Confirm consistent access
Last but not least, check MySQL’s status in System Preferences or Services, and verify your access by confirming a password prompt when connecting to MySQL.
Was this article helpful?