How to fix MySQL ERROR 2002 Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock

Posted on Nov 22, 2021

Learn how to fix MySQL ERROR 2002 Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock

When you try to connect to your MySQL server through the command line, you might encounter a MySQL ERROR 2002 as follows:

mysql -uroot -proot
ERROR 2002 (HY000): Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (2)

The ERROR 2002 above happens when the mysql.sock socket file can’t be found in your filesystem.

This file is created when MySQL server is started and removed when you stop the server.

To fix this error, you need to see if MySQL server is already installed and running on your computer.

If you’re using Linux, you may need to install mysql-server in addition to the mysql package:

apt-get install mysql-server mysql

Once you have the server installed, run the server with the following command:

sudo service mysql start
# or
sudo /etc/init.d/mysql start

That should start the server and generate the mysql.sock file. You can try to connect to your MySQL server again now.

For macOS

If you’re using macOS and installed MySQL using Homebrew, then you need to make sure that the server is started using the following command:

brew services start mysql

Once MySQL is running, you can try to connect using the mysql command again.

For Windows

For Windows OS, you need to make sure that MySQL service is running in the Services panel.

Open the Windows Start menu and search for the Services panel to see the result below:

Then, scroll through the services list until you reach the services that start with "M" to look for MySQL services.

Usually, you have the MySQL version number attached to the service name.

The MySQL version installed on my computer is MySQL 8.0.26 so I have MySQL80 service listed as shown below:

If you have MySQL version 7, then you may have MySQL70 listed on the Services panel.

As you can see from the picture above, the status of MySQL80 service is empty, meaning that it’s not currently running.

If you see the same status, you can run the service by clicking the Start the service link on the left pane.

Now you can try to connect again to your MySQL server from the Command Line.

To conclude, the ERROR 2002 happens when your computer can’t connect to MySQL server because the socket file is missing.

The socket file is generated when MySQL server is started, so you probably need to start the server to make it work.

Depending on your operating system, there are different ways to start your MySQL server.

Good luck fixing the error! 👍

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.