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.
For information on pricing and restrictions imposed by both Cloud SQL and App Engine, see Pricing and Access Limits.
Before you begin
-
Create or select a Cloud Platform project in the Cloud Platform Console
and then ensure that project includes an App Engine application:
Go
to App Engine
The Dashboard opens if an App Engine application already exists in your project. Otherwise, you are prompted to choose the region where you want your App Engine application located.
- To deploy a Java app to App Engine, you must first setup your environment, see Using Apache Maven and the App Engine Plugin for details.
Configuring your local environment
You can either use a local MySQL server to test your application or you can connect to Cloud SQL.
If you want to test your application with a local MySQL server, install it now:-
If you use Linux on a distribution with
apt-get, you can run:sudo apt-get install mysql-server
- For other operating systems, see the MySQL Community Server download page.
Setting up the Cloud SQL instance
- Create a Second Generation Cloud SQL instance and configure the root user.
- If you don't want to use the root user to connect, create a user.
- Using the Cloud SDK, get the Cloud SQL instance connection name
to use as a connection string in your application code:
gcloud sql instances describe [INSTANCE_NAME]
Record the value returned forconnectionName. You can also find this value in the Instance details page of the Google Cloud Platform Console. For example, in the Cloud SDK output:gcloud sql instances describe instance1 connectionName: project1:us-central1:instance1
-
Create a database on your Cloud SQL instance named
sqldemo.For more information, see Creating a database.
Granting access to App Engine
If your App Engine application and Cloud SQL instance are in different Google Cloud Platform projects, you must use a service account to allow your App Engine application access to Cloud SQL.
This service account represents your App Engine application and is created by default when you create a Google Cloud Platform project.
- If your App Engine application is in the same project as your Cloud SQL instance, proceed to Setting up. Otherwise, proceed to the next step.
-
Identify the service account associated with your App Engine
application. The default App Engine service account is named
[PROJECT-ID]@appspot.gserviceaccount.com.You can verify the App Engine service account on the IAM Permissions page. Ensure that you select the project for your App Engine application, not your Cloud SQL instance.
- Go to the IAM & Admin Projects page in the Google Cloud Platform Console.
- Select the project that contains the Cloud SQL instance.
- Search for the service account name.
- If the service account is already there with the
Cloud SQL ClientorEditorrole, you can proceed to Setting up. - Otherwise, add the service account by clicking Add.
- In the Add members dialog, provide the name of the service account
and select Cloud SQL > Cloud SQL Client for the role.
Alternatively, you can use the primitive Editor role by selecting Roles > Editor, but the Editor role includes permissions across Google Cloud Platform.
- Click Add.
You should now see the service account listed with the specified role.
Setting up
-
Add the Cloud SQL instance connection name, database, user, and password to
pom.xml. -
Add a JDBC library to your application. For example, if you use Maven, you
can add the dependency to the project's
pom.xml:Java 7
Java 8
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:
The properties set in the maven pom are used to create a connection URL below:
Testing in your development environment
To test your app with the local development server:
- If you are using a local MySQL server, start the MySQL server in your development environment.
- Start the development server. For example, if you use Maven:
mvn appengine:devserver
- The web server is now running and listening for requests on port 8080. To view, visit the following URL:
Something go wrong? See Using the Local Development Server for more information.
Deploying your app
-
To upload your app to App Engine, run the following Maven commands:
mvn clean package mvn appengine:updateFor details about deploying to App Engine, see Deploying a Java App.
-
Open your browser and view the app at
http://[YOUR_PROJECT_ID].appspot.com.