|
1 | | -# Node.js Cloud SQL sample on Google App Engine |
| 1 | +# Cloud SQL for MySQL Node.js sample on App Engine flexible environment |
2 | 2 |
|
3 | | -This sample demonstrates how to use [Google Cloud SQL][sql] (or any other SQL |
4 | | -server) on [Google App Engine Flexible][flexible]. |
| 3 | +This sample demonstrates how to use [Google Cloud SQL][sql] for |
| 4 | +[MySQL][mysql] on [Google App Engine Flexible][flexible]. |
5 | 5 |
|
6 | | -This sample has instructions for both [MySQL][mysql] and [Postgres][postgres]. |
7 | | - |
8 | | -## Setup |
9 | | - |
10 | | -### General steps |
11 | | -Before you can run or deploy the sample, you will need to do the following: |
12 | | - |
13 | | -1. In order for some of the commands below to work, you need to enable the |
14 | | -[Cloud SQL Admin API](https://console.cloud.google.com/apis/api/sqladmin-json.googleapis.com/overview). |
15 | | -1. Create a [Second Generation Cloud SQL][gen] instance. You can do this from |
16 | | -the [Cloud Console][console] or via the [Cloud SDK][sdk]. To create it via the |
17 | | -SDK use the following command: |
18 | | - |
19 | | - gcloud sql instances create [YOUR_INSTANCE_NAME] \ |
20 | | - --activation-policy=ALWAYS \ |
21 | | - --tier=db-n1-standard-1 |
22 | | - |
23 | | - where `[YOUR_INSTANCE_NAME]` is a name of your choice. |
24 | | - |
25 | | -1. Set the root password on your Cloud SQL instance: |
26 | | - |
27 | | - gcloud sql instances set-root-password [YOUR_INSTANCE_NAME] --password [YOUR_INSTANCE_ROOT_PASSWORD] |
28 | | - |
29 | | - where `[YOUR_INSTANCE_NAME]` is the name you chose in step 1 and |
30 | | - `[YOUR_INSTANCE_ROOT_PASSWORD]` is a password of your choice. |
31 | | - |
32 | | -1. Using the [Cloud SQL console][sql_console], select your Cloud SQL instance. |
33 | | -Then, create a [user][user] (using the button in the *Access Control* > *Users* tab) and a |
34 | | -[database][database] (using the button in the *Databases* tab). |
35 | | - |
36 | | -1. Create and download a [Service Account][service] for your project. You will |
37 | | -use this service account to connect to your Cloud SQL instance locally. |
38 | | - |
39 | | -1. Download and install the [Cloud SQL Proxy][proxy]. |
40 | | - |
41 | | -1. [Start the proxy][start] to allow connecting to your instance from your local |
42 | | -machine: |
43 | | - |
44 | | - ./cloud_sql_proxy \ |
45 | | - -instances=[YOUR_INSTANCE_CONNECTION_NAME]=tcp:[PORT] \ |
46 | | - -credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON_FILE |
47 | | - |
48 | | - where `[YOUR_INSTANCE_CONNECTION_NAME]` is the connection name of your |
49 | | - instance on its Overview page in the Google Cloud Platform Console, or use |
50 | | - `[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`. If you're using |
51 | | - MySQL, `[PORT]` will be `3306`; for Postgres, it will be `5432`. |
52 | | - |
53 | | -1. In a separate terminal, set the `SQL_USER`, `SQL_PASSWORD`, and `SQL_DATABASE` environment |
54 | | -variables to their respective values. This allows your local app to connect to your Cloud SQL |
55 | | -instance through the proxy. |
56 | | - |
57 | | - export SQL_USER="..." |
58 | | - export SQL_PASSWORD="..." |
59 | | - export SQL_DATABASE="..." |
60 | | - |
61 | | -### Choosing a SQL client |
62 | | -Choose which database connector to use via the `SQL_CLIENT` environment variable. |
63 | | - |
64 | | -To use MySQL, set it to `mysql`: |
65 | | - |
66 | | - export SQL_CLIENT="mysql" |
67 | | - |
68 | | -To use Postgres, set it to `pg`: |
69 | | - |
70 | | - export SQL_CLIENT="pg" |
71 | | - |
72 | | -### Final setup steps |
73 | | -1. Update the values in `app.yaml` with your instance configuration. |
74 | | - |
75 | | -1. Finally, run `createTables.js` to ensure that the database is properly |
76 | | -configured and to create the tables needed for the sample. |
77 | | - |
78 | | -### Running locally |
79 | | - |
80 | | -Refer to the [top-level README](../README.md) for instructions on running and deploying. |
81 | | - |
82 | | -It's recommended to follow the instructions above to run the Cloud SQL proxy. |
83 | | -You will need to set the appropriate environment variables (as shown above) and |
84 | | -run the following commands via your shell to run the sample: |
85 | | - |
86 | | - npm install |
87 | | - npm start |
| 6 | +To run the sample, see [the tutorial][tutorial]. |
88 | 7 |
|
89 | 8 | [sql]: https://cloud.google.com/sql/ |
90 | 9 | [flexible]: https://cloud.google.com/appengine |
91 | | -[gen]: https://cloud.google.com/sql/docs/create-instance |
92 | | -[console]: https://console.developers.google.com |
93 | | -[sql_console]: https://console.developers.google.com/sql/instances/ |
94 | | -[sdk]: https://cloud.google.com/sdk |
95 | | -[service]: https://cloud.google.com/sql/docs/external#createServiceAccount |
96 | | -[proxy]: https://cloud.google.com/sql/docs/external#install |
97 | | -[start]: https://cloud.google.com/sql/docs/external#6_start_the_proxy |
98 | | -[user]: https://cloud.google.com/sql/docs/create-user |
99 | | -[database]: https://cloud.google.com/sql/docs/create-database |
100 | 10 | [mysql]: https://www.mysql.com/downloads/ |
101 | | -[postgres]: https://www.postgresql.org/download/ |
| 11 | +[tutorial]: https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-sql |
0 commit comments