Search code examples
mysqlnode.jsdigital-oceandatabase-cluster

How to setup and connect my database to NodeJS application on digital ocean


I have managed to create a droplet on Digital-Ocean and managed to clone my Node JS app onto it. Locally , the app connects to MySQL database and I wanted to the same on the live version. Ignorantly, I attempted to create a Managed database cluster which I did and added 1 user account and created 1 database. Right now I do not know how I can import the exported database.sql file into the database since I am only used to phpMyAdmin.

How can I get this to work and connect to my NodeJS app?


Solution

  • First, make sure your database cluster is not open to the outside world by adding a DB firewall using DigitalOcean databases. You can allow connections from your own droplet's private IP address, and your own public address (or VPN or however way you're set up). Once you've done that, you should be able to import your SQL file locally (or from the DO Droplet, as long as you have the mysql client installed):

    mysql -h [host-provided-do] -P [port-provided-do] -u [username-provided-do] -p [db-name-provided-do] < my-file.sql
    

    The most important thing is to make sure your managed database is not open to the outside world, and that you make sure it only allows incoming connections from known IP addresses.

    In your NodeJS app, you can set the driver to connect to the private subnet that DO provides.