This sample demonstrates how to use Cloud SQL - MySQL on Google App Engine Standard.
The sample code is located in java-docs-samples/cloud-sql/mysql/servlet
. This directory has the supplemental files needed to deploy to Google App Engine Standard with Java 11.
-
Create a 2nd Gen Cloud SQL Instance by following these instructions. Note the connection string, database user, and database password that you create.
-
Create a database for your application by following these instructions. Note the database name.
-
Create a service account with the 'Cloud SQL Client' permissions by following these instructions. Download a JSON key to use to authenticate your connection.
-
See Prerequisites.
-
Add the appengine-simple-jetty-main Main class to your classpath:
cd java-docs-samples/appengine-java11/appengine-simple-jetty-main
mvn install
- Move into the sample directory:
cd ../../cloud-sql/mysql/servlet/
- In the new Java 11 runtime, you must remove your
appengine-web.xml
file and create anapp.yaml
file to configure your application settings. Create ansrc/main/appengine
directory and copy theapp.yaml
provided:
mkdir src/main/appengine
cp ../../../appengine-java11/cloudsql/app.yaml src/main/appengine/
- Use the information from creating your database to replace the
environment variables in your
app.yaml
:
CLOUD_SQL_CONNECTION_NAME: '<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>'
DB_NAME: 'my_db'
DB_USER: 'my-db-user'
DB_PASS: 'my-db-pass'
- Java 11 has specific requirements on packaging your app. Replace the
pom.xml
with the Java 11pom.xml
:
cp ../../../appengine-java11/cloudsql/pom.xml ./
The following command will deploy the application to your Google Cloud project:
mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id>
View your application:
gcloud app browse
or by visiting https://<your-project-id>.appspot.com
.