Can’t connect to local MySQL server through socket [SOLVED

If you work with MySQL, you are probably familiar with the error message cannot connect to the local mysql server via socket. And you will see an error message

like:

Error: ERROR 2002 (HY000): Unable to connect to local MySQL server via socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)

In this short article, we will show you how to fix and fix this problem in a jiffy

. And,

don’t worry, it’s a pretty straightforward solution and you have nothing to worry about

.

So let’s go.

What is Can’t connect to local MySQL server via socket error?

So what is causing the error message cannot connect to the local mysql server via socket?

This error message basically tells you that the application cannot locate the socket file needed to establish a MySQL connection.

And as you well know, the operating system uses the socket file to allow interface services such as MySQL or PHP to interact and communicate with each other.

For some people, this

pop-up message may appear while trying to restart MySQL and run

the /usr/local/mysql/bin/mysql startup command

which results

in the error message: Unable to connect to local MySQL server via socket ‘/var/mysql/mysql.sock’ (38)

On the other hand, for others, this error message arises when they try to log in to the MySQL database using any user credential.

Then you have those who experienced this same problem with their website application, especially those built with PHP.

For example, suddenly, the

website refuses to work, and when you intrude into the log file, you see the error message logged there similar to this:

SQLSTATE[HY000] [2002] Unable to connect to local MySQL server via socket ‘/var/run/mysqld/mysqld.sock’ How to Fix Can’t Connect to Local MySQL Server Through

Socket Error So, How does this fix Can’t connect to local MySQL server via socket error message?

Well, here are some steps you should follow to resolve this issue

:

Solution #1: Check if

the MySQL service is running

First of all, verify that the mysqld service is running or not. To check

if MySQL is running, run the following command: mysqladmin -u root -p status And if the service is not running,

then of course start it by running the following command

: MySQLD Service Start

Once you restart the service, try connecting to MySQL again

. Solution #2: Connect with 127.0.0.1

Another possible solution for the >cannot connect to the local mysql server through the socket > error message is to try to connect to MySQL using the IP address 127.0.0.1 instead of localhost. When localhost is used to connect to MySQL

, the

operating system uses the socket connector.

However, If you use the IP address 127.0.0.1, the operating system uses the TCP/IP connector.

Therefore, as a possible solution when you have problems with the socket connector, you can try to establish the connection using TCP/IP by specifying the IP address 127.0.0.1 instead of localhost.

Solution #3: Modify the

My.cnf File

The my.cnf file is a configuration file used by MySQL. And it is processed by the mysqld or mysqladmin service depending on the specified options.

So locate this my.cnf file that is normally in the /etc/ directory and modify it accordingly as follows

: [mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock After modifying the file,

restart the MySQL service and try connecting again

.

Solution #4: Check the location

of mysql.sock Another possible cause of this problem is that the mysql.sock file in another directory.

Therefore, you need to locate where the mysql.sock file is and create a symbolic link to it

.

Most of the time the file is located in /data/mysql_datadir/mysql.sock or /tmp/mysql.sock So, for

example, if you find the file in /data/mysql_datadir/mysql.sock, all you need to do is create a symbolic link for it using the following command: ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Once

done, go ahead and restart the MySQL service and try to initiate the connection again.

Solution #5: Change MySQL Folder Permission

There is another possible solution to fix this issue and that is to change the MySQL folder permission

. And this can

be easily done by running the following command if you are working in a local

environment: sudo chmod -R 755 /var/lib/mysql/

After which you can restart the mysql

service: starting the mysqld service

Then try to establish the connection again

.

Solution #

6: Multiple

MySQL Instances In some cases, installing multiple MySQLs in the server environment causes

this problem.

So you have a case where multiple instances of MySQL are running at the same time.

As such, the issue running the following commands may fix the problem: ps -A|grep mysql Then delete the mysql process by running this command: sudo pkill

mysql

Then do the same for mysqld: ps -A|grep mysqld

Again, kill this process too

: sudo pkill

mysqld

Finally, run the following commands to restart and connect

to MySQL server: sudo mysql service restart mysql -u root -p Conclusion on unable to connect to local MySQL server via socket We hope that one of the above possible solutions

cannot connect to the local mysql server via socket The error resolves your situation.

Contact US