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)
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
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.
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.
Thanks a lot for this article. I was facing this issue for a while.
I am using shared hosting, so don’t have root access in my web host. How to solve this problem? Plz help.
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.
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
It’s my pleasure. 🙂
Thanks, its worked for me
Thank you so much! This saved me.
Thank you so much!!
You are most welcome.
Hey, thanks a lot, very simple solution – and it works!
Excellent!!!!! Works for me. Thanks!
you make my day! Thank you.
how to create .sh file ?
how to excute ?
what is the process?
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.
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)
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.