RSS LinkedIn Twitter

PHP Warning: mysql_connect(): Can’t connect to MySQL server on… (13)

February 2nd, 2010 Categories: Apache Web Server, HTTP Servers, MySQL, client/server

I created some barebones Fedora servers that I’m intending to create a load balanced cluster from using Apache’s mod_proxy_balancer. My topology will eventually look like this:

load_balancer -> (ws1, ws2, ws3) -> mysql_server

As you can see, it’s nothing fancy. To test the balancer, each web server has a PHP script that connects to the MySQL database and inserts its hostname and IP address. Then I could run a simple query to determine whether the balancer was distributing the load according to my rules.

The PHP script was basic too:

$dbhost = "mysql.host.com";
$dbuser = "testuser";
$dbpass = "testpass";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());
$dbname = "testdb";
mysql_select_db($dbname);

$query = "INSERT INTO testtable(ip, host) VALUES('" . $_SERVER["SERVER_ADDR"] . "', '" . $_SERVER["SERVER_NAME"] . "');";

print $query;
mysql_query($query);
mysql_close($conn);

I’ve done this a thousand times, so you can imagine my frustration at getting this error message:

[Mon Feb 01 16:22:21 2010] [error] [client 192.168.1.1] PHP Warning:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to MySQL server on 'mysql.host.com' (13) in /var/www/html/index.php on line 7

I spent almost 2 hours looking for various solutions. I’ll list the most common ones in case you’re searching for a solution and mine doesn’t work for you:
–Ensure that MySQL user permissions are configured correctly.
–Ensure that MySQL is running on the server and on the correct port
–Ensure that selinux is not blocking the MySQL port or the mysqld process

These three items can be tested by simply logging into MySQL from a remote host using the following command:

mysql -u testuser -p -h mysql.host.com testdb

If that gives you a MySQL prompt, you can rule out the above three causes.

Some of the less obvious suggestions, which didn’t solve my problem either were:
–Ensure MySQL is using the correct path to the mysql.sock in my.ini
–Ensure that the server wasn’t started with –skip-networking

Though I tried a number of configuration changes for both MySQL, the server MySQL was running on (including disabling selinux completely), and PHP, I failed to overlook that selinux was blocking Apache’s outgoing connections to the MySQL database.

So what finally solved my problem was disabling selinux completely on the server Apache was running on. I should have done this in the first place, as I normally do with my Linux desktop installations.

This can be accomplished by changing a line in /etc/selinux/config. Change the line that says:

SELINUX=enforcing

to

SELINUX=disabled

If you do some searching you can find out how to add an exception for Apache, after 2 hours I didn’t feel like fussing with those.

Tags: , , , , ,

19 Responses to “PHP Warning: mysql_connect(): Can’t connect to MySQL server on… (13)”

  1. willomitzer
    February 20th, 2010 at 11:06
    1

    Hi there,

    had exactly the same problem.
    Found this website: http://www.beginlinux.com/server_training/web-server/976-apache-and-selinux

    I issued “setsebool -P httpd_can_network_connect 1″ as root and httpd was able to connect to mysql

    cheers
    thomas

  2. February 20th, 2010 at 11:20
    2

    Thanks, that was a very interesting post. I had no idea the relationship between Apache and selinux was so granular.

  3. March 3rd, 2010 at 08:14
    3

    That was really helpful.
    CentOS 5.4 and Lighttpd and Mysql on 2 servers… A true nightmare for that.
    Bests,
    Otto

  4. Mark
    May 12th, 2010 at 14:45
    4

    I had the same problem as above and following thomas’ instruction got it working. It’s nice to find a simple command line fix. Thanks guys!

    cheers
    Mark

  5. Rob
    May 25th, 2010 at 07:37
    5

    Having same issue…although I have SELINUX disabled….FC12 PHP5 APACHE2
    Keep getting mysql_connect() failures.

  6. nodmonkey
    July 22nd, 2010 at 07:55
    6

    Thank you SO much. This was also giving me trouble for the last few hours when trying to install the MindTouch wiki solution with a remote MySql database … so glad to have it resolved.

  7. September 26th, 2010 at 00:31
    7

    # setenforce 1

    # getsebool -a | grep ‘httpd’

    allow_httpd_anon_write –> off
    allow_httpd_bugzilla_script_anon_write –> off
    allow_httpd_cvs_script_anon_write –> off
    allow_httpd_mod_auth_pam –> off
    allow_httpd_nagios_script_anon_write –> off
    allow_httpd_prewikka_script_anon_write –> off
    allow_httpd_squid_script_anon_write –> off
    allow_httpd_sys_script_anon_write –> off
    httpd_builtin_scripting –> on
    httpd_can_network_connect –> off
    httpd_can_network_connect_db –> off
    httpd_can_network_relay –> off
    httpd_can_sendmail –> on
    httpd_disable_trans –> off
    httpd_enable_cgi –> on
    httpd_enable_ftp_server –> off
    httpd_enable_homedirs –> on
    httpd_rotatelogs_disable_trans –> off
    httpd_ssi_exec –> off
    httpd_suexec_disable_trans –> off
    httpd_tty_comm –> on
    httpd_unified –> on
    httpd_use_cifs –> off
    httpd_use_nfs –> off

    $ setsebool httpd_can_network_connect_db 1

    OK

  8. Scott Weston
    October 26th, 2010 at 17:58
    8

    Fantastic. Saved me lots of time. God bless you for giving me the command:

    setsebool -P httpd_can_network_connect 1 !!!

  9. March 28th, 2012 at 08:38
    9

    Thanks U Very much, Only these two commands,
    $ setsebool httpd_can_network_connect_db 1
    $ setsebool -P httpd_can_network_connect 1

    Helped me a lot :)

  10. Massimo Borgogno
    June 25th, 2012 at 02:57
    10

    GREAT WORK DUDE !!!

  11. Chaithanya
    September 19th, 2012 at 09:12
    11

    Saved my life.. ty

  12. Azmi
    November 16th, 2012 at 03:16
    12

    Marvelous bro…you have save me 2 hours…after disabled the selinux…voila…it works…

    Thanks again.

    Azmi
    Malaysia

  13. Nitin
    November 22nd, 2012 at 05:33
    13

    setsebool httpd_can_network_connect_db 1

    Helped me a lot.. thanx :)

  14. December 19th, 2012 at 05:35
    14

    Everything is very open with a very clear explanation of the
    issues. It was definitely informative. Your website is very
    useful. Thank you for sharing!

  15. January 8th, 2013 at 06:15
    15

    What’s up, I read your blog regularly. Your humoristic style is witty, keep doing what you’re doing!

  16. January 24th, 2013 at 22:27
    16

    nice job bro… tnkyu very much…

  17. Isaac
    February 11th, 2013 at 02:17
    17

    Thanks John, this problem was driving me nuts! Disabling SELINUX solved the problem :)

  18. Mert
    July 22nd, 2013 at 13:50
    18

    Thank you very much. “setsebool httpd_can_network_connect_db 1″ solved my problem.

Leave a Comment