0
votes
 <?php
 $servername = "192.168.179.130";
 $username = "root";
 $password = "";
 $dbname = "rawcdr";
 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

The above code gives a warning Warning: mysqli::mysqli(): (HY000/2002) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\new_table\index.php on line 59.

The host is a different server (CentOS 7), and i have checked its reach ability. I have also provided special privilege in mysql server by:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%' IDENTIFIED BY PASSWORD '';

Also started the firewall service by:

service firewalld start

Checked the port 3306 is open by following command:

netstat -nlp | grep 3306
2
"The host is a different server, and i have checked its reachability." How? And what were the results? - Oldskool
@ManinderpreetSingh I dont think that Dup link is actually very relevant in this situation as its talking about localhost - RiggsFolly

2 Answers

0
votes

Of course the first thing to check is that the firewall on the remote machine is not blocking access on port 3306.

But:

The root user account on a MYSQL Server is by default configured so that this account can only be used from the machine running the MySQL Server instance. So by default you cannot use root to access the server remotely.

And that is as it should be. It is afterall the SuperUser account.

EG

`root`@`localhost`

So you are going to have to create a new user on the remote MYSQL Server instance that is allowed to connect from either any remote host dangerous or a specific remote host (your PC's ip address). This new account should also be setup to be allowed access with the required privilages on the database you are trying to access.

CREATE USER 'ayax'@'192.168.1.200' IDENTIFIED BY 'new_password' 

Or if its 5.7 server dont forget the new password expire

CREATE USER 'ayax'@'your-ip-address' IDENTIFIED BY 'new_password' 
    PASSWORD EXPIRE NEVER;

If your ip address is allocated by a DHCP server and can change from day to day something like this would be more flexible

CREATE USER 'ayax'@'192.168.1.%' IDENTIFIED BY 'new_password' 
-2
votes

Try this :

$conn = mysqli_connect('192.168.179.130', 'my_user', 'my_password', 'my_db');

if (!$conn) {
  die('Connect Error (' . mysqli_connect_errno() . ') '
          . mysqli_connect_error());
}