-2

I need to connect to a remote MySQL server with PHP but I get the following error:

Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on ... (13) in /var/www/html/index.php on line 16

Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/html/index.php on line 17

Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/html/index.php on line 18

The remote server firewal is open, user has permission to connect from any Host, and I'm able to connect from the local server with mysql command line, but not able to connect with PHP.

What may be the problem here?

EDIT: Already tried with different connection code, but always same error.

This is the latest one:

$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS msg");
$row = $result->fetch_assoc();
echo $row['msg'];
miken32
  • 42,008
  • 16
  • 111
  • 154
BeoWulf
  • 627
  • 2
  • 6
  • 18
  • 1
    How should we tell? You're not sharing any of your connection code – baao Feb 07 '17 at 17:13
  • Edited to add the connection code. Anyway already tried different codes and didn't work. This seems to be some setting somewhere but couldn't find anything on php.ini that could affect remote MySQL connections. – BeoWulf Feb 07 '17 at 17:20
  • Debug it with http://php.net/manual/en/mysqli.connect-error.php glhf. – ʰᵈˑ Feb 07 '17 at 17:21
  • Do you have access to the terminal? Try running `mysql -hHOST -uUSER -p` replacing host with the IP of the remote mysql host, and USER with your username for the database. – Hydra IO Feb 07 '17 at 17:21
  • 1
    Try adding some code to check the connection: `if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); }` – Andy Feb 07 '17 at 17:21
  • @HydraIO, already tried to connect to the same server from the command line and it works fine. – BeoWulf Feb 07 '17 at 17:22
  • @AndyC, Already did. Output: Can't connect to MySQL server on '(server IP)' (13) – BeoWulf Feb 07 '17 at 17:27
  • Seesm to be an SELinux issue. Solved executing: setsebool -P httpd_can_network_connect=1 – BeoWulf Feb 07 '17 at 17:32

2 Answers2

4

Found this to be an issue with SELinux that is not allowing httpd network connections.

Executing:

setsebool -P httpd_can_network_connect=1

Solved the problem.

BeoWulf
  • 627
  • 2
  • 6
  • 18
0

Make sure you inserted information In the connection info. Replace DB_DATABASE and other keywords with your database info in single quotes.

$mysqli = new mysqli('localhost', 'MyUsername', ''mypassword', 'database1');
George Jones
  • 245
  • 2
  • 11