[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


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

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.

Samundra Shrestha

I work as fullstack developer. Having spent more than 5 years in the web development technologies, I aspire to create an environment where we can share, learn and grow together.

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

  • July 2, 2014 at 6:25 AM

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

    • July 4, 2014 at 8:44 AM

      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.

  • March 9, 2014 at 5:37 PM

    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

  • February 3, 2014 at 1:05 PM

    Thanks, its worked for me

  • January 15, 2014 at 8:39 AM

    Thank you so much! This saved me.

  • April 21, 2013 at 1:11 PM

    Hey, thanks a lot, very simple solution – and it works!

  • April 15, 2013 at 4:57 AM

    Excellent!!!!! Works for me. Thanks!

  • March 28, 2013 at 1:44 PM

    you make my day! Thank you.

  • May 31, 2012 at 6:24 AM

    how to create .sh file ?
    how to excute ?
    what is the process?

    • May 31, 2012 at 6:40 AM

      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.

  • October 20, 2011 at 11:33 PM

    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)

    • October 21, 2011 at 9:19 AM

      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.


Leave a Reply

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