Failed to connect to google cloudql from app

I have been trying for the last two days to connect to cloudql from appengine, I can connect from linux webserver over IP, but cannot from application engine with the same setup.

My connection string looks like this, note that the actual app has been replaced and also the dbname

in app.yaml

env_variables:
  MYSQL_DSN: mysql:unix_socket=/cloudsql/appid:us-central1:sql-instance-1;dbname=dbname
  MYSQL_USER: root
  MYSQL_PASSWORD: ''

      

In phpfile

$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
if (!isset($dsn, $user) || false === $password) {
    throw new Exception('Set MYSQL_DSN, MYSQL_USER, and MYSQL_PASSWORD environment variables');
}
$conn = new PDO($dsn, $user, $password);

      

The above was obtained from https://cloud.google.com/appengine/docs/standard/php/cloud-sql/ I've tried using a password rather than using a password. I tried the username root as username with password and username without.

I am using a second generation install and flex php environment, the error I am getting is the following:

Short error

SQLSTATE[HY000] [2002] No such file or directory

      

Complete error

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /app/dbconn.php:12 Stack trace: #0 /app/dbconn.php(12): PDO->__construct('mysql:unix_sock...', 'root', '') #1 /app/api.user.php(2): include_once('/app/dbconn.php') #2 {main} thrown in /app/dbconn.php on line 12

      

I'm really out of ideas now. Please note that both sql and application are in the same project.

+3


source to share


1 answer


If you are using SSH in your instances (this will be in debug mode), you can check for the existence of the socket file. You can open a connection to the cloud console.

Then

ls /cloudsql

      

This directory should contain a named item <project-id>:<region>:<instance-name>

following your environment variable MYSQL_DSN

.

If not, make sure you have the appropriate APIs enabled . From the documentation page Using Cloud SQL , follow the instructions in the Before You Begin section - specifically, Enable the API. (Your logs may also notify you of this: "Access Not Configured. The Cloud SQL Administration API was not previously used in the project or is disabled.") Wait a few minutes after enabling the API before redistributing .



You may have already done this, but in my experience this is a step that is easy to spot.

Also, make sure your app.yaml file contains the following :

beta_settings:
    cloud_sql_instances: "<project-id>:<region>:<instance-name>"

      

Finally, try putting quotes around the value in your app.yaml file . MYSQL_DSN

+4


source







All Articles