1

I am trying to connect cloud sql with google cloud app engine flexible.

My code is as bellow:

$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
try {
    $db = new PDO($dsn, $user, $password);
    $statement = $db->prepare("SELECT * from user");
    $statement->execute();
    $users = $statement->fetchAll();
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

app.yaml

runtime: php
env: flex

runtime_config:
    document_root: .

env_variables:
    MYSQL_DSN: mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME
    MYSQL_USER: username
    MYSQL_PASSWORD: password

Getting bellow error

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

parth
  • 111
  • 1
  • 2
  • 3
  • Consider using Private IP connectivity: https://cloud.google.com/blog/products/databases/introducing-private-networking-connection-for-cloud-sql It's easier to understand/debug. – Vadim Oct 19 '18 at 17:50

2 Answers2

2

If someone is still struggling with this issue, don't forget to use quotation marks around cloud_sql_instances:"CONNECTION_NAME" and also don't forget to use quotation marks around any environment variables in your app.yaml.

Additionally make sure that you have both Cloud SQL and Cloud SQL Admin API enabled (normally only the first one is enabled in your Google Cloud Console).

0

Try adding this in app.yaml

# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
    cloud_sql_instances: "CONNECTION_NAME"
ArmeniaH
  • 101