How do I connect to a Cloud SQL instance through a php file hosted on a Compute Engine VM?
• I have a MySQL instance in Cloud SQL called bd-vendas
, and I created a database named vendas
in it.
• I have a Linux Debian VM built on Compute Engine.
Note: I would like to make the connection using PDO
I tried this but it did not work:
<?php
function getConnection(){
try {
$connection = new PDO('mysql:host=999.999.999.99;dbname=vendas', 'usuario', 'senha');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $connection;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
return;
}
}
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
?>