mysql cant connect to local socket

How to Fix ‘Can’t Connect to Local MySQL Socket’ Error

MySQL is a popular programming language used by many websites & organizations. Sometimes while using MySQL, you may get an error saying ‘can’t connect to local MySQL socket’. In this article, we will learn how to fix this error.

How to Fix ‘Can’t Connect to Local MySQL Socket’ Error

This error mostly occurs in Linux systems and not Mac or Windows ones. Mostly it is because of a configuration error in my.cnf file. Open terminal and run the following command to open this file.

$ vi /etc/mysql/my.cnf

Look for the line starting with ‘socket =’

socket = ...

Ensure it reads as follows. If not, edit it to the following.


Save and close the file. Restart MySQL server to apply changes.

$ sudo service mysqld restart

This should solve the problem in most cases. You can check the status of your MySQL server with the following command.

$ mysqladmin -u root -p status

If you still get the error, try changing the permissions of /var/lib/mysql folder as shown below.

$ sudo chmod -R 755 /var/lib/mysql/

This should also solve your problem.

Of course, there are other things to try if you still get the problem.

  1. Make sure your MySQL server is actually running. You can use the above mentioned mysqladmin command for this purpose
  2. Ensure that you have installed MySQL client as well as server. You can do so with the following command yum -y install mysql mysql-server (or apt-get install mysql mysql-server)
  3. Use IP address as instead of using localhost

These three methods are less common causes of this issue. Nevertheless, you can try them if nothing seems to be working for you.

In this article, we have seen several ways to fix Can’t Connect to Local MySQL Socket’ Error in MySQL.

Also read:

How to Make Case Sensitive String Comparison in MySQL
How to Fix Error MySQL Shutdown Unexpectedly
How to Fix 2006 MySQL Server Has Gone Away
How to Get Previous URL in JavaScript
How to Get Client Timezone & Offset in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *