1
votes

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
Just as something to try...change the DB_HOST to 'localhost' in the GAE environment as well and confirm that you get the same error message (eliminating some easy to knock out things). - Gabe Weiss
Thanks Gabe. I tried your suggestion to change the DB_HOST to localhost, but when I did that I get an 'Error establishing a database connection'. This makes sense because without specifying the ':/cloudsql/project_id:region:instance_id' it won't know which database to connect to. I assume you were telling me to change the DB_HOST entry in the wp-config.php file and not in the mysqli_connect call, right? - DanielAttard
No that's good. I was wanting to confirm that the "file missing" wasn't the socket for some reason. That error usually means the database isn't available (not Cloud SQL now, we're talking the MySQL db under it). I'm on the move today, I'll see if I can poke someone to take a look too. - Gabe Weiss

1 Answers

2
votes

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);
}