简体   繁体   English

无法使用Google App Engine上的Slim框架连接到Google Cloud SQL

[英]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.

相关问题 使用Google App Engine端点连接到Cloud SQL - Connect to Cloud SQL with Google App Engine Endpoint NodeJ:无法从Google App Engine连接Google Cloud SQL - NodeJs:Unable to connect Google Cloud SQL from Google App Engine 使用mysqli_connect连接到Google App Engine中的Cloud SQL - Connect to Cloud SQL in Google App Engine using mysqli_connect Google APP Engine 和云 sql :: 无法在 Google 云 sql(我的 sql)中连接 Spring boot 应用程序 - Google APP Engine and cloud sql :: Unable to connect Spring boot app in Google cloud sql (my sql) Google App Engine 上的 Django 连接到云 SQL,无法连接 - Django on Google App Engine connection to Cloud SQL, cannot connect Google App Engine-使用mysqli用php连接到SQL Cloud实例 - Google App Engine - Connect to SQL Cloud Instance with php using mysqli Google App Engine:如何使用Zend DB连接到Cloud SQL - Google App Engine: How to connect to Cloud SQL with Zend DB 无法从 App Engine Node JS 标准环境连接 Google Cloud MySQL 实例 - Not able to connect Google Cloud MySQL Instance from App Engine Node JS standard environment 无法使用Google App Engine中的SSL + Golang连接到Google Cloud SQL - Cannot connect to Google Cloud SQL using SSL + Golang from Google App Engine 无法使用ZF2中的Pdo_Mysql通过Google App Engine连接到Google Cloud SQL - Cannot connect to Google Cloud SQL through Google App Engine with Pdo_Mysql in ZF2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM