Connect to Cloud SQL in Google App Engine using mysqli_connect

a 夏天 提交于 2019-12-11 06:04:25

问题


I am trying to configure my Wordpress site in the Google App Engine standard environment. I have configured a Cloud SQL for MySQL Second Generation instance and can access it using Cloud SQL Proxy.

The problem I have is connected to the Cloud SQL instance after the app has been deployed to the Google App Engine (GAE) environment. Here are two different connection strings; one for the GAE environment and another for the local environment.

if (isset($_SERVER['GAE_ENV'])) {
    $dbConn = mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK); 
} else {  // local environment
    $dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
}

Local environment
The connection string for the local environment works perfectly when I use these 4 parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME. When I try to use the same connection string with four parameters in the GAE environment, it throws an error:

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known

GAE Environment
In this question, I was told to call mysqli_connect using all 6 of the optional parameters, with null for parameter 1 and 3306 for parameter 5, being the port. When I try to make the connection using the GAE connection string with these 6 parameters: null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK, I get a slightly different error:

Fatal error: Uncaught mysqli_sql_exception: No such file or directory

Here is the code from wp-config.php which sets the connection string variables depending on the environment:

if ($onGae) {
    /** GAE Environment */
    define('DB_NAME', 'database');
    define('DB_HOST', ':/cloudsql/project_id:region:instance_id');
    define('DB_USER', 'user');
    define('DB_PASSWORD', 'password');
} else {
    /** Local environment */
    define('DB_NAME', 'database');
    define('DB_HOST', '127.0.0.1');
    define('DB_USER', 'user');
    define('DB_PASSWORD', 'password');
}

No matter what I try, I can't seem to get mysqli_connect to connect to CloudSQL from GAE. Can someone please tell me what I am doing wrong? Thank you.


回答1:


I suspect the problem is the colon in the path of your socket is preventing it from recognizing it is an absolute path. Try define('DB_HOST', '/cloudsql/<project_id:region:instance_id>'); instead.

A couple of other tips: You can use the Cloud SQL proxy to create a local unix socket at /cloudsql/<CONNECTION_NAME>, which means you can test the same code deployed and locally.

Also as general programming advice, I would try to avoid using DB_HOST for different types of values (because they really aren't the same). This will help you from inadvertently using $DB_HOST somewhere else and expecting an IP address. Try something like this instead:

define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
if ($onGae) {
    /** GAE Environment */
    define('DB_HOST', null);
    define('DB_SOCKET', '/cloudsql/project_id:region:instance_id');
} else {
    /** Local environment */
    define('DB_HOST', '127.0.0.1');
    define('DB_SOCKET', null);
}


来源:https://stackoverflow.com/questions/57683958/connect-to-cloud-sql-in-google-app-engine-using-mysqli-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!