Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions cloud_sql/mysql/pdo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,24 @@ Instructions are provided below for using the proxy with a TCP connection or a
Unix domain socket. On Linux or macOS, you can use either option, but the
Windows proxy currently requires a TCP connection.

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

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

```bash
sudo mkdir /path/to/the/new/directory
sudo chown -R $USER /path/to/the/new/directory
```

You'll also need to initialize an environment variable pointing to the directory
you just created:

```bash
export DB_SOCKET_DIR=/path/to/the/new/directory
sudo mkdir /cloudsql
sudo chown -R $USER /cloudsql
```

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

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

```bash
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
./cloud_sql_proxy -dir=/cloudsql --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
```

### TCP mode
### Launch proxy with TCP
To run the sample locally with a TCP connection, set environment variables and
launch the proxy as shown below.

#### Linux / macOS
#### Linux / Mac OS
Use these terminal commands to initialize environment variables:

```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
export DB_HOST='127.0.0.1'
export INSTANCE_CONNECTION_NAME='<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>'
export INSTANCE_HOST='127.0.0.1'
export DB_USER='<DB_USER_NAME>'
export DB_PASS='<DB_PASSWORD>'
export DB_NAME='<DB_NAME>'
Expand All @@ -99,7 +93,7 @@ Use these PowerShell commands to initialize environment variables:

```powershell
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
$env:DB_HOST="127.0.0.1"
$env:INSTANCE_HOST="127.0.0.1"
$env:DB_USER="<DB_USER_NAME>"
$env:DB_PASS="<DB_PASSWORD>"
$env:DB_NAME="<DB_NAME>"
Expand All @@ -126,45 +120,52 @@ php -S localhost:8080
Navigate towards http://localhost:8080 to verify your application is running
correctly.

## Google App Engine Standard
Note: App Engine Standard does not support TCP connections to Cloud SQL
instances, only Unix socket connections.

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

First, update [app.standard.yaml](app.standard.yaml) with the correct values to pass the
environment variables into the runtime.

Next, delete the `composer.lock` file if it exists. This will ensure that the sample app
is built with the package versions specified in `composer.json`.

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

```bash
$ gcloud app deploy app.standard.yaml
```

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

First, update `app.flex.yaml` with the correct values to pass the environment
First, update [app.flex.yaml](app.flex.yaml) with the correct values to pass the environment
variables into the runtime.

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

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

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

```bash
$ gcloud beta app deploy app.flex.yaml
```

## Google App Engine Standard
Note: App Engine Standard does not support TCP connections to Cloud SQL
instances, only Unix socket connections.

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

First, update `app.standard.yaml` with the correct values to pass the
environment variables into the runtime.
Also, make sure that the Cloud Build service account
`cloudbuild@<PROJECT-ID>.iam.gserviceaccount.com` has
the IAM role `Cloud SQL Client`.

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

```bash
$ gcloud app deploy app.standard.yaml
$ gcloud beta app deploy app.flex.yaml
```
14 changes: 7 additions & 7 deletions cloud_sql/mysql/pdo/app.flex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ env: flex
# something like https://cloud.google.com/secret-manager/ to help keep secrets
# secret.
env_variables:
INSTANCE_CONNECTION_NAME: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>"
DB_USER: my-db-user
DB_PASS: my-db-pass
DB_NAME: my-db
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
DB_USER: <YOUR_DB_USER_NAME>
DB_PASS: <YOUR_DB_PASSWORD>
DB_NAME: <YOUR_DB_NAME>

# TCP domain socket setup; uncomment if using a TCP domain socket
# DB_HOST: 172.17.0.1
# INSTANCE_HOST: 172.17.0.1


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

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

runtime_config:
document_root: .
Expand Down
8 changes: 4 additions & 4 deletions cloud_sql/mysql/pdo/app.standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ runtime: php74
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
# something like https://cloud.google.com/secret-manager/ to help keep secrets secret.
env_variables:
INSTANCE_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>
DB_USER: my-db-user
DB_PASS: my-db-pass
DB_NAME: my-db
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
DB_USER: <YOUR_DB_USER_NAME>
DB_PASS: <YOUR_DB_PASSWORD>
DB_NAME: <YOUR_DB_NAME>

# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
Expand Down
6 changes: 3 additions & 3 deletions cloud_sql/mysql/pdo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"php": ">= 7.2",
"slim/slim": "^4.5",
"slim/twig-view": "^3.1",
"pimple/pimple": "^3.3",
"guzzlehttp/psr7": "^2.0",
"http-interop/http-factory-guzzle": "^1.0"
"slim/http": "^1.0",
"slim/psr7": "^1.0",
"pimple/pimple": "^3.3"
}
}
5 changes: 3 additions & 2 deletions cloud_sql/mysql/pdo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

declare(strict_types=1);

use GuzzleHttp\Psr7;
use Slim\Psr7\Factory\StreamFactory;

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

Expand Down Expand Up @@ -48,7 +48,8 @@
: 'An error occurred';
}

return $response->withBody(Psr7\stream_for($message));
$streamFactory = new StreamFactory;
return $response->withBody($streamFactory->createStream($message));
});

$app->run();
151 changes: 0 additions & 151 deletions cloud_sql/mysql/pdo/src/DBInitializer.php

This file was deleted.

Loading