Cannot connect to google cloudsql from app engine
Asked Answered
P

1

8

I have been trying the past 2 days to connect to the cloudsql from the appengine, I am able to connect from a linux webserver via the IP, however cannot from the app engine with the same setup.

My connection string is as follows: please note the actual appid was replaced as well as 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 the 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 have tried with using a password, and not using a password. I have tried the username root, as username with a password and a username without.

I am using second generation setup, and flex php environment, the error I keep receiving is the following:

Short error

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

Full 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 am truly out of ideas now. Please note that both sql and app are in the same project.

Penal answered 14/5, 2017 at 14:21 Comment(2)
could you manage to resolve the issue ? I am getting the same one.Charmainecharmane
@IlkerBaltaci I never found a solution, and ended up using their VM/Compute engine rather.Penal
S
7

If you SSH into on of your instances (this will put it in debug mode) you can check for the existence of the socket file. You can open a connection from the Cloud Console.

Then,

ls /cloudsql

This directory should contain an item the named <project-id>:<region>:<instance-name>, following your MYSQL_DSN environment variable.

If it's not there, make sure that you've enabled the relevant APIs. In the documentation page Using Cloud SQL, follow the instructions under "Before you begin" -- specifically "Enable the API". (Your logs may also notify you about this: "Access Not Configured. Cloud SQL Administration API has not been used in project before or it is disabled.") Wait a few minutes after enabling the API before you redeploy.

You may have done this already, but in my experience it's a step that's easy to overlook.

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

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

Lastly, try putting the value of MYSQL_DSN in app.yaml file in quotes.

Semibreve answered 22/5, 2017 at 10:10 Comment(7)
Thank you for the answer, I tried ls /cloudsql with the following result: ls: cannot access /cloudsql: No such file or directory , I also made sure the API was enabled. As the end I just used a compute engine as I could not find a solution.Penal
I've updated my answer to include another possibility. Make sure that you have the beta_settings values set correctly.Semibreve
@Subash do you see the same error as the original poster, or a different one? If it's the same, check your logs for "Access Not Configured" messages.Semibreve
@subash try the quotes that i mentionedCharmainecharmane
unfortunately, i am getting the same error... i think it's because the /cloudsql directory is not available, even though i've enabled the API. does anyone know, how to solve this?Broadax
you may need to remove the instances to create a new one now with the api enabled.Bicknell
If the /cloudsql directory is not there, you may still need to create the directory and start the proxy to create the file socket, per these instructions. e.g. sudo mkdir /cloudsql; sudo chmod 777 /cloudsql , then wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy , chmod +x cloud_sql_proxy, ./cloud_sql_proxy -dir=/cloudsql &. Did that work for you?Av

© 2022 - 2024 — McMap. All rights reserved.