Skip to content

Commit de7a67b

Browse files
authored
feat(cloud_sql/mysql): update to V2 sample (GoogleCloudPlatform#1635)
1 parent 37a945c commit de7a67b

File tree

10 files changed

+281
-272
lines changed

10 files changed

+281
-272
lines changed

cloud_sql/mysql/pdo/README.md

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,24 @@ Instructions are provided below for using the proxy with a TCP connection or a
2727
Unix domain socket. On Linux or macOS, you can use either option, but the
2828
Windows proxy currently requires a TCP connection.
2929

30-
### Unix Socket mode
30+
### Launch proxy with Unix Domain Socket
3131
NOTE: this option is currently only supported on Linux and macOS. Windows users
3232
should use the TCP option.
3333

3434
To use a Unix socket, you'll need to create a directory and give write access to
3535
the user running the proxy:
3636

3737
```bash
38-
sudo mkdir /path/to/the/new/directory
39-
sudo chown -R $USER /path/to/the/new/directory
40-
```
41-
42-
You'll also need to initialize an environment variable pointing to the directory
43-
you just created:
44-
45-
```bash
46-
export DB_SOCKET_DIR=/path/to/the/new/directory
38+
sudo mkdir /cloudsql
39+
sudo chown -R $USER /cloudsql
4740
```
4841

4942
Use these terminal commands to initialize other environment variables as well:
5043

5144
```bash
5245
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
53-
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
46+
export INSTANCE_CONNECTION_NAME='<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>'
47+
export INSTANCE_UNIX_SOCKET='/cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>'
5448
export DB_USER='<DB_USER_NAME>'
5549
export DB_PASS='<DB_PASSWORD>'
5650
export DB_NAME='<DB_NAME>'
@@ -64,20 +58,20 @@ safe.
6458
Then use the following command to launch the proxy in the background:
6559

6660
```bash
67-
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
61+
./cloud_sql_proxy -dir=/cloudsql --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
6862
```
6963

70-
### TCP mode
64+
### Launch proxy with TCP
7165
To run the sample locally with a TCP connection, set environment variables and
7266
launch the proxy as shown below.
7367

74-
#### Linux / macOS
68+
#### Linux / Mac OS
7569
Use these terminal commands to initialize environment variables:
7670

7771
```bash
7872
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
79-
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
80-
export DB_HOST='127.0.0.1'
73+
export INSTANCE_CONNECTION_NAME='<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>'
74+
export INSTANCE_HOST='127.0.0.1'
8175
export DB_USER='<DB_USER_NAME>'
8276
export DB_PASS='<DB_PASSWORD>'
8377
export DB_NAME='<DB_NAME>'
@@ -99,7 +93,7 @@ Use these PowerShell commands to initialize environment variables:
9993

10094
```powershell
10195
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
102-
$env:DB_HOST="127.0.0.1"
96+
$env:INSTANCE_HOST="127.0.0.1"
10397
$env:DB_USER="<DB_USER_NAME>"
10498
$env:DB_PASS="<DB_PASSWORD>"
10599
$env:DB_NAME="<DB_NAME>"
@@ -126,45 +120,52 @@ php -S localhost:8080
126120
Navigate towards http://localhost:8080 to verify your application is running
127121
correctly.
128122

123+
## Google App Engine Standard
124+
Note: App Engine Standard does not support TCP connections to Cloud SQL
125+
instances, only Unix socket connections.
126+
127+
To run on GAE-Standard, create an App Engine project by following the setup for
128+
these
129+
[instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
130+
131+
First, update [app.standard.yaml](app.standard.yaml) with the correct values to pass the
132+
environment variables into the runtime.
133+
134+
Next, delete the `composer.lock` file if it exists. This will ensure that the sample app
135+
is built with the package versions specified in `composer.json`.
136+
137+
Next, the following command will deploy the application to your Google Cloud
138+
project:
139+
140+
```bash
141+
$ gcloud app deploy app.standard.yaml
142+
```
143+
129144
## Google App Engine Flex
130145
To run on App Engine Flex, create an App Engine project by following the setup
131146
for these
132147
[instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
133148

134-
First, update `app.flex.yaml` with the correct values to pass the environment
149+
First, update [app.flex.yaml](app.flex.yaml) with the correct values to pass the environment
135150
variables into the runtime.
136151

137152
To use a TCP connection instead of a Unix socket to connect your sample to your
138-
Cloud SQL instance on App Engine, make sure to uncomment the `DB_HOST`
153+
Cloud SQL instance on App Engine, make sure to uncomment the `INSTANCE_HOST`
139154
field under `env_variables`. Also make sure to remove the uncommented
140155
`beta_settings` and `cloud_sql_instances` fields and replace them with the
141156
commented `beta_settings` and `cloud_sql_instances` fields.
142157

143-
Then, make sure that the service account
144-
`service-{PROJECT_NUMBER}>@gae-api-prod.google.com.iam.gserviceaccount.com` has
158+
Then, make sure that the App Engine default service account
159+
`<PROJECT-ID>@appspot.gserviceaccount.com` has
145160
the IAM role `Cloud SQL Client`.
146161

147-
Next, the following command will deploy the application to your Google Cloud
148-
project:
149-
150-
```bash
151-
$ gcloud beta app deploy app.flex.yaml
152-
```
153-
154-
## Google App Engine Standard
155-
Note: App Engine Standard does not support TCP connections to Cloud SQL
156-
instances, only Unix socket connections.
157-
158-
To run on GAE-Standard, create an App Engine project by following the setup for
159-
these
160-
[instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
161-
162-
First, update `app.standard.yaml` with the correct values to pass the
163-
environment variables into the runtime.
162+
Also, make sure that the Cloud Build service account
163+
`cloudbuild@<PROJECT-ID>.iam.gserviceaccount.com` has
164+
the IAM role `Cloud SQL Client`.
164165

165166
Next, the following command will deploy the application to your Google Cloud
166167
project:
167168

168169
```bash
169-
$ gcloud app deploy app.standard.yaml
170+
$ gcloud beta app deploy app.flex.yaml
170171
```

cloud_sql/mysql/pdo/app.flex.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ env: flex
1919
# something like https://cloud.google.com/secret-manager/ to help keep secrets
2020
# secret.
2121
env_variables:
22-
INSTANCE_CONNECTION_NAME: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>"
23-
DB_USER: my-db-user
24-
DB_PASS: my-db-pass
25-
DB_NAME: my-db
22+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
23+
DB_USER: <YOUR_DB_USER_NAME>
24+
DB_PASS: <YOUR_DB_PASSWORD>
25+
DB_NAME: <YOUR_DB_NAME>
2626

2727
# TCP domain socket setup; uncomment if using a TCP domain socket
28-
# DB_HOST: 172.17.0.1
28+
# INSTANCE_HOST: 172.17.0.1
2929

3030

3131
# Choose to enable either a TCP or Unix domain socket for your database
3232
# connection:
3333
# Enable a Unix domain socket:
3434
beta_settings:
35-
cloud_sql_instances: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>"
35+
cloud_sql_instances: "<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>"
3636

3737
# Enable a TCP domain socket:
3838
# beta_settings:
39-
# cloud_sql_instances: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:3306"
39+
# cloud_sql_instances: "<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:3306"
4040

4141
runtime_config:
4242
document_root: .

cloud_sql/mysql/pdo/app.standard.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ runtime: php74
1717
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
1818
# something like https://cloud.google.com/secret-manager/ to help keep secrets secret.
1919
env_variables:
20-
INSTANCE_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>
21-
DB_USER: my-db-user
22-
DB_PASS: my-db-pass
23-
DB_NAME: my-db
20+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
21+
DB_USER: <YOUR_DB_USER_NAME>
22+
DB_PASS: <YOUR_DB_PASSWORD>
23+
DB_NAME: <YOUR_DB_NAME>
2424

2525
# Defaults to "serve index.php" and "serve public/index.php". Can be used to
2626
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to

cloud_sql/mysql/pdo/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"php": ">= 7.2",
1010
"slim/slim": "^4.5",
1111
"slim/twig-view": "^3.1",
12-
"pimple/pimple": "^3.3",
13-
"guzzlehttp/psr7": "^2.0",
14-
"http-interop/http-factory-guzzle": "^1.0"
12+
"slim/http": "^1.0",
13+
"slim/psr7": "^1.0",
14+
"pimple/pimple": "^3.3"
1515
}
1616
}

cloud_sql/mysql/pdo/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
declare(strict_types=1);
1919

20-
use GuzzleHttp\Psr7;
20+
use Slim\Psr7\Factory\StreamFactory;
2121

2222
include __DIR__ . '/vendor/autoload.php';
2323

@@ -48,7 +48,8 @@
4848
: 'An error occurred';
4949
}
5050

51-
return $response->withBody(Psr7\stream_for($message));
51+
$streamFactory = new StreamFactory;
52+
return $response->withBody($streamFactory->createStream($message));
5253
});
5354

5455
$app->run();

cloud_sql/mysql/pdo/src/DBInitializer.php

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)