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
13 changes: 13 additions & 0 deletions .kokoro/cloudsql-mysql.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Format: //devtools/kokoro/config/proto/build.proto

# Set the folder in which the tests are run
env_vars: {
key: "PROJECT"
value: "cloudsql/mysql/mysql"
}

# Tell the trampoline which build file to use.
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-docs-samples/.kokoro/build.sh"
}
92 changes: 92 additions & 0 deletions cloud-sql/mysql/mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Connecting to Cloud SQL - MySQL

## Before you begin

1. If you haven't already, set up a Node.js Development Environment by following the [Node.js setup guide](https://cloud.google.com/nodejs/docs/setup) and
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).

1. Create a 2nd Gen 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.

1. Create a database for your application by following these
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database
name.

1. 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.


1. Use the information noted in the previous steps:
```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>'
export DB_USER='my-db-user'
export DB_PASS='my-db-pass'
export DB_NAME='my_db'
```
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe.

## Running locally

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).

Once the proxy is ready, use the following command to start the proxy in the
background:
```bash
./cloud_sql_proxy -dir=/cloudsql --instances=$CLOUD_SQL_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS
```
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.

Next, setup install the requirements with `npm`:
```bash
npm install
```

Finally, start the application:
```bash
npm start
```

Navigate towards `http://127.0.0.1:8080` to verify your application is running correctly.

## Google App Engine Standard

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

First, update `app.standard.yaml` with the correct values to pass the environment
variables into the runtime.

Next, the following command will deploy the application to your Google Cloud project:
```bash
gcloud app deploy app.standard.yaml
```

To launch your browser and view the app at https://[YOUR_PROJECT_ID].appspot.com, run the following
command:
```bash
gcloud app browse
```

## Deploy to Google App Engine Flexible

First, update `app.flexible.yaml` with the correct values to pass the environment
variables into the runtime.

Next, the following command will deploy the application to your Google Cloud project:
```bash
gcloud app deploy app.flexible.yaml
```

To launch your browser and view the app at https://[YOUR_PROJECT_ID].appspot.com, run the following
command:
```bash
gcloud app browse
```

30 changes: 30 additions & 0 deletions cloud-sql/mysql/mysql/app.flexible.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2019, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: nodejs
env: flex

# The following env variables may contain sensitive information that grants
# anyone access to your database. Do not add this file to your source control.
env_variables:
DB_USER: MY_DB_USER
DB_PASS: MY_DB_PASSWORD
DB_NAME: MY_DATABASE
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
CLOUD_SQL_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>

beta_settings:
# The connection name of your instance, available by using
# 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
# the Instance details page in the Google Cloud Platform Console.
cloud_sql_instances: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>
23 changes: 23 additions & 0 deletions cloud-sql/mysql/mysql/app.standard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2019, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: nodejs8

# The following env variables may contain sensitive information that grants
# anyone access to your database. Do not add this file to your source control.
env_variables:
DB_USER: MY_DB_USER
DB_PASS: MY_DB_PASSWORD
DB_NAME: MY_DATABASE
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
CLOUD_SQL_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>
64 changes: 64 additions & 0 deletions cloud-sql/mysql/mysql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "cloudsql-mysql-mysql",
"description": "Node.js Cloud SQL MySQL Connectivity Sample",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8"
},
"scripts": {
"unit-test": "ava --verbose test/*.test.js",
"start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -dir=/cloudsql -instances=$CLOUD_SQL_INSTANCE_NAME &",
"system-test": "repo-tools test app -- server.js",
"system-test-proxy": "npm run start-proxy; npm run system-test",
"all-test": "npm run unit-test && npm run system-test",
"test": "repo-tools test run --cmd npm -- run all-test"
},
"dependencies": {
"@google-cloud/logging-winston": "^0.10.2",
"body-parser": "1.18.3",
"express": "4.16.2",
"promise-mysql": "^3.3.1",
"prompt": "1.0.0",
"pug": "2.0.3",
"winston": "^3.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "3.1.0",
"ava": "0.25.0",
"proxyquire": "^2.1.0",
"supertest": "^3.3.0",
"sinon": "^7.1.1"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"test": {
"app": {
"requiredEnvVars": [
"DB_USER",
"DB_PASS",
"DB_NAME",
"CLOUD_SQL_INSTANCE_NAME"
],
"args": [
"server.js"
]
},
"build": {
"requiredEnvVars": [
"DB_USER",
"DB_PASS",
"DB_NAME",
"CLOUD_SQL_INSTANCE_NAME"
]
}
}
}
}
Loading