Codementor Events

How to Install a Proper Mongo Server in Ubuntu 16 in AWS EC2

Published May 05, 2018
How to Install a Proper Mongo Server in Ubuntu 16 in AWS EC2

I will be using mongo version 3.4. I will try to set up MongoDb server on EC2.

Importing the Public Key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create source list file MongoDB

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Replace 3.4 with the version you want for mongo

Update the repository

sudo apt-get update

Installing MongoDB

sudo apt-get install -y mongodb-org  --allow-unauthenticated

Start MongoDB and add it as a service to be started at boot time

sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl enable mongod

Configure MongoDB username and password

export LC_ALL=C
mongo
use admin
db.createUser({user:"admin", pwd:"password123", roles:[{role:"root", db:"admin"}]})

Exit out of mongo client using Ctrl+C

Enable mongodb authentication

sudo vim /lib/systemd/system/mongod.service

In this file you will see a a line like this

ExecStart=/usr/bin/mongod — config /etc/mongod.conf

Replace this with

ExecStart=/usr/bin/mongod — auth — config /etc/mongod.conf

Save and exit

Reload System Service

sudo service mongod restart

Now check that MongoDB has been started on port 27017 with the netstat command.

netstat -plntu

Launch mongo with auth

mongo -u admin -p password123 --authenticationDatabase admin
Discover and read more posts from Raj Nandan Sharma
get started
post commentsBe the first to share your opinion
Show more replies