I opened every links google results had thrown to me in regards to this MySQL error. I have been spending hours to get this to work..

I have a web server (apache in centos installed in host B, and want to connect to a MySQL server in host A) both hosts are in the same subnet.

I can access remote mysql using CLI mysql -u root -h <ip> -p

but cannot connect to mysql using PHP function mysql_connect()

I have already disabled SELINUX and system-config-firewall-tui and included below syntax in /etc/my.cnf

  port =3306
  socket = /var/lib/mysql/mysql.sock

Some solutions found in the web tells me to type in the setbool associated with selinux, but the thing is, SELINUX is already disabled.

the odd thing is, I can connect thru CLI, but not in php function

also, I have php-mysql in both hosts

Here is my main.php

mysql_connect('','root','admin') or die (mysql_error());
echo "Connected to MySQL server";

mysql_select_db("web") or die (mysql_error());
echo "Connected to Database";

here is my /etc/my.cnf



selinux status

SELinux status: disabled

netstat -atn tcp 0 0* Listen

error received at client where apache is hosted:

[error] [client] PHP warning: mysql_connect(); cant connect to mysql server (13) in /var/www/html/main.php on line 2

  • 2
    13 is an OS error code for permission denied. SELinux is disabled on host B, right ? just checking...
    – melsayed
    Apr 29, 2013 at 10:15
  • Are you sure that the IP that you connect from in php and the one from the command line are the same?
    – NickW
    Apr 29, 2013 at 10:16
  • yes. i disabled SELINUX in host A and B just to be sure. Apr 29, 2013 at 10:16
  • @NickW yes im very sure of it sir. Apr 29, 2013 at 10:16
  • What does the log on the mysql server say? BTW, just an simple question, does the password you use have any non-alphanumeric characters?
    – NickW
    Apr 29, 2013 at 10:19

6 Answers 6


I could access from server B from command line mysql but I also could not connect my remote database via httpd - I know you say selinux was disabled on both sides, however in my situation, the httpd server side needed selinux

setsebool httpd_can_network_connect_db=1 

to enable apache to connect to the remote database.

  • You saved my day again! I keep forgetting this stupid property
    – Michel
    May 5, 2015 at 18:14
  • 1
    setsebool -P httpd_can_network_connect_db on to enable it permanatly (can take 20 secs to complete)
    – Michel
    May 6, 2015 at 20:27
  • This solution worked for me as well. In my case, ls -Z /var/www/html displayed a proper looking SELinux context, which is why I didn't think to use setsebool. Mar 19, 2017 at 18:11

do you have something like this in my.cnf ?

 bind-address            =

and in php do you have safe_mode or apache mod_security enabled?


Check your MySQL root user "host" setting. Perhaps try with anyhost, 192.168.8.%, localhost, or "www.domain.tld". Probably a good idea to test with another user rather than using root...


Try these steps and see if it changes anything:

  1. Edit /etc/my.cnf and change bind-address = to your Mysql server's ip.
  2. Allow root user to connect from any host


However, allowing remote root access is highly not recommend, you should create a new user and give it only the permissions it needs on a certain database and use that user.

Update: did not notice the post date at first, but lets hope this helps someone someday.


Use the below code. I too got the answer.

setsebool -P httpd_can_network_connect=1

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

setsebool httpd_can_network_connect=1
  • 1
    A single command is not enough, better if you explain, what it does.
    – peterh
    Mar 18, 2015 at 4:34
  • Check it out. Can't connect to mysql with error 13
    – Phoenix
    Mar 18, 2015 at 9:06
  • A single link is also not okay. As the years are going, link targets tend to disappear. And the SE (the company) needs qualified content to advice google to get more hits. But if you copy the essence of the link (some sentence is already enough) nobody will have any problem with that, and you will get upvotes.
    – peterh
    Mar 18, 2015 at 9:08
  • Looks you won't really understand why I am talking, so here is a downvote, maybe it is enough good argument.
    – peterh
    Mar 18, 2015 at 9:39

Try this it should work

  // Mysql settings
  $user   = "root";
  $password = "admin";
  $database = "IP";
  $host   = "yourhost";
  mysql_select_db($database) or die( "Unable to select database");
  • no, it didnt work. also made server restart after modifying. :( Apr 29, 2013 at 11:24
  • 1
    Note that putting everything in variables is not going to make any difference on the call itself. Aug 9, 2014 at 3:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .