Connecting Symfony app to Google Cloud SQL (App Engine Deployment)

Viewed 299

Problem :

Can't establish a PDO connection from my symfony App, deployed on Google App Engine, to a Google Cloud SQL database

Setup :

Followed this tutorial, which does seem a little outdated, on deploying a Symfony/PHP App to App Engine:

1. Cloud SQL Database

MySQL instance, server_version=5.7, public ip_address

2. Symfony

Version 5.2

config/packages/framework.yaml

framework:
  secret: "%env(APP_SECRET)%"

  session:
    enabled: true
    handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
    cookie_secure: auto
    cookie_samesite: lax
    gc_maxlifetime: 36000

  php_errors:
    log: true

config/services.yaml

services:
  # ...

  Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
    arguments:
      # - "%env(DATABASE_URL)%"
      # you can also use PDO configuration, but requires passing two arguments
      - "mysql:dbname=[INSERT_DB_NAME]; host=[INSERT_HOST]; port=[INSERT_PORT]"
      - { db_username: [INSERT_USER], db_password: [INSERT_PW] }

config/packages/doctrine.yaml

doctrine:
  dbal:
    url: "%env(resolve:DATABASE_URL)%"
...

3. App Engine

app.yaml

runtime: php74

env_variables:
  APP_ENV: prod
  APP_SECRET: [INSERT_APP_SECRET]
  DATABASE_URL: mysql://root:DB_PASSWORD@localhost?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME;dbname=symfonydb

handlers:
 ...

Different Configuration & Errors :

  1. Choosing to use the ENV_DATABASE_URL or the PDO_configuration_arguments in config/services.yaml doesn't seem to affect the outcome. Neither does adding or not the port 3306.
  2. I'm connecting with the root user of the Google Cloud MySQL Instance with a password.

1. localhost

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

  at vendor/symfony/http-foundation/Session/Storage/Handler/PdoSessionHandler.php:443
  at PDO->__construct('mysql:dbname=[INSERT_DB_NAME]; host=localhost;', 'root', '[INSERT_PW]', array())

2. 127.0.0.1

PDOException:
SQLSTATE[HY000] [2002] Connection refused

  at vendor/symfony/http-foundation/Session/Storage/Handler/PdoSessionHandler.php:443
  at PDO->__construct('mysql:dbname=[INSERT_DB_NAME]; host=127.0.0.1;', 'root', '[INSERT_PW]', array())

3. Cloud SQL Public IP

PDOException:
SQLSTATE[HY000] [2002] Connection timed out

  at vendor/symfony/http-foundation/Session/Storage/Handler/PdoSessionHandler.php:443
  at PDO->__construct('mysql:dbname=[INSERT_DB_NAME]; host=[INSERT_PUBLIC_IP]; port=3306', 'root', '[INSERT_PW]', array())

Update

After updating packages to symfony 5.3, it magically started working. But I'm not totally convinced this was the actual solution

0 Answers
Related