I'm attempting to execute the next PHP script (known as test1.php, say) on the web server running Apache. The script attempts to connect with a MySQL server:


$myHost = "localhost";

$myUser = "anyone";

$myPwd = "1234";

$link = mysql_connect($myHost,$myUser,$myPwd);

if (!$link) {

die(mysql_errno().": ".mysql_error()."\n");


echo "Connected successfully.\n";


I stored receiving errors such as this:

2002: Can't connect with local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)

However, as root or normal user, I'm able to execute the above mentioned script effectively in the spend:

shell>php /var/www/html/test1.php

Connected effectively.

I'm confused and am grateful if anybody has any hints relating to this problem. Thanks a lot.

Execute <?php phpinfo(); both under apache and CLI and compare mysql.default_socket value.

Once you get valid socket path (from CLI phpinfo) - you are able to specify exactly the same towards the php.ini, or in your code:

$link = mysql_connect($myHost . ':/tmp/mysql.sock', $myUser, $myPwd);

Interesting very fast reply. Really appreciate.

In the CLI, I acquired:

mysql.default_socket => /var/lib/mysql/mysql.sock => /var/lib/mysql/mysql.sock

From Apache, I acquired:

mysql.default_socket    /var/lib/mysql/mysql.sock   /var/lib/mysql/mysql.sock

In php.ini, I additionally have a similar factor:

mysql.default_socket = '/var/lib/mysql/mysql.sock'

Finally, I additionally transformed the script towards the following:

$link = mysql_connect($myHost.':/var/lib/mysql/mysql.sock',$myUser,$myPwd);

However I still got exactly the same error:

2002: Could not connect: Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) 

It really works now once i transformed SELinux choice to Permissive it had been Enforce.