I suddenly can't login to mysql after my computer restarted! I'm in directory /opt/homebrew/etc. Here is my my.cnf:
mysql -u -root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
mysql -u -root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Assistant
I'm sorry to hear that you're having trouble connecting to MySQL after restarting your computer. The error message you're encountering:
code
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
indicates that the MySQL client cannot find or access the MySQL server socket file at /tmp/mysql.sock. This issue can occur for several reasons, such as the MySQL server not running, the socket file being located elsewhere, or configuration mismatches.
Here are some steps to help you diagnose and resolve the problem:
1. Verify if the MySQL Server is Running
First, check whether the MySQL server is running. Since you're using Homebrew on macOS, you can use the following commands:
Check MySQL Service Status:
bash
brew services list
Look for the mysql entry in the list. If it shows as stopped, the server isn't running.
Start the MySQL Server:
bash
brew services start mysql
Alternatively, you can start MySQL manually:
bash
mysql.server start
Check if MySQL Process is Running:
bash
ps aux | grep mysql
2. Locate the MySQL Socket File
The MySQL client is attempting to connect via the socket file at /tmp/mysql.sock, but the socket might be located elsewhere.
Search for the Socket File:
bash
sudo find / -name 'mysql.sock' 2>/dev/null
Common locations include:
/tmp/mysql.sock
/var/lib/mysql/mysql.sock
/usr/local/var/mysql/mysql.sock
/opt/homebrew/var/mysql/mysql.sock
3. Update MySQL Configuration (my.cnf)
Your my.cnf file may not have the correct socket path. Edit the configuration file located at /opt/homebrew/etc/my.cnf.