Skip to content

Latest commit

 

History

History
 
 

cloudsql

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Node.js Cloud SQL sample on Google App Engine

This sample demonstrates how to use Google Cloud SQL (or any other SQL server) on Google App Engine Flexible.

Setup

Before you can run or deploy the sample, you will need to do the following:

  1. Create a Second Generation Cloud SQL instance. You can do this from the Cloud Console or via the Cloud SDK. To create it via the SDK use the following command:

     gcloud sql instances create [YOUR_INSTANCE_NAME] \
         --activation-policy=ALWAYS \
         --tier=db-n1-standard-1
    

    where [YOUR_INSTANCE_NAME] is a name of your choice.

  2. Set the root password on your Cloud SQL instance:

     gcloud sql instances set-root-password [YOUR_INSTANCE_NAME] --password [YOUR_INSTANCE_ROOT_PASSWORD]
    

    where [YOUR_INSTANCE_NAME] is the name you chose in step 1 and [YOUR_INSTANCE_ROOT_PASSWORD] is a password of your choice.

  3. Create a Service Account for your project. You will use this service account to connect to your Cloud SQL instance locally.

  4. Download and install the Cloud SQL Proxy.

  5. Start the proxy to allow connecting to your instance from your local machine:

     cloud_sql_proxy \
         -dir /cloudsql \
         -instances=[YOUR_INSTANCE_CONNECTION_NAME] \
         -credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON
    

    where [YOUR_INSTANCE_CONNECTION_NAME] is the connection name of your instance on its Overview page in the Google Cloud Platform Console, or use [YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME].

  6. Use the MySQL command line tools (or a management tool of your choice) to create a new user and database for your application:

     mysql --socket [YOUR_SOCKET_PATH] -u root -p
       mysql> create database YOUR_DATABASE;
       mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
       mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';
    

    where [YOUR_SOCKET_PATH] is that socket opened by the proxy. This path was printed to the console when you started the proxy, and is of the format: /[DIR]/[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME].

  7. Set the MYSQL_USER, MYSQL_PASSWORD, MYSQL_SOCKET_PATH, and MYSQL_DATABASE environment variables. This allows the app to connect to your Cloud SQL instance through the proxy.

  8. Update the values in in app.yaml with your instance configuration.

  9. Finally, run createTables.js to ensure that the database is properly configured and to create the tables needed for the sample.

Running locally

Refer to the top-level README for instructions on running and deploying.

It's recommended to follow the instructions above to run the Cloud SQL proxy. You will need to set the following environment variables via your shell before running the sample:

export MYSQL_USER="YOUR_USER"
export MYSQL_PASSWORD="YOUR_PASSWORD"
export MYSQL_SOCKET_PATH="YOUR_SOCKET_PATH"
export MYSQL_DATABASE="YOUR_DATABASE"
npm install
npm start