@@ -10,33 +10,28 @@ Before you can run or deploy the sample, you will need to do the following:
10
10
11
11
$ gcloud sql instances create YOUR_INSTANCE_NAME
12
12
13
- 1 . Set the root password on your Cloud SQL instance:
13
+ 1 . Create a database for the sample, for instance ` cloudsql_sample ` :
14
14
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
16
16
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:
18
18
19
- ## Run locally
19
+ $ gcloud sql users set-password root --host % --instance YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
20
20
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
24
22
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
27
25
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:
32
28
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"
40
35
41
36
## Deploy to App Engine
42
37
@@ -46,11 +41,54 @@ in your browser.
46
41
47
42
** Deploy with gcloud**
48
43
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
50
49
gcloud config set project YOUR_PROJECT_ID
50
+ ```
51
+
52
+ Next, deploy your application:
53
+
54
+ ``` sh
51
55
gcloud app deploy
52
56
gcloud app browse
53
57
```
54
58
55
59
The last command will open ` https://{YOUR_PROJECT_ID}.appspot.com/ `
56
60
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.
0 commit comments