Connect to Cloud SQL for MySQL from App Engine flexible environment
Learn how to deploy a sample app on App Engine flexible environment connected to a MySQL instance by using the Google Cloud console and a client app.
Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud APIs necessary to run a Cloud SQL sample app on App Engine flexible environment.
Console
Click the button below to enable the APIs required for this quickstart.
This enables the following APIs:
- Cloud SQL Admin API
- Compute Engine API
- Cloud Build API
- Cloud Logging API
By clicking the button, you also enable the Google App Engine Flexible Environment.
gcloud
Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the
gcloudcommands presented throughout this quickstart.Run the following
gcloudcommand using Cloud Shell:gcloud services enable appengineflex.googleapis.com sqladmin.googleapis.com \ compute.googleapis.com cloudbuild.googleapis.com logging.googleapis.com
This command enables the following APIs:
- Cloud SQL Admin API
- Compute Engine API
- Cloud Build API
- Cloud Logging API
By running the above command, you also enable the Google App Engine Flexible Environment.
Set up Cloud SQL
Create a Cloud SQL instance
Create a database
Take the following steps to create a database:
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select
quickstart-instance. - From the SQL navigation menu, select Databases.
- Click Create database.
- In the Database name field of the Create a database dialog,
enter
quickstart-db. Leave the values for the character set and collation. - Click Create.
gcloud
Run the gcloud
sql databases create command to create a database.
gcloud sql databases create quickstart-db --instance=quickstart-instance
Create a user
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select the quickstart-instance MySQL instance that you created.
- From the SQL navigation menu, select Users.
- Click Add user account.
- In the Add a user account to instance quickstart-instance page,
add the following information:
- In the User name field, enter
quickstart-user. - In the Password field, specify a password for your database user. Make a note of this password for use in a later step of this quickstart.
- In the User name field, enter
- Click Add.
gcloud
Before running the following command, make the following replacements:
- PASSWORD with a password for your database user. Make a note of this for use in a later step of this quickstart.
Run the gcloud sql users create command to create the user.
gcloud sql users create quickstart-user \ --instance=quickstart-instance \ --password=PASSWORD
Username length limits are the same for Cloud SQL as for on-premises MySQL; 32 characters for MySQL 8.0 and later, 16 characters for earlier versions.
Deploy a sample app to App Engine flexible environment
Create an App Engine app
Console
-
In the Google Cloud console, go to the App Engine page.
- Click Create application.
- From the Select a region drop-down menu, select us-central.
- Click Next.
- After the Get started page appears, scroll to the bottom of the page.
- Click the I'll do this later link.
gcloud
-
Run the following
gcloud app createcommand to create an App Engine app: - When prompted to choose the region where you want your App Engine app located, enter the numeric choice for us-central.
gcloud app create
Configure the App Engine service account
Console
-
In the Google Cloud console, go to the Service accounts page.
- For the service account named App Engine default service account, click the pencil icon.
- Click Add another role.
- Add the Role named Cloud SQL Client.
- Click Save.
gcloud
-
Run the following
gcloudcommand to get a list of your project's service accounts:gcloud iam service-accounts list
- Copy the EMAIL of the App Engine service account.
- Run the following command to add the Cloud SQL Client
role to App Engine service account:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \ --role="roles/cloudsql.client"
Configure and deploy a sample app for Cloud SQL
Now that you created a Cloud SQL instance and database, and you configured the App Engine service account so that it has permissions to connect to Cloud SQL, you can configure and deploy a sample app to connect to your Cloud SQL instance.