Import the keys and add the mongoDB lists to the source lists so that everytime you will do an apt-get update it will check if new versions of any Mongodb modules exist.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
The command below will load the source lists in which there are the mongoDB packages.
sudo apt-get update
sudo apt-get install -y mongodb-org
In case you don’t want a mongodb module to be automatically updated when you type a “apt-get update”, you can disable the update of a module by typing one of the directives below.
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
Now it is time to add the mongod service to the system.
sudo systemctl enable mongod
sudo systemctl start mongod
Let’s check if it is working. Mongodb listens on the 27017 port. Type the command below and you should see in the list something like LISTEN 127.0.0.1:27017
ss -ltpn
When you install MongoDB, there is no admin user and no authentication. You must set it up manually. Open the terminal and start a mongo session:
mongo
use admin
db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})
Now go to the service file of the mongo server module and add the –auth parameter to ExecStart=/usr/bin/
sudo nano /lib/systemd/system/mongod.service
Then, type
Now to open a mongo session and login as admin with a password
mongo admin -u mongoadmin -p 'mongoadmin'