Google Cloud Platform
Cloud SQL

Connecting to Google Cloud SQL from Google App Engine

The method for setting up a connection between an App Engine application and a Cloud SQL instance depends on the types of the App Engine environment and Cloud SQL instance:

  1. App Engine standard environment to a Cloud SQL First Generation instance
  2. App Engine standard environment to a Cloud SQL Second Generation instance
  3. App Engine flexible environment to a Cloud SQL Second Generation instance

App Engine standard environment to a First Generation instance

You connect from App Engine standard environment to First Generation instances by completing the following process:

Granting an App Engine standard environment application access to a First Generation instance

To grant access to a First Generation instance for an application running on App Engine standard environment, you authorize the application ID on the Cloud SQL instance. The application ID is the project ID for the project that contains the application.

Console

  1. Go to the Instances page in the Google Cloud Platform Console.
  2. Click the instance name.
  3. Select Access Control > Authorization.
  4. In the Authorized App Engine applications section, click Add item and enter the application ID.
  5. Save your change by clicking the edit icon Save or edit..
  6. Click the Save button to update the instance.
  7. Update your application.

gcloud

  1. Install the Cloud SQL command line if you haven't already (see Managing Instances Using the Cloud SDK).
  2. List the existing authorized App Engine applications for the instance.
    gcloud sql instances describe YOUR_INSTANCE_NAME
    
  3. Use the sql instances patch command to provide the App Engine application gae-app-name names for all App Engine applications that can access this instance. Use commas to separate multiple application names.
    gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-gae-apps GAE_APP_NAME
    
  4. Update your application.

Update your App Engine standard environment application to connect to a First Generation instance

  1. Go to the Instances page in the Google Cloud Platform Console.
  2. Click the instance name.
  3. Record the Instance connection name from the Instance properties.
  4. Use the following connection string in your application:
    /cloudsql/[INSTANCE_CONNECTION_NAME]
    

    The complete code statement depends on your language and framework. For more details about connecting, see the appropriate language-specific link below.

Java App Engine Applications

Python App Engine Applications

PHP App Engine Applications

Go App Engine Applications

App Engine standard environment to a Second Generation instance

You connect from Google App Engine standard environment) to Second Generation instances by completing the following steps:

Granting an App Engine standard environment application access to a Second Generation instance

If your application is in a different project than your Second Generation instance, grant your application access:

  1. If your Google App Engine application is in the same project as your Second Generation instance, you are done; no extra authentication steps are required.
  2. Identify the default App Engine service account for the application's project.
    The default App Engine service account is named 'project-id@appspot.gserviceaccount.com'.
  3. Go to the Google Cloud Platform Console Permissions pane.
  4. Select the project that contains the Cloud SQL instance.
  5. Open the list of accounts with the Editor privilege.
  6. If the service account is listed there, you are done with this task.
  7. If the service account is not shown in the list of accounts with the Editor privilege, add it by clicking Add member.
  8. In the Add members dialog, provide the name of the service account and select Editor for the role.
  9. Click Add and confirm that the service account is now listed with the project members with Editor privileges.
  10. Update your application.

Updating your App Engine standard environment application

Update your application:
  1. Go to the Instances page in the Google Cloud Platform Console.
  2. Click the instance name.
  3. Record the Instance connection name from the Instance properties.
  4. In your application code, use the following string to connect to the instance:
    /cloudsql/[INSTANCE_CONNECTION_NAME]
    

The complete code statement you use depends on your language and framework. For more details about connection strings, see the appropriate language-specific link below.

Java App Engine Applications

Python App Engine Applications

PHP App Engine Applications

Go App Engine Applications

App Engine flexible environment to a Second Generation instance

You connect from Google App Engine flexible environment to Second Generation instances by completing the following process:

Granting an App Engine flexible environment application access to a Second Generation instance

Grant your application access:

  1. Enable the SQL Admin API, if you haven't already.
  2. If your Google App Engine flexible environment application is in the same project as your Second Generation instance, you are done with this task; you do not need to create a service account.
  3. Otherwise, identify the service account associated with the App Engine application.
    The service account for the App Engine application is usually 'app-name@appspot.gserviceaccount.com'.
  4. Go to the Google Cloud Platform Console Permissions pane.
  5. Select the project that contains the Cloud SQL instance.
  6. Open the list of accounts with the Editor privilege.
  7. If the application's service account is listed there, you are done with this task.
  8. If the application's service account is not shown in the list of accounts with the Editor privilege, add it by clicking Add members.
  9. In the Add members dialog, provide the name of the application's service account and select Editor for the role.
  10. Click Add and confirm that the service account is now listed with the project members with Editor privileges.
  11. Update your application.

Updating your App Engine flexible environment application

Update your App Engine flexible environment application:
  1. Go to the Instances page in the Google Cloud Platform Console.
  2. Click the instance name.
  3. Record the Instance connection name from the Instance properties.
  4. Add the following key to your app.yaml runtime configuration file :
    beta_settings:
          cloud_sql_instances: [INSTANCE_CONNECTION_NAME]
    
    You can include multiple instance names, separated by commas.
  5. In your application code, connect to the instance by opening a socket with the following name:
    /cloudsql/[INSTANCE_CONNECTION_NAME}
    

The complete code statement you use depends on your language and framework. For more details about connection strings, see the appropriate language-specific link below.

Java App Engine Applications

Python App Engine Applications

Go App Engine Applications

What's next