|
2 | 2 |
|
3 | 3 | ## Before you begin
|
4 | 4 |
|
5 |
| -1. Before you use this code sample, you need to have [Composer](https://getcomposer.org/) installed or downloaded into this folder. Download instructions can be found [here](https://getcomposer.org/download/). Once you've installed composer, use it to install required dependencies by running `composer install`. |
6 |
| -2. Create a MySQL Cloud SQL Instance by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string, database user, and database password that you create. |
7 |
| -3. Create a database for your application by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database name. |
8 |
| -4. Create a service account with the 'Cloud SQL Client' permissions by following these [instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account). Download a JSON key to use to authenticate your connection. |
| 5 | +1. Before you use this code sample, you need to have |
| 6 | +[Composer](https://getcomposer.org/) installed or downloaded into this folder. |
| 7 | +Download instructions can be found [here](https://getcomposer.org/download/). |
| 8 | +Once you've installed composer, use it to install required dependencies by |
| 9 | +running `composer install`. |
| 10 | +2. Create a MySQL Cloud SQL Instance by following these |
| 11 | +[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note |
| 12 | +the connection string, database user, and database password that you create. |
| 13 | +3. Create a database for your application by following these |
| 14 | +[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). |
| 15 | +Note the database name. |
| 16 | +4. Create a service account with the 'Cloud SQL Client' permissions by following |
| 17 | +these |
| 18 | +[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account). |
| 19 | +Download a JSON key to use to authenticate your connection. |
9 | 20 |
|
10 | 21 | ## Running Locally
|
11 | 22 |
|
12 |
| -To run this application locally, download and install the `cloud_sql_proxy` by following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install). |
| 23 | +To run this application locally, download and install the `cloud_sql_proxy` by |
| 24 | +following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install). |
13 | 25 |
|
14 |
| -To authenticate with Cloud SQL, set the `$GOOGLE_APPLICATION_CREDENTIALS` environment variable: |
| 26 | +Instructions are provided below for using the proxy with a TCP connection or a |
| 27 | +Unix domain socket. On Linux or macOS, you can use either option, but the |
| 28 | +Windows proxy currently requires a TCP connection. |
| 29 | + |
| 30 | +### Unix Socket mode |
| 31 | +NOTE: this option is currently only supported on Linux and macOS. Windows users |
| 32 | +should use the TCP option. |
| 33 | + |
| 34 | +To use a Unix socket, you'll need to create a directory and give write access to |
| 35 | +the user running the proxy: |
15 | 36 |
|
16 | 37 | ```bash
|
17 |
| -export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json |
| 38 | +sudo mkdir /path/to/the/new/directory |
| 39 | +sudo chown -R $USER /path/to/the/new/directory |
18 | 40 | ```
|
19 | 41 |
|
20 |
| -To run the Cloud SQL proxy, you need to set the instance connection name. See the instructions [here](https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test#get_the_instance_connection_name) for finding the instance connection name. |
| 42 | +You'll also need to initialize an environment variable pointing to the directory |
| 43 | +you just created: |
21 | 44 |
|
22 | 45 | ```bash
|
23 |
| -export CLOUD_SQL_CONNECTION_NAME='::' |
| 46 | +export DB_SOCKET_DIR=/path/to/the/new/directory |
24 | 47 | ```
|
25 | 48 |
|
26 |
| -Once the proxy is ready, use one of the following commands to start the proxy in the background. |
| 49 | +Use these terminal commands to initialize other environment variables as well: |
27 | 50 |
|
28 |
| -You may connect to your instance via either unix sockets or TCP. To connect using a socket, you must provide the `-dir` option when starting the proxy. To connect via TCP, you must provide a port as part of the instance name. Both are demonstrated below. |
| 51 | +```bash |
| 52 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json |
| 53 | +export CLOUD_SQL_CONNECTION_NAME='::' |
| 54 | +export DB_USER='' |
| 55 | +export DB_PASS='' |
| 56 | +export DB_NAME='' |
| 57 | +``` |
29 | 58 |
|
30 |
| -### Unix Socket mode |
| 59 | +Note: Saving credentials in environment variables is convenient, but not |
| 60 | +secure - consider a more secure solution such as |
| 61 | +[Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets |
| 62 | +safe. |
| 63 | + |
| 64 | +Then use the following command to launch the proxy in the background: |
31 | 65 |
|
32 | 66 | ```bash
|
33 |
| -$ ./cloud_sql_proxy -dir=/cloudsql \ |
34 |
| - --instances=$CLOUD_SQL_CONNECTION_NAME \ |
35 |
| - --credential_file=$GOOGLE_APPLICATION_CREDENTIALS |
| 67 | +./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$CLOUD_SQL_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS & |
36 | 68 | ```
|
37 | 69 |
|
38 |
| -Note: Make sure to run the command under a user with write access in the `/cloudsql` directory. This proxy will use this folder to create a unix socket the application will use to connect to Cloud SQL. |
39 |
| - |
40 | 70 | ### TCP mode
|
| 71 | +To run the sample locally with a TCP connection, set environment variables and |
| 72 | +launch the proxy as shown below. |
| 73 | + |
| 74 | +#### Linux / macOS |
| 75 | +Use these terminal commands to initialize environment variables: |
41 | 76 |
|
42 | 77 | ```bash
|
43 |
| -$ ./cloud_sql_proxy \ |
44 |
| - --instances=$CLOUD_SQL_CONNECTION_NAME=tcp:3306 \ |
45 |
| - --credential_file=$GOOGLE_APPLICATION_CREDENTIALS |
| 78 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json |
| 79 | +export CLOUD_SQL_CONNECTION_NAME='::' |
| 80 | +export DB_HOST='127.0.0.1' |
| 81 | +export DB_USER='' |
| 82 | +export DB_PASS='' |
| 83 | +export DB_NAME='' |
46 | 84 | ```
|
47 | 85 |
|
48 |
| -### Set Configuration Values |
49 |
| -Set the required environment variables for your connection to Cloud SQL. If you are using TCP mode as described above, do not set the `CLOUD_SQL_CONNECTION_NAME` variable. |
| 86 | +Note: Saving credentials in environment variables is convenient, but not |
| 87 | +secure - consider a more secure solution such as |
| 88 | +[Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets |
| 89 | +safe. |
| 90 | + |
| 91 | +Then use the following command to launch the proxy in the background: |
50 | 92 |
|
51 | 93 | ```bash
|
52 |
| -export DB_USER='my-db-user' |
53 |
| -export DB_PASS='my-db-pass' |
54 |
| -export DB_NAME='my-db-name' |
55 |
| -export DB_HOSTNAME='localhost' |
| 94 | +./cloud_sql_proxy -instances=$CLOUD_SQL_CONNECTION_NAME=tcp:3306 -credential_file=$GOOGLE_APPLICAITON_CREDENTIALS & |
56 | 95 | ```
|
57 | 96 |
|
58 |
| -Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets safe. |
| 97 | +#### Windows/PowerShell |
| 98 | +Use these PowerShell commands to initialize environment variables: |
59 | 99 |
|
60 |
| -Execute the following: |
| 100 | +```powershell |
| 101 | +$env:GOOGLE_APPLICATION_CREDENTIALS="" |
| 102 | +$env:DB_HOST="127.0.0.1" |
| 103 | +$env:DB_USER="" |
| 104 | +$env:DB_PASS="" |
| 105 | +$env:DB_NAME="" |
| 106 | +``` |
61 | 107 |
|
62 |
| -```bash |
63 |
| -$ php -S localhost:8080 |
| 108 | +Note: Saving credentials in environment variables is convenient, but not |
| 109 | +secure - consider a more secure solution such as |
| 110 | +[Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets |
| 111 | +safe. |
| 112 | + |
| 113 | +Then use the following command to launch the proxy in a separate PowerShell |
| 114 | +session: |
| 115 | + |
| 116 | +```powershell |
| 117 | +Start-Process -filepath "C:\" -ArgumentList "-instances=::=tcp:3306 -credential_file=" |
| 118 | +``` |
| 119 | + |
| 120 | +### Testing the application |
| 121 | +Execute the following to start the application server: |
| 122 | +``` bash |
| 123 | +php -S localhost:8080 |
64 | 124 | ```
|
65 | 125 |
|
66 |
| -Navigate towards http://localhost:8080 to verify your application is running correctly. |
| 126 | +Navigate towards http://localhost:8080 to verify your application is running |
| 127 | +correctly. |
67 | 128 |
|
68 | 129 | ## Google App Engine Flex
|
| 130 | +To run on App Engine Flex, create an App Engine project by following the setup |
| 131 | +for these |
| 132 | +[instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin). |
69 | 133 |
|
70 |
| -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). |
| 134 | +First, update `app.flex.yaml` with the correct values to pass the environment |
| 135 | +variables into the runtime. |
71 | 136 |
|
72 |
| -First, update `app.yaml` with the correct values to pass the environment variables into the runtime. |
| 137 | +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` |
| 139 | +field under `env_variables`. Also make sure to remove the uncommented |
| 140 | +`beta_settings` and `cloud_sql_instances` fields and replace them with the |
| 141 | +commented `beta_settings` and `cloud_sql_instances` fields. |
73 | 142 |
|
74 |
| -Then, make sure that the service account `service-{PROJECT_NUMBER}>@gae-api-prod.google.com.iam.gserviceaccount.com` has the IAM role `Cloud SQL Client`. |
| 143 | +Then, make sure that the service account |
| 144 | +`service-{PROJECT_NUMBER}>@gae-api-prod.google.com.iam.gserviceaccount.com` has |
| 145 | +the IAM role `Cloud SQL Client`. |
75 | 146 |
|
76 |
| -Next, the following command will deploy the application to your Google Cloud project: |
| 147 | +Next, the following command will deploy the application to your Google Cloud |
| 148 | +project: |
77 | 149 |
|
78 | 150 | ```bash
|
79 |
| -$ gcloud beta app deploy |
| 151 | +$ gcloud beta app deploy app.flex.yaml |
80 | 152 | ```
|
81 | 153 |
|
82 | 154 | ## Google App Engine Standard
|
| 155 | +Note: App Engine Standard does not support TCP connections to Cloud SQL |
| 156 | +instances, only Unix socket connections. |
83 | 157 |
|
84 |
| -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). |
| 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). |
85 | 161 |
|
86 |
| -First, update `app.yaml` with the correct values to pass the environment variables into the runtime. |
| 162 | +First, update `app.standard.yaml` with the correct values to pass the |
| 163 | +environment variables into the runtime. |
87 | 164 |
|
88 |
| -Next, the following command will deploy the application to your Google Cloud project: |
| 165 | +Next, the following command will deploy the application to your Google Cloud |
| 166 | +project: |
89 | 167 |
|
90 | 168 | ```bash
|
91 |
| -$ gcloud app deploy app-standard.yaml |
| 169 | +$ gcloud app deploy app.standard.yaml |
92 | 170 | ```
|
0 commit comments