Using Google Cloud SQL

Python |Java |PHP |Go

Google Cloud SQL provides a relational database that you can use with your App Engine application. Cloud SQL is a MySQL database that lives in Google's cloud. To learn more about Google Cloud SQL, see the Google Cloud SQL documentation.

You can connect to Cloud SQL First Generation or Second Generation instances.

For information on pricing and restrictions imposed by both Cloud SQL and App Engine, see Pricing and Access Limits.

  1. Before you begin
  2. Setting up
  3. Code sample overview
  4. Testing and deploying

Before you begin

  • Select or create a Cloud Platform Console project.

    Go to the Projects page

  • To test in your development environment, you need to connect to a local MySQL server. Install the MySQL Community Server.

    If you use Linux on a distribution with apt-get, you can run:

    sudo apt-get install mysql-server

Setting up

  1. In the same project as your App Engine application, create a Cloud SQL instance and configure the root user.

    You can use a Cloud SQL First Generation or a Second Generation instance. For more information about Cloud SQL Second Generation, see Second Generation Capabilities. Note that Cloud SQL Second Generation is in beta.

  2. For the Cloud SQL instance, create a database.
  3. The default user for App Engine applications is root@localhost. If you don't want to use the default user, create a user.
  4. If you are using a Cloud SQL First Generation instance, in the Cloud Platform Console, grant your App Engine application access to the Google Cloud SQL instance.

    If you are using a Cloud SQL Second Generation instance and your application is in the same project as the SQL instance, then your application should already have access.

  5. Get the Cloud SQL instance connection name to use as a connection string in your application code. To find the instance connection name, go to the Cloud SQL Instances page in the Cloud Platform Console.
    Go to the Cloud SQL Instances page
    1. Click on the Instance ID for the Cloud SQL instance you are using.
    2. Under Properties, find the Instance connection name.
  6. Add the Cloud SQL instance connection name, database name, user, and password to appengine-web.xml.
  7. Add a JDBC library to your application. For example, if you use Maven, you can add the dependency to the project's pom.xml:

Code sample overview

The following code sample creates a visitor log in a Cloud SQL instance. It writes visit information to Cloud SQL and then reads and returns the last ten visits:

Testing and deploying

The development server in the Google App Engine SDK can use a locally installed MySQL server instance to closely mirror the Google Cloud SQL environment during development. To test in your development environment:
  1. Update the connection string in your application code to connect to a local instance of MySQL server.
  2. Start the MySQL server in your development environment.
  3. Start the development server. For example, if you use Maven:

    mvn appengine:devserver

  4. The web server is now running and listening for requests on port 8080. To view, visit the following URL:


  5. When you are ready, build and deploy your project to App Engine. For example, if you are using Maven:

    mvn clean install
    mvn appengine:update

Send feedback about...