I have a wordpress website hosted on AWS Lightsail. On that website, I would like to use a shortcode to retrieve data from a database using mysqli. However, everytime the script that connects to the database is executed, I receive a connection error. The database is on the same AWS Lightsail instance. I was wondering if I need to establish an SSH-tunnel because this is also needed if I connect to phpmyadmin. But since the script is executed on the server itself this does not really makes sense in my understanding. I have used the exact same code on a local host and it worked perfectly fine. What am I missing here?
//Params to connect to a database
$dbHost = "*static ip of wordpress site*";
$dbUser = "root";
$dbPassword = "*password*";
$dbName = "*database name*";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset("utf8mb4");
} catch(Exception $e) {
error_log($e->getMessage());
exit('Error connecting to database');
}
$dbHost = "*static ip of wordpress site*";
- shouldn't that be localhost?