Connecting from App Engine

This page provides language-specific links to informaton about how to set up a connection from an App Engine application and a Second Generation instance. It also describes how to set up a connection between an App Engine standard environment application and a First Generation instance.

For information about the various options for connecting to Cloud SQL, see Connection Options for External Applications.

General information for connecting to Cloud SQL from App Engine

When you are connecting to Cloud SQL from App Engine, keep these best practices in mind:

  • App Engine has the ability to automatically create more instances when load increases. If the number of App Engine instances increases beyond what the Cloud SQL instance is sized to handle, your Cloud SQL instance will not be able to meet the demand and could even lose SLA coverage. You can avoid this issue by setting the max_instances value for the basic_scaling element in your app.yaml file. For more information, see Scaling elements.
  • Open database connections consume resources; your application should always close connections when they are no longer being used. For App Engine standard environment, there is a hard limit on the number of connections an App Engine instance can have open to Cloud SQL. If your application requires more open connections, consider using connection pooling.

App Engine standard environment to Second Generation

For information about connecting from App Engine standard environment to Second Generation instances, see the App Engine documentation for your programming language:
Language Documentation
Python Using Google Cloud SQL with Python
Java Using Google Cloud SQL with Java
PHP Using Google Cloud SQL with PHP
Go Using Google Cloud SQL with Go

Need help? For help troubleshooting the proxy, see Troubleshooting Cloud SQL Proxy connections. Or, see our Cloud SQL Support page.

App Engine flexible environment to Second Generation

For information about connecting from App Engine flexible environment to Second Generation instances, see the App Engine documentation for your programming language:
Language Documentation
Python Using Cloud SQL for MySQL with Python
Java Using Cloud SQL for MySQL with Java
Node.js Using Cloud SQL for MySQL with Node.js
Go Using Cloud SQL for MySQL with Go
Ruby Using Cloud SQL for MySQL with Ruby
PHP Using Cloud SQL for MySQL with PHP
.NET Using Cloud SQL for MySQL with .NET

Need help? For help troubleshooting the proxy, see Troubleshooting Cloud SQL Proxy connections. Or, see our Cloud SQL Support page.

App Engine standard environment to First Generation

  1. Grant the application access:

    Console

    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name to open its Instance details page.
    3. Select the Authorization tab.
    4. In the Authorized App Engine Standard applications section, click Add project ID and enter the project ID for the application you want to authorize.
    5. Click Done to exit edit mode.
    6. Click Save to update the instance.

    gcloud

    1. List the existing authorized App Engine projects for the instance.
      gcloud sql instances describe [INSTANCE_NAME]
      
    2. Use the sql instances patch command to provide the project ID for all applications that can access this instance. Use commas to separate multiple IDs.
      gcloud sql instances patch [INSTANCE_NAME] --authorized-gae-apps [GAE_APP_PROJECT_ID]
      
  2. Update your application:
    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name to open its Instance details page, and copy the Instance connection name.
    3. Use the following connection string in your application:
      /cloudsql/<INSTANCE_CONNECTION_NAME>
      

      The complete code statement depends on your language and framework.

Need help? For help troubleshooting the proxy, see Troubleshooting Cloud SQL Proxy connections. Or, see our Cloud SQL Support page.

What's next

Send feedback about...

Cloud SQL for MySQL