With the following code am able to connect to my SQL instance from localhost environment , but when i upload the same code to my VM instance and try to connect it from there am getting "connection failed: connection timed out"
Note: I added VM instance's External IP address in Authorized network section.
<?php
$servername = "SQL INSTANCE IP ADDRESS : port";
$username = "root";
$password = "password";
$dbname="appdb";
$response=array();
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Please help, Thanks.
If you have your PHP outside of GCP (compute VM, App Engine, etc) You can either Install the cloud proxy in your app server and your app would connect like if the DB was in localhost, or you can use the public IP settings (I would suggest using the Proxy this way you don't have to use attach a public IP to your SQL instance)
If the PHP app is hosted in GCP you may follow the steps for the service where your app is