Search code examples
laravelgoogle-app-enginegoogle-cloud-sql

App Engine can't connect to gcloud sql database


I've been using Laravel with gcloud to create a website (App Engine) and database (SQL) but for some reason I can't get the App Engine to connect to the database.

The error I'm getting is "SQLSTATE[HY000] [2002] No such file or directory (SQL: select * fromusers)" from laravel's own debugging and "Failed to load resource: the server responded with a status of 500 ()" from the browser inspect window.

It works fine if I run a local instance since I've allowed my IP to access the SQL database. I can also see the data using MySQL workbench.

I've used the following tutorial. There is also exists one for Standard Environment but I didn't even get this far on that on. Using Laravel 5.5 worked a bit better.

Other things I've tried includes connecting the App Engine like it's an external app that's not in the same project as the database. https://cloud.google.com/sql/docs/mysql/connect-app-engine which also includes granting roles to service accounts: https://cloud.google.com/iam/docs/granting-roles-to-service-accounts

I've also done the following steps:

  1. Go to the IAM & admin Service accounts page.
  2. Select the project that has your App Engine application.
  3. Find App Engine default service account and copy the email address under it ([email protected]).
  4. Using the top-left project menu, return to the project that contains this SQL instance (project-name).
  5. Go to the IAM & admin IAM page.
  6. Click Add, enter the email address as the member, and choose Cloud SQL Client as the role. Then click Add.

    • App.yaml looks as follows
env: flex

runtime_config:
  document_root: public

# Ensure we skip ".env", which is only for local development
skip_files:
  - .env

env_variables:
  # Put production environment variables here.
  APP_LOG: errorlog
  APP_KEY: app-key
   ## Set these environment variables according to your CloudSQL configuration.

  DB_CONNECTION: mysql
  DB_HOST: sql-ip
  DB_PORT: 3306
  DB_DATABASE: database-name
  DB_USERNAME: db-username
  DB_PASSWORD: pass
  DB_SOCKET: /cloudsql/instance-connection:name
  beta_settings: 
  cloud_sql_instances: instance-connection:name```
  • I also use the following in composer.json to allow access to files and cache configs. 755 works similar and should be final.
    "chmod -R 777 bootstrap\/cache",
    "chmod -R 777 storage",
    "php artisan config:cache"
]

It's also worth noting that the bootstrap config file has had errors that makes it point to local files on my computer and not on the uploaded files.

If you need any more information, ask.

Thanks.


Solution

  • I got it working thanks to Samuel Romero comment. I followed the following "tutorial": https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql making my app.yaml file to look as follows

    runtime: php
    env: flex
    
    runtime_config:
      document_root: public
    
    # Ensure we skip ".env", which is only for local development
    skip_files:
      - .env
    
    env_variables:
      SQLALCHEMY_DATABASE_URI: >-
        mysql+pymysql://USERNAME:PASSWORD@/DATABASE?unix_socket=/INSTANCE_CONNECTION_NAME
      # Put production environment variables here.
      APP_LOG: errorlog
      APP_KEY: APPKEY
      ## Set these environment variables according to your CloudSQL configuration.
    
      DB_CONNECTION: mysql
      DB_HOST: localhost;unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME
      DB_DATABASE: DATABASE
      DB_USERNAME: USERNAME
      DB_PASSWORD: PASSWORD
    beta_settings:
      cloud_sql_instances: INSTANCE_CONNECTION_NAME
    

    Apart from that enabling the Cloud SQL API, which I should have had enabled but maybe got disabled by some reason. And then I also ran "gcloud auth application-default login" in the Cloud SDK.

    Hopefully this will help someone, and if there is a security issue with this please tell me.