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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With