Mark Lau
Mark Lau

Reputation: 157

MySQL connection error

I am trying to execute the following PHP script (called test1.php, say) on a web server running Apache. The script tries to connect to 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 kept receiving errors like this:

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

However, as root or normal user, I can execute the above script successfully from the shell:

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

Connected successfully.

I am confused and am grateful if anyone has any hints about this problem. Thanks so much.

Upvotes: 0

Views: 588

Answers (2)

Mark Lau
Mark Lau

Reputation: 157

It works now after I changed SELinux option to Permissive; it was Enforce.

Upvotes: 0

zerkms
zerkms

Reputation: 254886

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

After you get valid socket path (from CLI phpinfo) - you can specify the same to the php.ini, or right in your code:

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

Upvotes: 2

Related Questions