[英]Not able to connect to Google Cloud SQL with Slim framework on Google App Engine
I'm using Slim framework to build api for my application. 我正在使用Slim框架为我的应用程序构建api。 I'm not able to connect to Google Cloud SQL. 我无法连接到Google Cloud SQL。
I have provided the error message below. 我在下面提供了错误消息。
I have been using this in codeigniter without any problem. 我一直在codeigniter中使用它没有任何问题。 Can someone help me understand on how to fix this issue. 有人可以帮我理解如何解决这个问题。
DB Code: 数据库代码:
function getDB() {
$dbhost=":/cloudsql/projid:instancename";
$dbuser="root";
$dbpass="";
$dbname="dbname";
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
Error message 错误信息
{"error":{"text":SQLSTATE[HY000] [2002] php_network_getaddresses: gethostbyname failed. errno=0}}
PS I'm not facing this issue on Localhost. PS我在Localhost上没有遇到这个问题。
Your PDO
connection string should be using mysql:unix_socket
instead of mysql:host
. 你的PDO
连接字符串应该使用mysql:unix_socket
而不是mysql:host
。
https://cloud.google.com/appengine/docs/php/cloud-sql/ https://cloud.google.com/appengine/docs/php/cloud-sql/
PDO_MySQL PDO_MYSQL
$db = new pdo('mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=<database-name>',
'root', // username
'' // password
);
This is how I changed my code 这就是我改变代码的方式
From 从
function getDB() {
$dbhost=":/cloudsql/projid:instancename";
$dbuser="root";
$dbpass="";
$dbname="dbname";
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
To 至
function getDB() {
$dbConnection = new pdo('mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=<database-name>',
'root', // username
'' // password
);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.