Skip to content

Instantly share code, notes, and snippets.

@sunlee-newyork
Last active January 8, 2017 23:25
Show Gist options
  • Save sunlee-newyork/d797823582e59a532a3e3b92e492f023 to your computer and use it in GitHub Desktop.
Save sunlee-newyork/d797823582e59a532a3e3b92e492f023 to your computer and use it in GitHub Desktop.

Deploy a Remote DigitalOcean MongoDB Server

Table of Content

  1. Introduction
  2. Droplet Setup
  3. MongoDB Setup
  4. Firewall Setup

Introduction

This is a working instructional to deploy a Mongo 3.2.7 remote database with the following (as of January 8th, 2017):

  • DigitalOcean Ubuntu 14.04 One-click Mongo app
  • DB Connection Authentiation
  • Server Firewall

The following are missing from this instructional:

  • SSL setup
  • Clusters/Sharding

This is Part 2 of the Meteor/MongoDB deployment instructions where we'll setup a remote Mongo database to use for our production Meteor application. Meteor setup instructions as Part 1 can be found here.

Although the intent is to get it set up for the Meteor app, you can still basically use the entirety of this guideline for any other application.

Please feel free to leave comments, questions, or edits as you see fit! I am by no means claiming to be an expert in anything. I just wanted to share a truly end-to-end working solution. Cheers!

Droplet Setup

  1. Login to DigitalOcean
  2. Create a new droplet
  3. Select "One-click Apps"
  4. Select "MongoDB 3.2.7 on 14.04"
  5. ssh root@xxx.xxx.xx.xxx

MongoDB Setup

  1. Shutdown current mongod process:
$ mongo
> use admin;
> db.shutdownServer();
  1. mkdir /data; mkdir /data/db1
  2. Start Mongo daemon without auth: mongod --port 27017 --dbpath /data/db1
  3. Open a new terminal tab and add user:
$ mongo
> use admin;
> db.createUser({user: "your_username", pwd: "your_password", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]});
> db.auth("your_username", "your_password"); // login
  1. Create a separate database for your app:
> use yourappname;
> db.createUser({user: "your_username", pwd: "your_password", roles: ["dbOwner"]});
> db.auth("your_username", "your_password");
  1. Shutdown mongod in previous tab: mongod --shutdown --dbpath /data/db1
  2. sudo nano /etc/mongod.conf and edit the following:
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /data/db // <- HERE
  journal:
    enabled: true
#  engine: wiredTiger

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

processManagement: // <- HERE
  fork: true // <- HERE
  pidFilePath: /var/log/mongodb/mongod-pid.log // <- HERE

# network interfaces
net:
  port: [5_DIGIT_PORT_#_OF_CHOICE] // <- HERE
#  ssl:
#    mode: requireSSL
#    PEMKeyFile: /etc/ssl/mongodb.pem
#    CAFile: /etc/ssl/mongodb-cert.crt
#    allowInvalidCertificates: true
  bindIp: 0.0.0.0 // <- HERE

security: // <- HERE
  authorization: enabled // <- HERE

operationProfiling: // <- HERE
  mode: slowOp // <- HERE

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Firewall Setup

  1. Add input/output firewall settings for production server. If you want to add more addresses, enter separately as needed:
iptables -A INPUT -s xxx.xxx.xx.xxx -p tcp --destination-port YOUR_PORT_# -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d xxx.xxx.xx.xxx -p tcp --source-port YOUR_PORT_# -m state --state ESTABLISHED -j ACCEPT

(Here is where you'll want to add your Meteor DigitalOcean droplet IP address if you're also following [these instructions)[https://gist.github.com/sunlee-newyork/b4d2a57fd2bb44fdf5f8b7e0f0a5ff52].)

  1. Check iptables: sudo iptables -L --line-numbers
  2. If you need to delete an accidental entry, get the line number and run: sudo iptables -D INPUT 1 where INPUT is either INPUT or OUTPUT, and 1 is the line number.
  3. Run forked mongo daemon: mongod --config /etc/mongod.conf

References

  1. [MongoDB - Enable Auth](https://docs.mongodb .com/manual/tutorial/enable-authentication/)
  2. MongoDB - db.createUser()
  3. MongoDB - db.updateUser()
  4. MongoDB - Users
  5. MongoDB - Manage Users and Roles
  6. MongoDB - Enable Auth
  7. MongoDB - Manage mongod Processes
  8. MongoDB - Configure Linux iptables Firewall for MongoDB
  9. MongoDB - Connection String URI Format
  10. How To Use the MongoDB One-Click Application
  11. How to Install MongoDB on Ubuntu 16.04
  12. How to secure MongoDB with username and password
  13. Deploy ignoring external mongodb / url
  14. How To List and Delete Iptables Firewall Rules
  15. How do I specify mongodb's config file?
  16. MongoDB “root” user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment