-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'];
8
  • 1
    How should we tell? You're not sharing any of your connection code Commented Feb 7, 2017 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. Commented Feb 7, 2017 at 17:20
  • Debug it with php.net/manual/en/mysqli.connect-error.php glhf. Commented Feb 7, 2017 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. Commented Feb 7, 2017 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(); } Commented Feb 7, 2017 at 17:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.