Skip to content

Commit 69dbd8c

Browse files
feat(cloudsql/mysql): update mysql to v2 samples (GoogleCloudPlatform#7856)
1 parent d98de56 commit 69dbd8c

File tree

14 files changed

+578
-420
lines changed

14 files changed

+578
-420
lines changed

cloud-sql/mysql/sqlalchemy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ COPY ./certs /app/certs
3737
# webserver, with one worker process and 8 threads.
3838
# For environments with multiple CPU cores, increase the number of workers
3939
# to be equal to the cores available.
40-
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
40+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

cloud-sql/mysql/sqlalchemy/README.md

Lines changed: 89 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,38 @@ shown below.
3636
Use these terminal commands to initialize environment variables:
3737
```bash
3838
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
39-
export DB_HOST='127.0.0.1:3306'
40-
export DB_USER='<DB_USER_NAME>'
41-
export DB_PASS='<DB_PASSWORD>'
42-
export DB_NAME='<DB_NAME>'
39+
export INSTANCE_HOST='127.0.0.1'
40+
export DB_PORT='3306'
41+
export DB_USER='<YOUR_DB_USER_NAME>'
42+
export DB_PASS='<YOUR_DB_PASSWORD>'
43+
export DB_NAME='<YOUR_DB_NAME>'
4344
```
4445
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
4546
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
4647
help keep secrets safe.
4748

4849
Then use this command to launch the proxy in the background:
4950
```bash
50-
./cloud_sql_proxy -instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
51+
./cloud_sql_proxy -instances=<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
5152
```
5253

5354
#### Windows/PowerShell
5455
Use these PowerShell commands to initialize environment variables:
5556
```powershell
5657
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
57-
$env:DB_HOST="127.0.0.1:3306"
58-
$env:DB_USER="<DB_USER_NAME>"
59-
$env:DB_PASS="<DB_PASSWORD>"
60-
$env:DB_NAME="<DB_NAME>"
58+
$env:INSTANCE_HOST="127.0.0.1"
59+
$env:DB_PORT="3306"
60+
$env:DB_USER="<YOUR_DB_USER_NAME>"
61+
$env:DB_PASS="<YOUR_DB_PASSWORD>"
62+
$env:DB_NAME="<YOUR_DB_NAME>"
6163
```
6264
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
6365
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
6466
help keep secrets safe.
6567

6668
Then use this command to launch the proxy in a separate PowerShell session:
6769
```powershell
68-
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>"
70+
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>"
6971
```
7072

7173
### Launch proxy with Unix Domain Socket
@@ -80,26 +82,21 @@ sudo mkdir /cloudsql
8082
sudo chown -R $USER /cloudsql
8183
```
8284

83-
You'll also need to initialize an environment variable containing the directory you just created:
84-
```bash
85-
export DB_SOCKET_DIR=/path/to/the/new/directory
86-
```
87-
8885
Use these terminal commands to initialize other environment variables as well:
8986
```bash
9087
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
91-
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
92-
export DB_USER='<DB_USER_NAME>'
93-
export DB_PASS='<DB_PASSWORD>'
94-
export DB_NAME='<DB_NAME>'
88+
export INSTANCE_UNIX_SOCKET='./cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>'
89+
export DB_USER='<YOUR_DB_USER_NAME>'
90+
export DB_PASS='<YOUR_DB_PASSWORD>'
91+
export DB_NAME='<YOUR_DB_NAME>'
9592
```
9693
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
9794
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
9895
help keep secrets safe.
9996

10097
Then use this command to launch the proxy in the background:
10198
```bash
102-
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
99+
./cloud_sql_proxy -dir=./cloudsql --instances=<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME> --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
103100
```
104101

105102
### Testing the application
@@ -113,34 +110,69 @@ pip install -r requirements.txt
113110

114111
Finally, start the application:
115112
```bash
116-
python main.py
113+
python app.py
117114
```
118115

119116
Navigate towards `http://127.0.0.1:8080` to verify your application is running correctly.
120117

121-
## Google App Engine Standard
118+
## Deploy to App Engine Standard
122119

123120
To run on GAE-Standard, create an App Engine project by following the setup for these
124121
[instructions](https://cloud.google.com/appengine/docs/standard/python3/quickstart#before-you-begin).
125122

126123
First, update `app.standard.yaml` with the correct values to pass the environment
127-
variables into the runtime.
124+
variables into the runtime. Your `app.standard.yaml` file should look like this:
125+
126+
```yaml
127+
runtime: python37
128+
entrypoint: gunicorn -b :$PORT app:app
129+
130+
env_variables:
131+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
132+
DB_USER: <YOUR_DB_USER_NAME>
133+
DB_PASS: <YOUR_DB_PASSWORD>
134+
DB_NAME: <YOUR_DB_NAME>
135+
```
136+
137+
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
138+
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
139+
help keep secrets safe.
128140
129141
Next, the following command will deploy the application to your Google Cloud project:
142+
130143
```bash
131144
gcloud app deploy app.standard.yaml
132145
```
133146

134-
## Google App Engine Flexible
147+
## Deploy to App Engine Flexible
135148

136149
To run on GAE-Flexible, create an App Engine project by following the setup for these
137150
[instructions](https://cloud.google.com/appengine/docs/flexible/python/quickstart#before-you-begin).
138151

139152
First, update `app.flexible.yaml` with the correct values to pass the environment
140-
variables into the runtime. Also update this file to configure either a TCP or a
141-
Unix domain socket connection to your database.
153+
variables into the runtime. Your `app.flexible.yaml` file should look like this:
154+
155+
```yaml
156+
runtime: custom
157+
env: flex
158+
entrypoint: gunicorn -b :$PORT app:app
159+
160+
env_variables:
161+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
162+
DB_USER: <YOUR_DB_USER_NAME>
163+
DB_PASS: <YOUR_DB_PASSWORD>
164+
DB_NAME: <YOUR_DB_NAME>
165+
166+
beta_settings:
167+
cloud_sql_instances: <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
168+
```
169+
170+
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
171+
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
172+
help keep secrets safe.
142173
143174
Next, the following command will deploy the application to your Google Cloud project:
175+
144176
```bash
145177
gcloud app deploy app.flexible.yaml
146178
```
@@ -153,51 +185,64 @@ for more details on connecting a Cloud Run service to Cloud SQL.
153185
1. Build the container image:
154186

155187
```sh
156-
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql
188+
gcloud builds submit --tag gcr.io/<YOUR_PROJECT_ID>/run-mysql
157189
```
158190

159191
2. Deploy the service to Cloud Run:
160192

161193
```sh
162-
gcloud run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
194+
gcloud run deploy run-mysql --image gcr.io/<YOUR_PROJECT_ID>/run-mysql \
195+
--add-cloudsql-instances '<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
196+
--set-env-vars INSTANCE_UNIX_SOCKET='/cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
197+
--set-env-vars DB_USER='<YOUR_DB_USER_NAME>' \
198+
--set-env-vars DB_PASS='<YOUR_DB_PASSWORD>' \
199+
--set-env-vars DB_NAME='<YOUR_DB_NAME>'
163200
```
164201

165202
Take note of the URL output at the end of the deployment process.
166203

167-
3. Configure the service for use with Cloud Run
168-
169-
```sh
170-
gcloud run services update run-mysql \
171-
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
172-
--set-env-vars INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
173-
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
174-
```
175204
Replace environment variables with the correct values for your Cloud SQL
176205
instance configuration.
177206

178-
This step can be done as part of deployment but is separated for clarity.
179-
180207
It is recommended to use the [Secret Manager integration](https://cloud.google.com/run/docs/configuring/secrets) for Cloud Run instead
181208
of using environment variables for the SQL configuration. The service injects the SQL credentials from
182209
Secret Manager at runtime via an environment variable.
183210

184211
Create secrets via the command line:
185212
```sh
186-
echo -n $INSTANCE_CONNECTION_NAME | \
187-
gcloud secrets create [INSTANCE_CONNECTION_NAME_SECRET] --data-file=-
213+
echo -n $INSTANCE_UNIX_SOCKET | \
214+
gcloud secrets create [INSTANCE_UNIX_SOCKET_SECRET] --data-file=-
188215
```
189216

190217
Deploy the service to Cloud Run specifying the env var name and secret name:
191218
```sh
192-
gcloud beta run deploy SERVICE --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
193-
--add-cloudsql-instances $INSTANCE_CONNECTION_NAME \
194-
--update-secrets INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME_SECRET]:latest,\
219+
gcloud beta run deploy SERVICE --image gcr.io/<YOUR_PROJECT_ID>/run-sql \
220+
--add-cloudsql-instances <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME> \
221+
--update-secrets INSTANCE_UNIX_SOCKET=[INSTANCE_UNIX_SOCKET_SECRET]:latest,\
195222
DB_USER=[DB_USER_SECRET]:latest, \
196223
DB_PASS=[DB_PASS_SECRET]:latest, \
197224
DB_NAME=[DB_NAME_SECRET]:latest
198225
```
199226

200-
4. Navigate your browser to the URL noted in step 2.
227+
3. Navigate your browser to the URL noted in step 2.
201228

202229
For more details about using Cloud Run see http://cloud.run.
203230
Review other [Python on Cloud Run samples](../../../run/).
231+
232+
## Deploy to Cloud Functions
233+
234+
To deploy the service to [Cloud Functions](https://cloud.google.com/functions/docs) run the following command:
235+
236+
```sh
237+
gcloud functions deploy votes --runtime python39 --trigger-http --allow-unauthenticated \
238+
--set-env-vars INSTANCE_UNIX_SOCKET=/cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME> \
239+
--set-env-vars DB_USER=$DB_USER \
240+
--set-env-vars DB_PASS=$DB_PASS \
241+
--set-env-vars DB_NAME=$DB_NAME
242+
```
243+
244+
Take note of the URL output at the end of the deployment process or run the following to view your function:
245+
246+
```sh
247+
gcloud app browse
248+
```

cloud-sql/mysql/sqlalchemy/app.flexible.yaml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
runtime: python
15+
runtime: custom
1616
env: flex
17-
entrypoint: gunicorn -b :$PORT main:app
17+
entrypoint: gunicorn -b :$PORT app:app
1818

19-
runtime_config:
20-
python_version: 3
21-
22-
# Choose to enable either a TCP or Unix domain socket for your database connection
23-
# # Enable a Unix domain socket
24-
# beta_settings:
25-
# cloud_sql_instances: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>
26-
27-
# Enable a TCP domain socket
28-
beta_settings:
29-
cloud_sql_instances: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>=tcp:<PORT>
30-
31-
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
32-
# something like https://cloud.google.com/secret-manager/docs/overview to help keep
33-
# secrets secret.
19+
# Note: Saving credentials in environment variables is convenient, but not
20+
# secure - consider a more secure solution such as
21+
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
22+
# keep secrets safe.
3423
env_variables:
35-
INSTANCE_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>
36-
DB_USER: my-db-user
37-
DB_PASS: my-db-pass
38-
DB_NAME: my_db
24+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
25+
DB_USER: <YOUR_DB_USER_NAME>
26+
DB_PASS: <YOUR_DB_PASSWORD>
27+
DB_NAME: <YOUR_DB_NAME>
3928

40-
# TCP connection setup; remove if using a Unix domain socket instead
41-
DB_HOST: 172.17.0.1:<PORT>
29+
beta_settings:
30+
cloud_sql_instances: <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>

0 commit comments

Comments
 (0)