'Can't remotely connect to MySQL error (13) with PHP script, connecting through CLI works

I have looked at around 50 of the same question on here, but none of them seem to be fixing my problem. I can't remotely connect to my MySQL database through a php script, but!! connecting with mysql -u admin -p -h 172.16.2.93 works.

Here is the code I'm using

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

$dbhost = "172.16.2.93";
$dbuser = "admin";
$dbpass = "mariadb";
$dbname = "registration";

// connect to the database
$db = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname") or die(mysql_error());

The Error

And I'm getting the following error in /var/log/httpd/access_log when I access the website the script is connected to:

[:error] [pid 14797] [client 172.16.2.143:55201] PHP Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '172.16.2.93' (13) in /var/www/html/server.php on line 15

Things I've tried

  • Changing bind-address to bind-address=0.0.0.0 and #bind-address

  • Turning off all firewalls

  • Running the command setsebool httpd_can_network_connect=1

  • Disabling selunix in /etc/selinux/config

Edit: The problem seems to have been fixed. After creating the user remote@% with mysql_native_password I was able to access the database through the script.



Solution 1:[1]

Please check if the Mysql is responding from client on port 3306 and you have to mention the port in your mysqli_connect if it a different port ($host, $user, $password, $dbname, $port, $socket).

You must be missing the port param

Solution 2:[2]

setsebool -P httpd_can_network_connect=1

This command will fix the permissions problem for the current session.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Roldan