Error #2002: Connecting to MySQL Server through Socket
When attempting to connect to a local MySQL server through MAMP, users may encounter the following error:
error #2002 "Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)"
Possible Causes:
The error typically occurs when the MySQL socket file (/Applications/MAMP/tmp/mysql/mysql.sock) is missing or inaccessible.
Solution:
Testing:
Try starting MySQL using the full path:
/Applications/MAMP/Library/bin/mysql -u root -p
If this works, proceed to the fix.
Fixing:
Run the following command to create a symbolic link to the MySQL socket file:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
Now MySQL should work normally using the command:
mysql -u root -p
Fallback Solution (Dynamic Path Finding):
If the above solution fails, try to find the MySQL path dynamically:
Run the following command:
$($(for dir in /usr/local/mysql/bin /usr/bin /usr/local/bin /Applications/MAMP/Library/bin /Applications/XAMPP/xamppfiles/bin; do [ -x "$dir/mysql" ] && echo "$dir/mysql" && break; done) -u root -p)
This will attempt to locate the MySQL executable and then start MySQL.
The above is the detailed content of How to Fix Error #2002: Connecting to MySQL Server through Socket?. For more information, please follow other related articles on the PHP Chinese website!