Googolflex!!
  • Home
  • About
  • Contracting

Recent Posts

  • Sprint’s new “Simply ‘Almost’ Everything®” Plans
  • CSS Changes in Flex 4
  • Dotted Underline LinkButton (Flex)

About The Author : jwd

This is John Dusbabek's tech blog. John is a software engineer and Flex developer in Provo, UT, where he lives with his lovely wife and four sons.

Recent Comments

  • nodmonkey on PHP Warning: mysql_connect(): Can’t connect to MySQL server on… (13)
  • Can't connect to mysql with php/apache but can with cli | That-Matt on PHP Warning: mysql_connect(): Can’t connect to MySQL server on… (13)
Feb
02

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

By jwd

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.

Categories : Apache Web Server, HTTP Servers, MySQL, client/server

7 Comments

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

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
jwd
February 20th, 2010 at 11:20 am

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

3
Otto
March 3rd, 2010 at 8:14 am

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 2:45 pm

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 7:37 am

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

6
Can't connect to mysql with php/apache but can with cli | That-Matt
July 7th, 2010 at 3:00 pm

[...] this isn’t a permanent solution and won’t persist through a reboot without changing a config file. This can be accomplished by changing a line in /etc/selinux/config. Change the line that says: [...]

7
nodmonkey
July 22nd, 2010 at 7:55 am

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.

Leave a Comment

CAPTCHA Image Audio Version
Reload Image

Search

Feedburner

Subscribe to

Get the latest updates delivered via email

Calendar

July 2010
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

  • July 2010 (1)
  • June 2010 (2)
  • May 2010 (1)
  • February 2010 (11)
  • January 2010 (3)
  • December 2009 (5)
  • November 2009 (1)
  • August 2009 (8)
  • July 2009 (8)
  • May 2009 (4)
  • April 2009 (1)
  • March 2009 (6)
  • January 2009 (1)
  • November 2008 (4)
  • October 2008 (5)
  • September 2008 (1)
  • August 2008 (5)
  • July 2008 (1)
  • June 2008 (2)
  • May 2008 (8)
  • April 2008 (5)
  • March 2008 (2)
  • February 2008 (3)
  • January 2008 (1)
  • December 2007 (6)
  • November 2007 (9)
  • October 2007 (1)
  • September 2007 (2)

Categories

Tag Cloud

adobe apache Architecture book review C++ centos client server architecture Custom Components database Design error message fedora flash catalyst flex Flex 3 Flex 4 fms iis 6 Interaction Design load balancing master-master master-slave mod_proxy_balancer Monkey Patching MySQL no protocol p2p peer to peer Perl PHP Red5 regex replication self registration selinux Shell Scripting shortcut manager skins socket policy file sockets states stored procedures stratus tools workflow

Coworkers

  • Casey Jackman
  • Sean Murphy

Family

  • Emily & CJ
  • Family Blog
  • Gary Dusbabek

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

RSS FlexExamples

  • Setting the header height on a Spark Panel container in Flex 4
  • Adding a hover glow filter to an MX Image control in Flex 4
  • Fading an item renderer background fill on a Spark List control in Flex 4
  • Setting the border color on the MX Accordion container headers in Flex
  • Setting the tab width on an MX TabNavigator container in Flex 3

Spam Blocked

842 spam comments
blocked by
Akismet

Sponsored Links

JUICE Chat

BYU Adobe Users Group


Copyright © 2010 All Rights Reserved
Flexx Theme by iThemes
Powered by WordPress