[Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Xampp is my favourite webserver for linux as well as windows for web development works. And while importing huge mysql database dumps I use the commandline tool in windows as well as linux. And I often find myself so irritating to the error messages as follows :

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Error 2002 (Hy000)

When mysql is unable to find the appropriate socket to connect with the service, then we get the above error.

The above error can be solved by two different ways.
1. By specifying the correct socket in php.ini settings
2. By creating symbolic link to the appropriate socket file.

I will explain step 2 : How to view the correct socket file to create symbolic link ?

Use command below to identify the correct socket file in use

samundra@samundra-apple:~$ ps -aux | grep mysqld

Output will be something like below ::

root      6244  0.0  0.0   1912   572 pts/0    S    14:41   0:00 /bin/sh /opt/lampp/bin/mysqld_safe --datadir=/opt/lampp/var/mysql --pid-file=/opt/lampp/var/mysql/samundra-apple.pid
nobody    6291  0.1  0.3  32636 13700 pts/0    Sl   14:41   0:09 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --user=nobody --pid-file=/opt/lampp/var/mysql/samundra-apple.pid --skip-external-locking --port=3306 --socket=/opt/lampp/var/mysql/mysql.sock
samundra  8370  0.0  0.0   4160   856 pts/0    S+   16:01   0:00 grep --color=auto mysqld

See the screenshot below
MySql Process Dump
What we just did was that we first verified that the mysqld (service for mysql) is currently running and then we retreived the file in use by the process.

In above output we can see

--socket=/opt/lampp/var/mysql/mysql.sock

This is the required file, where we will create our symbolic link. Now all we have to do is execute the following command.

samundra@samundra-apple:~$ cd /var/run/
samundra@samundra-apple:~$ sudo mkdir mysqld
samundra@samundra-apple:~$ ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock

See the screenshot below for symblic link pointing to the /opt/lampp/var/mysql/mysql.sock file.
Symbolic Link

The above has to be carried out each time you restart linux, so to make things easier what we can do is create a script and execute it manually. It can also be set to execute at boot time, You can google for that part.

I created a file mysql.sh having contents

#!/bin/bash
sudo rm -rf /var/run/mysqld
sudo mkdir /var/run/mysqld
sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysql.sock

then I changed the mysql.sh permission to execute.

samundra@samundra-apple:~$ chmod u+x mysql.sh

To execute use the following command:

samundra@samundra-apple:~$ ./mysql.sh

It will ask me my admins password and then will do the rest of the job of creating symbolic links and all.

15 thoughts on “[Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

  1. Jiban

    I am using shared hosting, so don’t have root access in my web host. How to solve this problem? Plz help.

    Reply
    1. Samundra Shrestha Post author

      Hi Jiban,

      See if you can access the console output by using some kind of tricks. Like using php exec for shell command executions and try to execute the given commands from that console.

      Reply
  2. auchomage

    Samudra, thank you very much for this. It was a huge help, I’m new to MySQL imports/exports and Linux in general. This saved me some serious grief. Thanks again for sharing

    Reply
    1. Samundra Shrestha

      1. Open the gedit or your favourite text editor
      then paste the contents similar to like this #!/bin/bash
      sudo rm -rf /var/run/mysqld
      sudo mkdir /var/run/mysqld
      sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysql.sock
      in file and save it as mysql.sh, then you change the permissions and to execute it you will use ./mysql.sh. These are in the last line of the article. Please read the last lines of the article. There I have shown how to execute the file.

      Reply
  3. Lolo782011

    i have this problem when i was to finish to install vicidial in ubuntu server 11.04

    plase help me..

    my email: lolo782011@yahoo.com

    ERROR 2002: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    Reply
    1. Samundra Post author

      Hello Lolo,

      This error usually occurs, when mysql is unable to connect through the socket. You will have to find the correct socket to use with mysql and then create sym link to the socket.

      The easiest would be to find the socket used by vicidial, may be read through the vicidial documentation and find the socket used by it. Then create a symlink to this file.

      Reply

Leave a Reply

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