php can't connect to mysql with error 13 (but command line can)

PhpApache

Php Problem Overview


I have weird situation in newly installed server, and it seems that Google can't help me this time. I can't connect to (remote) mysql from my php-code. When I try to connect from command line on the same server the connection succseds.

> Could not connect: Can't connect to > MySQL server on 'MYSQL.SERVER' (13)

Here is the code and the connect attempt from the command line

[u1@bosko httpdocs]$ cat  test.php

<?
$link = mysql_connect('MYSQL.SERVER', 'testusersimon', '123456');
if (!$link) {
    die('Could not connect: ' .  mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

[u1@bosko httpdocs]$ mysql -h MYSQL.SERVER -utestusersimon --password=123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 352108
Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

I tried running the php script both in mod_php mode and in FastCGI, check that "/etc/php.d/mysql.ini" shows up in the phpinfo() as well as mysql,mysqli and pdo_mysql sections.

but the result was the same, I know its something simple but I just can't . Please help :)

Edit: The problem was with SElinux

setsebool -P httpd_can_network_connect_db=1

Was the solution.

Php Solutions


Solution 1 - Php

setsebool -P httpd_can_network_connect=1

will also be a helpful CLI command to many people visiting this question, as to allow mysql_connet() connections from within HTTP (Apache) requests to a remote MySQL database server, ensure to enable Network Connections from httpd in SElinux usually located in /etc/selinux/config (disabled by default to prevent hackers from attacking other machines using your httpd).

Solution 2 - Php

On CentOs 6, you can use the following (without -P)

setsebool httpd_can_network_connect=1

Solution 3 - Php

On Fedora 21 with apache 2/httpd version 2.6 using php version 5.6 when connecting to a remote mysql server 5.6 or mariadb version 10. It even seems to be a problem connecting to local server when specifying the server's FQDN instead of localhost in the php code.

This command will fix the permissions problem for the current session:

setsebool httpd_can_network_connect_db on

To make the fix permanent for subsequent reboots you need to do this:

setsebool -P httpd_can_network_connect_db on

Thanks to all on this question for rescuing me from "permission denied" hell. :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimSimYView Question on Stackoverflow
Solution 1 - PhpPallieter KoopmansView Answer on Stackoverflow
Solution 2 - PhpMax ChernopolskyView Answer on Stackoverflow
Solution 3 - PhpGrant RostigView Answer on Stackoverflow