I’m using msqli_connect (…) to connect from my server (IP-a) to a Google Cloud SQL instance with Public IP (IP-g). All the GS firewall rules are in place to allow this, SSL is not used currently. However, I consistently get “Connection refused”. I cannot seem to find any further detail on this: I can’t see any relevant GC error/warning logging, and there appears to be no further client-side detail available.
Looking though the web and ChatGPT, I can see this a common problem (exacerbated but the lack of debug detail I think). However, I’ve not found any new insights into how to address this problem.
From my office PC (Unbuntu) I can run mysql with the relevant parameters and get a successful connection, where I can see and interrogate the target database. But I cannot seem to do this from a SSH on my server (IP-a).
Essential code:
<?php
....
$dbhost = [IP-g]
$dbport = 3306;
$dbname = [DB-name]
$dbuser = [DB-user]
$dbpass = [DB-pass]
//
echo "<p>About to get DB... host='$dbhost'</p>n";
//
mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//
$myaddr = $_SERVER['SERVER_ADDR'];
echo "<p>+++(ip=$myaddr): Got here: host = '$dbhost', user='$dbuser', pass='$dbpass', name='$dbase', port='$dbport'.</p>n";
//
try {
$dbid = mysqli_connect ($dbhost, $dbuser, $dbpass, $dbname, $dbport);
echo "<p>1:dbid = '<pre>" . print_r ($dbid, true) . "</pre>'</p>n";
}
catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
echo "<p>Get db failed.. mce = '" . mysqli_connect_error() . ".</p>n";
die ('bye');
}
// Success!
echo "<p>OK:dbid = '<pre>" . print_r ($this->dbid, true) . "</pre>'</p>n";
//
...
?>
OUTPUT:
About to get DB... host=[IP-g]
DB::Got here: host = '[IP-g]', name=[DB-name], user=[DB-user], pass=[DB-pass], port=]DB-port].
+++(ip=[IP-a]: Got here: host = [IP-g], user=[DB-user], pass=[DB-pass], name=[DB-name], port='3306'.
Message: Connection refused
Get db failed.. mce = 'Connection refused'.
bye