Skip to content

Commit b3dea20

Browse files
authored
Fix cloudsql README and app.yamls (GoogleCloudPlatform#642)
1 parent b9fbf5b commit b3dea20

File tree

5 files changed

+59
-42
lines changed

5 files changed

+59
-42
lines changed

appengine/php72/cloudsql/README.md

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@ Before you can run or deploy the sample, you will need to do the following:
1010

1111
$ gcloud sql instances create YOUR_INSTANCE_NAME
1212

13-
1. Set the root password on your Cloud SQL instance:
13+
1. Create a database for the sample, for instance `cloudsql_sample`:
1414

15-
$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
15+
$ gcloud sql databases create cloudsql_sample --instance=YOUR_INSTANCE_NAME
1616

17-
1. Update the connection string in `app.yaml` with your configuration values. These values are used when the application is deployed.
17+
1. Set the root password on your Cloud SQL instance:
1818

19-
## Run locally
19+
$ gcloud sql users set-password root --host % --instance YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
2020

21-
You can connect to a local database instance by setting the `CLOUDSQL_` environment variables
22-
to your local instance. Alternatively, you can set them to your Cloud instances, but you will need
23-
to create a firewall rule for this, which may be a safety concern.
21+
1. Clone the repository and CD into the directory for this sample
2422

25-
```sh
26-
cd php-docs-samples/appengine/php72/cloudsql
23+
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
24+
cd php-docs-samples/appengine/php72/cloudsql
2725

28-
# set local connection parameters
29-
export CLOUDSQL_USERNAME=root
30-
export CLOUDSQL_PASSWORD=
31-
export CLOUDSQL_DSN="mysql:host=localhost;dbname=guestbook"
26+
1. Update `app.yaml` (or if you're using CloudSQL with PostgreSQL, update `app-postgres.yaml`)
27+
with your configuration values. These values are used when the application is deployed:
3228

33-
php -S localhost:8080
34-
```
35-
36-
> be sure the `CLOUDSQL_` environment variables are appropriate for your MySQL or PostgreSQL instance.
37-
38-
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
39-
in your browser.
29+
env_variables:
30+
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
31+
# values obtained when configuring your Cloud SQL instance.
32+
CLOUDSQL_USER: USER
33+
CLOUDSQL_PASSWORD: PASSWORD
34+
CLOUDSQL_DSN: "mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME"
4035

4136
## Deploy to App Engine
4237

@@ -46,11 +41,54 @@ in your browser.
4641

4742
**Deploy with gcloud**
4843

49-
```
44+
If you haven't already, authenticate gcloud using your Google account and
45+
configure gcloud to use your project ID:
46+
47+
```sh
48+
gcloud auth login
5049
gcloud config set project YOUR_PROJECT_ID
50+
```
51+
52+
Next, deploy your application:
53+
54+
```sh
5155
gcloud app deploy
5256
gcloud app browse
5357
```
5458

5559
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
5660
in your browser.
61+
62+
If your CloudSQL instance is running PostgreSQL instead of MySQL, deploy using
63+
`app-postgres.yaml` instead:
64+
65+
```sh
66+
gcloud app deploy app-postgres.yaml
67+
```
68+
69+
## Run locally
70+
71+
To run the sample locally, you will want to use the [CloudSQL proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).
72+
The CloudSQL proxy allows you to connect to your CloudSQL instance locally without
73+
having to set up firewall rules.
74+
75+
```sh
76+
./cloud_sql_proxy \
77+
-instances YOUR_INSTANCE_NAME \
78+
-dir /cloudsql \
79+
-credentials /path/to/your_service_account_credentials.json
80+
```
81+
82+
Then set your CloudSQL environment variables and run the PHP web server:
83+
84+
```sh
85+
# set local connection parameters (but replace the uppercase words!)
86+
export CLOUDSQL_USERNAME=USER
87+
export CLOUDSQL_PASSWORD=PASSWORD
88+
export CLOUDSQL_DSN="mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME"
89+
90+
php -S localhost:8080
91+
```
92+
93+
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
94+
in your browser.

appengine/php72/cloudsql/app-postgres.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
runtime: php72
55

6-
handlers:
7-
- url: /
8-
script: index.php
9-
106
# [START gae_cloudsql_postgres_env]
117
env_variables:
128
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
@@ -15,9 +11,3 @@ env_variables:
1511
CLOUDSQL_PASSWORD: PASSWORD
1612
CLOUDSQL_DSN: "pgsql:dbname=DATABASE;host=/cloudsql/CONNECTION_NAME"
1713
# [END gae_cloudsql_postgres_env]
18-
19-
# [START gae_cloudsql_postgres_settings]
20-
# Use the connection name obtained when configuring your Cloud SQL instance.
21-
beta_settings:
22-
cloud_sql_instances: "CONNECTION_NAME"
23-
# [END gae_cloudsql_postgres_settings]

appengine/php72/cloudsql/app.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
runtime: php72
55

6-
handlers:
7-
- url: /
8-
script: index.php
9-
106
# [START gae_cloudsql_mysql_env]
117
env_variables:
128
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
@@ -16,8 +12,3 @@ env_variables:
1612
CLOUDSQL_DSN: "mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME"
1713
# [END gae_cloudsql_mysql_env]
1814

19-
# [START gae_cloudsql_mysql_settings]
20-
# Use the connection name obtained when configuring your Cloud SQL instance.
21-
beta_settings:
22-
cloud_sql_instances: "CONNECTION_NAME"
23-
# [END gae_cloudsql_mysql_settings]

appengine/php72/cloudsql/test/DeployMySQLTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static function beforeDeploy()
5959
$appYaml = Yaml::parse($appYamlContents);
6060
$appYaml['env_variables']['CLOUDSQL_USER'] = $user;
6161
$appYaml['env_variables']['CLOUDSQL_PASSWORD'] = $password;
62-
$appYaml['beta_settings']['cloud_sql_instances'] = $connectionName;
6362
$appYaml['env_variables']['CLOUDSQL_DSN'] = str_replace(
6463
['DATABASE', 'CONNECTION_NAME'],
6564
[$database, $connectionName],

appengine/php72/cloudsql/test/DeployPostgresTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static function beforeDeploy()
5959
$appYaml = Yaml::parse($appYamlContents);
6060
$appYaml['env_variables']['CLOUDSQL_USER'] = $user;
6161
$appYaml['env_variables']['CLOUDSQL_PASSWORD'] = $password;
62-
$appYaml['beta_settings']['cloud_sql_instances'] = $connectionName;
6362
$appYaml['env_variables']['CLOUDSQL_DSN'] = str_replace(
6463
['DATABASE', 'CONNECTION_NAME'],
6564
[$database, $connectionName],

0 commit comments

Comments
 (0)