Access Denied error while using PHP to connect to database in CentOS 7

68
March 16, 2019, at 04:00 AM

I am trying to connect to remote MySQL database at address 192.168.43.198. I am using CentOS 7 on both the nodes. my setup is as follows:

Database Node

IP: 192.168.43.198/24

have installed necessary packages for MySQL DB

Enabled necessary services and ports in firewall

Client Node

IP: 192.168.122.73/24

Configured it for hosting a website. Installed httpd, mysql and dependencies.

Enabled necessary services and ports in firewall

I have enabled remote login from mysql_secure_installation command and have fired several queries in mysql which grant every user every permission

grant usage on *.* to DBuser@'%' identified by '123456789';
grant all privileges on *.* to DBuser@'%' identified by '123456789';
flush privileges;

in Client node, when I run the command

mysql -h 192.168.42.198 -u DBuser -p
Enter password: 123456789

I can access the database.

But my client node is also a web-server and when I try to connect to database using php, it is giving permission denied error

/var/www/html/index.html file

<html>
 <head>
  <title>Node 01</title>
 </head>
 <body>
  <form action="index.php" method="get">
    <input type="submit" value="Open DB">
  </form>
 </body>
</html>

I just have one button which points to the index.php file.

/var/www/html/index.php file

$link = mysqli_connect("192.168.43.198", "DBuser", "123456789","demo");
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

echo 'Connected to the database.<br>';

database demo exists and I can manipulate it through command line as stated above and it works fine but when I click on the button in HTML, I am getting the error "ACCESS DENIED"

ERROR: Could not connect. Permission denied

I am using php 7.0 in Client Node here are the packages I installed in order to get php working

# rpm -qa | grep -i php
php-pdo-5.4.16-46.el7.x86_64
php-mcrypt-5.4.16-7.el7.x86_64
php-mysql-5.4.16-46.el7.x86_64
php-gd-5.4.16-46.el7.x86_64
php-ldap-5.4.16-46.el7.x86_64
php-5.4.16-46.el7.x86_64
php-common-5.4.16-46.el7.x86_64
php-cli-5.4.16-46.el7.x86_64
READ ALSO
How to fix file not found on Heroku?

How to fix file not found on Heroku?

I have a laravel project deployed in Heroku, in my Laravel project i have a php script i made to receive json, my php script is in public folder, when i use php artisan serve to serve the project, the script is accessible with no problem through the following...

55
How to set a shipping cost by item in Woocommerce

How to set a shipping cost by item in Woocommerce

In Woocommerce, when I add multiple products in cart, the shipping only applies to to 1 productHow should I change that to apply the cost to each item?

27
Show table with conditions from 2 input fields

Show table with conditions from 2 input fields

I'm having problems showing filtered table under 2 conditions from my database table

33