HostOnNet Blog

MySQL Error: Can’t connect through socket /var/tmp/mysql.sock

On a Cpanel/WHM server running RHEL, after rebooting MySQL stoped working for all sites hosted on the server. I tried restarting MySQL, but it won’t solved the problem. Sites still shows the error message

Warning: mysql_pconnect(): Can’t connect to local MySQL server through socket ‘/var/tmp/mysql.sock’ (2) in /home/flashhos/public_html/index.php on line 9
Problem with dB connection!

The problem is, MySQL could not find the socket at /var/tmp/mysql.sock

So i locked the orginal location of socket with

# locate mysql.sock

Found two files. So i go to /var/tmp/ and created a sym link to /var/lib/mysql/mysql.sock

# cd /var/tmp
# ln -s /var/lib/mysql/mysql.sock

After doing this, restarted MySQL server

service mysql stop
service mysql start

Now everything working fine.

Posted in Database. Bookmark the permalink.

  • vel

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

    I got a problem like this . how to fix this error . i am using doctrine

  • Login to mysql server, run

    show variables like '%socket%';

    Check if you get something like

    mysql> show variables like '%socket%';
    | Variable_name | Value                     |
    | socket        | /var/lib/mysql/mysql.sock |
    1 row in set (0.00 sec)

    If path is different, you need to set a symlink.