/ security

Installing Mongodb Ubuntu 16.04

See Official MongoDB install docs for all platforms.

See getting started with MongoDB for an overview of what MongoDB is.

1. Import Key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

2. Create List file

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

3. Reload package database

sudo apt-get update

4. Install Mongoddb packages

We install a specific version of Mongdodb (3.6.2) here because then we know exactly what we're dealing with. Just pulling the latest version is great, but knowing what version your deployments are running can reduce ambiguity and bug chasing.

sudo apt-get install -y mongodb-org=3.6.2 mongodb-org-server=3.6.2 mongodb-org-shell=3.6.2 mongodb-org-mongos=3.6.2 mongodb-org-tools=3.6.2

Pin a specific version of MongoDB.

This prevents apt-get upgrade from auto upgrading mongodb. There's two sides to this, it's great we've pinned the version and prevented auto updates- which might prevent breakage. But who's in control of patch managements and making sure you're up to date on that. Have a plan.

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Note the official install docs point out all packages need to be marked as hold to keep them from being updated.

MongoDB default directory on Ubuntu

Now you've installed MongoDB, good to know where everything is.

  • Data files:
    • /var/lib/mongodb
  • Log files:
    • /var/log/mongodb
  • The MongoDB process / daemon runs as mongod.

Verify MongoDB Installation

1. Start your MondoDB instance

sudo service mongod start

2. Verify it started successfully

Use tail to look at the end of the log:

tail -f /var/log/mongodb/mongod.log

On the end of the log file you should see somthing like:

2018-01-13T16:37:26.954+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data' 2018-01-13T16:37:26.954+0000 I NETWORK [initandlisten] waiting for connections on port 27017

Specifically, "waiting for connections on port 27017".

3. How to stop/restart MondoDB

  • Stop:
    • sudo service mongod stop
  • Restart:
    • sudo service mongod restart

4. Connect to MongoDB cli

mongo --host 127.0.0.1:27017

Will give you a shell, becaue MongoDB listens on the loopback address (127.0.0.1) by default.

To purge and remove MongoRB, see the end of the official mongoDB install docs.

Done. Now why not deploy a replica set with authentication!?