Skip to content

Commit d82fac7

Browse files
author
remi Taylor
authored
Merge pull request #36 from GoogleCloudPlatform/cloud-sql-v2
Update Cloud SQL sample to use Cloud SQL second generation
2 parents bc03749 + 5a84473 commit d82fac7

5 files changed

Lines changed: 93 additions & 37 deletions

File tree

appengine/cloudsql/README.md

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,90 @@
11
# Ruby Cloud SQL sample on Google App Engine flexible environment
22

3-
This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/)
4-
on [Google App Engine flexible environment](https://cloud.google.com/appengine/docs/flexible/).
3+
This sample demonstrates how to use [Google Cloud SQL][sql] (or any other SQL
4+
server) on [Google App Engine flexible environment][flexible].
55

66
## Setup
77

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

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

14-
gcloud sql instances create [your-instance-name] \
15-
--assign-ip \
16-
--authorized-networks 0.0.0.0/0 \
17-
--tier D0
14+
gcloud sql instances create [YOUR_INSTANCE_NAME] \
15+
--activation-policy=ALWAYS \
16+
--tier=db-n1-standard-1
17+
18+
where `[YOUR_INSTANCE_NAME]` is a name of your choice.
19+
20+
1. Set the root password on your Cloud SQL instance:
21+
22+
gcloud sql instances set-root-password [YOUR_INSTANCE_NAME] --password [YOUR_INSTANCE_ROOT_PASSWORD]
23+
24+
where `[YOUR_INSTANCE_NAME]` is the name you chose in step 1 and
25+
`[YOUR_INSTANCE_ROOT_PASSWORD]` is a password of your choice.
26+
27+
1. Create a [Service Account][service] for your project. You will use this
28+
service account to connect to your Cloud SQL instance locally.
29+
30+
1. Download and install the [Cloud SQL Proxy][proxy].
31+
32+
1. [Start the proxy][start] to allow connecting to your instance from your local
33+
machine:
34+
35+
cloud_sql_proxy \
36+
-dir /cloudsql \
37+
-instances=[YOUR_INSTANCE_CONNECTION_NAME] \
38+
-credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON
39+
40+
where `[YOUR_INSTANCE_CONNECTION_NAME]` is the connection name of your
41+
instance on its Overview page in the Google Cloud Platform Console, or use
42+
`[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`.
43+
44+
1. Use the MySQL command line tools (or a management tool of your choice) to
45+
create a [new user][user] and [database][database] for your application:
46+
47+
mysql --socket [YOUR_SOCKET_PATH] -u root -p
48+
mysql> create database YOUR_DATABASE;
49+
mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
50+
mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';
51+
52+
where `[YOUR_SOCKET_PATH]` is that socket opened by the proxy. This path was
53+
printed to the console when you started the proxy, and is of the format:
54+
`/[DIR]/[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`.
55+
56+
1. Set the `MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_SOCKET_PATH`, and
57+
`MYSQL_DATABASE` environment variables. This allows the app to connect to your
58+
Cloud SQL instance through the proxy.
1859

19-
1. Create a new user and database for the application. The easiest way to do
20-
this is via the [Google Developers Console](https://console.developers.google.com/project/_/sql/instances/example-instance2/access-control/users).
21-
Alternatively, you can use MySQL tools such as the command line client or
22-
workbench.
2360
1. Update the values in in `app.yaml` with your instance configuration.
61+
2462
1. Finally, run `create_tables.rb` to ensure that the database is properly
2563
configured and to create the tables needed for the sample.
2664

2765
## Running locally
2866

29-
Refer to the [appengine/README.md](../README.md) file for instructions on
30-
running and deploying.
67+
Refer to the [top-level README](../README.md) for instructions on running and deploying.
3168

32-
To run locally, set the environment variables via your shell before running the
33-
sample:
69+
It's recommended to follow the instructions above to run the Cloud SQL proxy.
70+
You will need to set the following environment variables via your shell before
71+
running the sample:
3472

35-
export MYSQL_HOST=<your-cloudsql-host>
36-
export MYSQL_USER=<your-cloudsql-user>
37-
export MYSQL_PASSWORD=<your-cloudsql-password>
38-
export MYSQL_DATABASE=<your-cloudsql-database>
73+
export MYSQL_USER="YOUR_USER"
74+
export MYSQL_PASSWORD="YOUR_PASSWORD"
75+
export MYSQL_SOCKET_PATH="YOUR_SOCKET_PATH"
76+
export MYSQL_DATABASE="YOUR_DATABASE"
3977
bundle install
4078
bundle exec ruby create_tables.rb
4179
bundle exec ruby app.rb
80+
81+
[sql]: https://cloud.google.com/sql/
82+
[flexible]: https://cloud.google.com/appengine
83+
[gen]: https://cloud.google.com/sql/docs/create-instance
84+
[console]: https://console.developers.google.com
85+
[sdk]: https://cloud.google.com/sdk
86+
[service]: https://cloud.google.com/sql/docs/external#createServiceAccount
87+
[proxy]: https://cloud.google.com/sql/docs/external#install
88+
[start]: https://cloud.google.com/sql/docs/external#6_start_the_proxy
89+
[user]: https://cloud.google.com/sql/docs/create-user
90+
[database]: https://cloud.google.com/sql/docs/create-database

appengine/cloudsql/app.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
require "sinatra"
1818
require "sequel"
1919

20-
DB = Sequel.mysql2 host: ENV["MYSQL_HOST"],
21-
user: ENV["MYSQL_USER"],
20+
DB = Sequel.mysql2 user: ENV["MYSQL_USER"],
2221
password: ENV["MYSQL_PASSWORD"],
23-
database: ENV["MYSQL_DATABASE"]
22+
database: ENV["MYSQL_DATABASE"],
23+
socket: ENV["MYSQL_SOCKET"]
2424

2525
get "/" do
2626
# Save visit in database

appengine/cloudsql/app.yaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ entrypoint: bundle exec ruby app.rb -p $PORT
1818

1919
# [START env_variables]
2020
env_variables:
21-
# Replace user, password, and host with the values obtained when
22-
# configuring your Cloud SQL instance.
23-
MYSQL_HOST: <your-cloudsql-host>
24-
MYSQL_USER: <your-cloudsql-user>
25-
MYSQL_PASSWORD: <your-cloudsql-password>
26-
MYSQL_DATABASE: <your-cloudsql-database>
21+
MYSQL_USER: [YOUR_USER]
22+
MYSQL_PASSWORD: [YOUR_PASSWORD]
23+
MYSQL_DATABASE: [YOUR_DATABASE]
24+
# This path was printed to the console when you started the proxy, and is of
25+
# the format: `/[DIR]/[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`
26+
MYSQL_SOCKET_PATH: [YOUR_SOCKET_PATH]
2727
# [END env_variables]
28+
29+
# [START cloudsql_settings]
30+
beta_settings:
31+
# The connection name of your instance on its Overview page in the Google
32+
# Cloud Platform Console, or use `[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`
33+
cloud_sql_instances: [YOUR_INSTANCE_CONNECTION_NAME]
34+
# [END cloudsql_settings]

appengine/cloudsql/create_tables.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# [START all]
1616
require "sequel"
1717

18-
DB = Sequel.mysql2 host: ENV["MYSQL_HOST"],
19-
user: ENV["MYSQL_USER"],
18+
DB = Sequel.mysql2 user: ENV["MYSQL_USER"],
2019
password: ENV["MYSQL_PASSWORD"],
21-
database: ENV["MYSQL_DATABASE"]
20+
database: ENV["MYSQL_DATABASE"],
21+
socket: ENV["MYSQL_SOCKET"]
2222

2323
DB.create_table :visits do
2424
primary_key :id

appengine/cloudsql/spec/cloudsql_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
app_yaml = File.expand_path("../../app.yaml", __FILE__)
2525

2626
configuration = File.read(app_yaml)
27-
configuration.sub! "<your-cloudsql-host>", ENV["MYSQL_HOST"]
28-
configuration.sub! "<your-cloudsql-user>", ENV["MYSQL_USER"]
29-
configuration.sub! "<your-cloudsql-password>", ENV["MYSQL_PASSWORD"]
30-
configuration.sub! "<your-cloudsql-database>", ENV["MYSQL_DATABASE"]
27+
configuration.sub! "[YOUR_USER]", ENV["MYSQL_USER"]
28+
configuration.sub! "[YOUR_PASSWORD]", ENV["MYSQL_PASSWORD"]
29+
configuration.sub! "[YOUR_DATABASE]", ENV["MYSQL_DATABASE"]
30+
configuration.sub! "[YOUR_SOCKET_PATH]", ENV["MYSQL_SOCKET_PATH"]
3131

3232
File.write(app_yaml, configuration)
3333

0 commit comments

Comments
 (0)