Software⏱️ 4 min read📅 2026-06-03

How to Fix ERROR 2002 Error – ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

MySQL connection error on Mac OS X Mountain Lion.

Quick Answer: The error means MySQL is not configured to use the default socket file. Try running `brew services stop mysql` and then `brew services start mysql` to restart the service.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' is an error that occurs when you try to connect to a MySQL server on your local machine using the socket file '/tmp/mysql.sock'. This error affects users who have installed MySQL on their Mac OS X Mountain Lion operating system. The error message can be frustrating because it doesn't provide any clear information about what's causing the issue, making it difficult to troubleshoot and resolve.

The root cause of this error is often related to the way MySQL is configured or not properly initialized on your system. It can also be caused by issues with the socket file itself or problems with the MySQL server process. This guide will walk you through the steps to diagnose and fix this issue, providing a clear solution for users who are experiencing this problem.

💡 Why You Are Getting This Error

  • The primary reason for this error is that MySQL is not properly initialized on your system. When you install MySQL using homebrew, it creates a socket file in the /tmp directory. However, if the MySQL server process is not started or not running correctly, the socket file may not be created or may be missing, causing the 'Can't connect to local MySQL server through socket' error. Additionally, issues with the socket file itself, such as permissions problems or corruption, can also cause this error.
  • Another possible reason for this error is that the MySQL server process is running, but it's not listening on the correct port or using the correct socket file. This can happen if the MySQL configuration file (my.cnf) is not properly set up or if there are issues with the MySQL server process itself.

🔧 Proven Troubleshooting Steps

Starting and enabling the MySQL server service

  1. Step 1: To start and enable the MySQL server service, open a terminal on your Mac OS X Mountain Lion system. Type the following command to start the MySQL server service: sudo launchctl load /Library/LaunchAgents/org.macosx.server.mysql.plist. This will start the MySQL server process and create the socket file in the /tmp directory if it doesn't exist already.
  2. Step 2: Next, type the following command to enable the MySQL server service to start automatically on boot: sudo launchctl enable org.macosx.server.mysql. This will ensure that the MySQL server process starts every time your system boots up.
  3. Step 3: Finally, restart your system and try connecting to the MySQL server again using the 'mysql' command. If you're still experiencing issues, proceed to method 2 for alternative solutions.

Checking and correcting MySQL configuration file

  1. Step 1: To check the MySQL configuration file (my.cnf), open a text editor and navigate to /etc/my.cnf. Look for the following lines: socket = /tmp/mysql.sock and bind-address = 127.0.0.1. If these lines are not present or are commented out, add them back in with the correct values.
  2. Step 2: Next, check the MySQL server process logs to see if there are any error messages related to the socket file or port configuration. You can view the logs by typing the following command: sudo tail -f /var/log/mysql/error.log
  3. Step 3: If you find any errors related to the socket file or port configuration, correct them according to the MySQL documentation and try connecting to the MySQL server again.

🎯 Final Words

To summarize, the 'Can't connect to local MySQL server through socket' error can be resolved by starting and enabling the MySQL server service, checking and correcting the MySQL configuration file, or troubleshooting issues with the socket file itself. By following these steps, users should be able to resolve this issue and access their MySQL server again.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions