简体   繁体   中英

MySQL - Connection failed: No route to host

Confused why getting this issue while trying to connect to my DB.

Connection failed: No route to host

<?php
$servername = "HOST";
$username = "USERNAME";
$password = "PASSWORD";
$database = "DBNAME";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

// echo $_SERVER['SERVER_NAME'];
?>

Can anyone provide a solution to connect?

Your $servername should be localhost not HOST

Change $servername = "HOST"; to $servername = "localhost";

If localhost doesn't work, contact your web host to ask what their particular host variable is the correct IP address to use. localhost or 127.0.0.1 are common variables.

Also, as Adrenaxus pointed out, be sure to provide the actual username, password and database name in your variables.

Happy Coding

1) Check the mysql service that it is running.

2) Check the firewall that the mysql port is open.

3) If the database is located on a remote hosting check that the connection to your database can be opened.

4) If there is a router between the database, check the port forwarding.

Declare $PORT = '<yoursqlport>' Then, replace $conn

$conn = new mysqli($servername, $username, $password, $database, $PORT);

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