Skip to content

Commit 6f3c788

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #213 from GoogleCloudPlatform/cloudsql-proxy
Moving cloudsql sample to Cloud SQL proxy
2 parents f0a425f + 1cb9882 commit 6f3c788

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

managed_vms/cloudsql/README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,47 @@ This sample demonstrates how to use [Google Cloud SQL](https://cloud.google.com/
66

77
Before you can run or deploy the sample, you will need to do the following:
88

9-
1. Create a Cloud SQL instance. You can do this from the [Google Developers Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command:
9+
1. Create a [Second Generation Cloud SQL](https://cloud.google.com/sql/docs/create-instance) instance. You can do this from the [Cloud Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command:
1010

11-
$ gcloud sql instances create [your-instance-name] \
12-
--assign-ip \
13-
--authorized-networks 0.0.0.0/0 \
14-
--tier D0
11+
$ gcloud sql instances create YOUR_INSTANCE_NAME \
12+
--activation-policy=ALWAYS \
13+
--tier=db-n1-standard-1
1514

16-
2. Create a new user and database for the application. The easiest way to do this is via the [Google Developers Console](https://console.developers.google.com/project/_/sql/instances/example-instance2/access-control/users). Alternatively, you can use MySQL tools such as the command line client or workbench, but you will need to set a root password for your database using `gcloud sql instances set-root-password`.
15+
1. Set the root password on your Cloud SQL instance:
1716

18-
3. Update the connection string in ``app.yaml`` with your instance values.
17+
$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
1918

20-
4. Finally, run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.
19+
1. Create a [Service Account](https://cloud.google.com/sql/docs/external#createServiceAccount) for your project. You'll use this service account to connect to your Cloud SQL instance locally.
20+
21+
1. Download the [Cloud SQL Proxy](https://cloud.google.com/sql/docs/sql-proxy).
22+
23+
1. Run the proxy to allow connecting to your instance from your machine.
24+
25+
$ cloud_sql_proxy \
26+
-dir /tmp/cloudsql \
27+
-instances=YOUR_PROJECT_ID:us-central1:YOUR_INSTANCE_NAME=tcp:3306 \
28+
-credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON
29+
30+
1. Use the MySQL command line tools (or a management tool of your choice) to create a [new user](https://cloud.google.com/sql/docs/create-user) and [database](https://cloud.google.com/sql/docs/create-database) for your application:
31+
32+
$ mysql -h 127.0.0.1 -u root -p
33+
mysql> create database YOUR_DATABASE;
34+
mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
35+
mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';
36+
37+
1. Set the connection string environment variable. This allows the app to connect to your Cloud SQL instance through the proxy:
38+
39+
export SQLALCHEMY_DATABASE_URI=mysql+pymysql://USER:PASSWORD@127.0.0.1/YOUR_DATABASE
40+
41+
1. Run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.
42+
43+
1. Update the connection string in ``app.yaml`` with your configuration values. These values are used when the application is deployed.
2144

2245
## Running locally
2346

2447
Refer to the [top-level README](../README.md) for instructions on running and deploying.
2548

26-
You will need to set the following environment variables via your shell before running the sample:
49+
It's recommended to follow the instructions above to run the Cloud SQL proxy. You will need to set the following environment variables via your shell before running the sample:
2750

2851
$ export SQLALCHEMY_DATABASE_URI=[your connection string]
2952
$ python main.py

managed_vms/cloudsql/app.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ runtime_config:
77

88
#[START env]
99
env_variables:
10-
# Replace user, password, and host with the values obtained when
11-
# configuring your Cloud SQL instance.
12-
SQLALCHEMY_DATABASE_URI: mysql+pymysql://user:password@host/db
13-
# If you are connecting over SSL, you can specify your certificates by
14-
# using a connection string such as:
15-
# > mysql+pymysql://user:password@host/db?
16-
# ssl_key=client-key.pem?ssl_cert=client-cert.pem?ssl_ca=server-ca.pem
10+
# Replace user, password, database, project, and instance with the values obtained
11+
# when configuring your Cloud SQL instance.
12+
SQLALCHEMY_DATABASE_URI: >-
13+
mysql+pymysql://USER:PASSWORD@/DATABASE?unix_socket=/cloudsql/PROJECT:us-central1:INSTANCE
1714
#[END env]
15+
16+
#[START cloudsql_settings]
17+
# Replace project and instance with the values obtained when configuring your
18+
# Cloud SQL instance.
19+
beta_settings:
20+
cloud_sql_instances: PROJECT:us-central1:INSTANCE
21+
#[END cloudslq_settings]

managed_vms/cloudsql/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def is_ipv6(addr):
3535
# [START example]
3636
# Environment variables are defined in app.yaml.
3737
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
38+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
3839

3940
db = SQLAlchemy(app)
4041

0 commit comments

Comments
 (0)