-
Notifications
You must be signed in to change notification settings - Fork 2k
Added Cloud SQL MySQL connectivity samples for mysql library. #1104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
510f4c7
Added Cloud SQL MySQL connectivity samples for Node.js mysql libary.
kurtisvg 55d59c8
Address some lint errors.
kurtisvg a38e431
Fix remaining lint issues.
kurtisvg 7c2ca2d
Address feedback.
kurtisvg f04e284
Added kokoro config.
kurtisvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.