Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

When I tried to connect to the MySQL server through PHP, I got the following error: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

To give you some background, here is my situation. My Fedora Linux is a 64-bit machine, and I have installed a 32-bit version of XAMPP (i.e., 32-bit PHP, 32-bit MySQL) for the web server. Also, I have some PHP scripts that will be run in background. These scripts are run using 64-bit PHP. Both 32-bit and 64-bit PHP need to access the MySQL database. Long story short. I got the MySQL error message when I tried to execute using the 64-bit PHP, while the 32-bit PHP has no complain.

When PHP talks to MySQL, it needs to talk to the MySQL representative, i.e., mysql.sock. If it cannot talk to the mysql.sock, then PHP will be unable to access the MySQL database.

The reason why my 64-bit PHP could not talk to MySQL because of a wrong location of mysql.sock. To solve this problem, simply do the following:

First, let’s find out where is the PHP.ini of the 64-bit PHP:

sudo find / -name "php.ini"

In Fedora Linux, it is:

/etc/php.ini

Then, we need to find out where is the mysql.sock:

sudo find / -name "mysql.sock"

In my case, it is:

/opt/lampp/var/mysql/mysql.sock

Now, let’s tell PHP the location of the mysql.sock:

sudo nano /etc/php.ini

And change the following line from:

mysql.default_socket =

to:

mysql.default_socket = /opt/lampp/var/mysql/mysql.sock

Now, try to run your script again. The problem should be gone.

Have fun with MySQL.

–Derrick

Our sponsors:

1 comment

  1. This did not completely solve the issue in the case where there is mysql installed already on system and lampp is installed after that.
    To resolve this issue particularly either update the path or trick the os into using the lampp version of mysql. To do so, simply use

    mv /usr/bin/mysql /usr/bin/mysql.old

    ln -s /opt/lampp/bin/mysql mysql

Leave a Reply

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