Viewed   147 times

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';

[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

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.


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).

Saturday, November 5, 2022

As was determined in the comments above, this was caused by an environmental difference - the HOME env var was set differently inside the executed process. Using proc_open instead of simple exec gave more precise control over said process and explicitly setting that env var solved the issue.

Saturday, October 8, 2022

You can make a .htaccess file and enter Options -Indexes this will disable listing of the files in the directory.

If you also need the traffic to originate from your site you will need to make a file say... index.php with code that checks $_SERVER['HTTP_REFERER'] to see if the traffic originates from your site.


Oh I forgot you can actually fix it all in the .htaccess:

Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^*$ [NC]
RewriteRule ^.* /403-page [L,R]

This will do all the work of the script I suggested, so you won't need it anymore.

Friday, August 19, 2022

From your edit, the issue appears to be a scoping one.

You're attempting to make a connection inside a function where the $username, $password and $database variables are defined outside that function.

I suggest you read the Variable Scope section of the manual and turn up your error reporting as @netcoder suggests in the question comments.

Sunday, December 4, 2022
$sql = new mysqli('','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');

It will work. Just remove port from localhost (

Tuesday, August 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :