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 successf开发者_运维百科ully 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.
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);
It works now after I changed SELinux option to Permissive; it was Enforce.
精彩评论