Php cannot connect to mysql with error 13 (but on command line)

I have a strange situation on a newly installed server, and it seems that Google this time can not help me. I cannot connect to (remote) mysql from my php code. When I try to connect from the command line on the same server, the connection is succseds.

Failed to connect: unable to connect to MySQL Server on "MYSQL.SERVER" (13)

Here is the code and attempt to connect 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 in both mod_php and FastCGI mode, check that "/etc/php.d/mysql.ini" is displayed in the phpinfo () sections, as well as in the mysql, mysqli and pdo_mysql sections.

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

Edit: The problem was in SElinux

 setsebool -P httpd_can_network_connect_db=1 

There was a solution.

+52
php apache
Nov 02 '10 at 13:39
source share
3 answers
 setsebool -P httpd_can_network_connect=1 

will also be a useful CLI command for many people who visit this question to allow mysql_connet () connections from HTTP (Apache) requests to a remote MySQL database server, to enable network connections from httpd to SElinux, usually located in / etc / selinux / config (disabled by default so that hackers do not attack other machines using your httpd).

+112
Aug 30 '11 at 12:20
source share

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

 setsebool httpd_can_network_connect=1 
+26
Oct 08 '13 at 2:26
source share

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

This command fixes the permissions issue 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 

Thank you all for this question for saving me from the "permission denied" hell. :)

+2
Apr 24 '15 at 8:36
source share



All Articles