Paras Patidar
Paras Patidar

Reputation: 45

Setting Up MySQL Database On Google Cloud SQL In Nodejs Application

How to set up MySQL Database On Google Cloud SQL in NodeJs Application ? This will help you out setting up SQL database on google cloud to connect you nodejs application.

Upvotes: 0

Views: 951

Answers (1)

Paras Patidar
Paras Patidar

Reputation: 45

Here's a quick solution...

  1. Open GCP
  2. In the Storage section select SQL
  3. Select MySQL
  4. Enter InstanceId and Password(save it for later use)
  5. Click Show Configuration Options Below and In Connectivity Section, inside the public IP section there will be an option to add network. Include Your Current IP there(Check your IP https://www.whatismyip.com/)
  6. Click Create, It will take little time
  7. Create new database if you want by clicking database section
  8. Create new user if you want by clicking user section, by default user will be root and password we entered earlier
  9. Open Your NodeJs App and select db.config.js file to set up your database configuration
  10. Include This
module.exports = {
    HOST: "<Include Instance IP Here from GCP SQL Dashboard>",
    USER: "root",  //Change user if you need by default root user
    PASSWORD: "<Password>",  // Enter Password you entered while creating instance
    DB: "<DBName>",  //Database name you want to connect with
    // Leave As it is or configure according to your need
    dialect: "mysql",
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
    }
  };
  1. It Will Look Like This
module.exports = {
    HOST: "10.5.25.2",
    USER: "root",
    PASSWORD: "rootPassword",
    DB: "testdb",
    dialect: "mysql",
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
    }
  };

Thanks and if facing any issue reach out to me.

Happy To Help!

Upvotes: 1

Related Questions