通过托管在 Compute Engine 上的 PHP 连接到云端 SQL
Connect to Cloud SQL via PHP hosted on Compute Engine
如何通过托管在 Compute Engine VM 上的 php 文件连接到 Cloud SQL 实例?
• 我在云 SQL 中有一个名为 bd-sales
的 MySQL(第 1º 代)实例和一个名为 sales
的数据库
• 我有一个 Linux(Debian) VM 内置于 Compute Engine
Obs.: 我想使用 PDO
建立连接
我试过了,但没用:
<?php
function getConnection(){
try {
$connection = new PDO('mysql:host=999.999.999.99;dbname=sales', 'user', 'pass');
$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');
?>
我解决了问题,更改连接字符串:
<?php
function getConnection(){
try { {GCSQL IPv4} {instance name} {Database name}
$connection = new PDO('mysql:host=999.999.999.99;unix_socket=/cloudsql/instance:bd-instance;charset=utf8;dbname=vendas', 'user', 'pass');
$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');
?>
如何通过托管在 Compute Engine VM 上的 php 文件连接到 Cloud SQL 实例?
• 我在云 SQL 中有一个名为 bd-sales
的 MySQL(第 1º 代)实例和一个名为 sales
• 我有一个 Linux(Debian) VM 内置于 Compute Engine
Obs.: 我想使用 PDO
建立连接我试过了,但没用:
<?php
function getConnection(){
try {
$connection = new PDO('mysql:host=999.999.999.99;dbname=sales', 'user', 'pass');
$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');
?>
我解决了问题,更改连接字符串:
<?php
function getConnection(){
try { {GCSQL IPv4} {instance name} {Database name}
$connection = new PDO('mysql:host=999.999.999.99;unix_socket=/cloudsql/instance:bd-instance;charset=utf8;dbname=vendas', 'user', 'pass');
$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');
?>