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.