简体   繁体   中英

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.

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

setsebool -P httpd_can_network_connect=1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM