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 ::
<br></br>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<br></br>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<br></br>samundra 8370 0.0 0.0 4160 856 pts/0 S+ 16:01 0:00 grep --color=auto mysqld<br></br>

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.

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

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
<br></br>#!/bin/bash<br></br>sudo rm -rf /var/run/mysqld<br></br>sudo mkdir /var/run/mysqld<br></br>sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysql.sock<br></br>

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.