Skip to content

Commit

Permalink
Moving cloudsql sample to Cloud SQL proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott committed Mar 2, 2016
1 parent 6da2b63 commit 7fef68d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
41 changes: 32 additions & 9 deletions managed_vms/cloudsql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,47 @@ This sample demonstrates how to use [Google Cloud SQL](https://cloud.google.com/

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

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:
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:

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

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`.
1. Set the root password on your Cloud SQL instance:

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

4. Finally, run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.
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.

1. Download the [Cloud SQL Proxy](https://cloud.google.com/sql/docs/sql-proxy).

1. Run the proxy to allow connecting to your instance from your machine.

$ cloud_sql_proxy \
-dir /tmp/cloudsql \
-instances=YOUR_PROJECT_ID:us-central1:YOUR_INSTANCE_NAME=tcp:3306 \
-credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON

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:

$ mysql -h 127.0.0.1 -u root -p
mysql> create database YOUR_DATABASE;
mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';

1. Set the connection string environment variable. This allows the app to connect to your Cloud SQL instance through the proxy:

export SQLALCHEMY_DATABASE_URI=mysql+pymysql://USER:PASSWORD@127.0.0.1/YOUR_DATABASE

1. Run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.

1. Update the connection string in ``app.yaml`` with your configuration values. These values are used when the application is deployed.

## Running locally

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

You will need to set the following environment variables via your shell before running the sample:
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:

$ export SQLALCHEMY_DATABASE_URI=[your connection string]
$ python main.py
18 changes: 11 additions & 7 deletions managed_vms/cloudsql/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ runtime_config:

#[START env]
env_variables:
# Replace user, password, and host with the values obtained when
# configuring your Cloud SQL instance.
SQLALCHEMY_DATABASE_URI: mysql+pymysql://user:password@host/db
# If you are connecting over SSL, you can specify your certificates by
# using a connection string such as:
# > mysql+pymysql://user:password@host/db?
# ssl_key=client-key.pem?ssl_cert=client-cert.pem?ssl_ca=server-ca.pem
# Replace user, password, database, project, and instance with the values obtained
# when configuring your Cloud SQL instance.
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://USER:PASSWORD@/DATABASE?unix_socket=/cloudsql/PROJECT:us-central1:INSTANCE
#[END env]

#[START cloudsql_settings]
# Replace project and instance with the values obtained when configuring your
# Cloud SQL instance.
beta_settings:
cloud_sql_instances: PROJECT:us-central1:INSTANCE
#[END cloudslq_settings]
1 change: 1 addition & 0 deletions managed_vms/cloudsql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def is_ipv6(addr):
# [START example]
# Environment variables are defined in app.yaml.
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

Expand Down

0 comments on commit 7fef68d

Please sign in to comment.