|
1 | 1 | # Ruby Cloud SQL sample on Google App Engine flexible environment |
2 | 2 |
|
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]. |
5 | 5 |
|
6 | 6 | ## Setup |
7 | 7 |
|
8 | 8 | Before you can run or deploy the sample, you will need to do the following: |
9 | 9 |
|
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: |
13 | 13 |
|
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. |
18 | 59 |
|
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. |
23 | 60 | 1. Update the values in in `app.yaml` with your instance configuration. |
| 61 | + |
24 | 62 | 1. Finally, run `create_tables.rb` to ensure that the database is properly |
25 | 63 | configured and to create the tables needed for the sample. |
26 | 64 |
|
27 | 65 | ## Running locally |
28 | 66 |
|
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. |
31 | 68 |
|
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: |
34 | 72 |
|
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" |
39 | 77 | bundle install |
40 | 78 | bundle exec ruby create_tables.rb |
41 | 79 | 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 |
0 commit comments