MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and setup the MongoDB 4 Community Server on Ubuntu 18.04 LTS. The steps should be the same for other versions of MongoDB including MongoDB 3 and Linux systems.
This tutorial provides the steps required to install MongoDB on the Desktop version of Ubuntu. You can also follow the Ubuntu Server Tutorial published by Tutorials24x7 to install MongoDB on the server.
Notes: We need to start the MongoDB server when required. It won't be added to the startup tasks just like other desktop software.
Download MongoDB
Open the MongoDB Download Page and download the most recent version of MongoDB for Windows as shown in Fig 1.
Click on the Download Button to start the download as highlighted in Fig 1.
Install MongoDB
In this step, we will install MongoDB using the Debian package downloaded by us in the previous step. We can check the previous installation and remove it in case it's already installed on the system using the commands as shown below.
# Check previous installation sudo apt list --installed | grep mongodb
# Take the backup of existing databases
# Completely remove previous installation
sudo apt remove mongodb
sudo apt purge mongodb
Now double click the installer downloaded by us. It will launch the installer as shown in Fig 2.
Click on the Install Button to start installing the MongoDB server. It will show the installation progress as shown in Fig 3 and Fig 4.
Now close the installer after completing the installation.
Verify Installation
In this step, we will verify the installation using the commands as shown below.
# Start the Service sudo service mongod start
# Check version mongod -version # Output db version v4.2.0 git version: a4b751dcf51dd249c5865812b390cfd1c0129c30 OpenSSL version: OpenSSL 1.1.1 11 Sep 2018 allocator: tcmalloc modules: none build environment: distmod: ubuntu1804 distarch: x86_64 target_arch: x86_64
This confirms that we have successfully installed the MongoDB server on the Ubuntu Desktop.
Server Commands
We can start and stop the server as system service using the commands as shown below.
# Start the Service sudo service mongod start
# Restart the Service sudo service mongod restart
# Stop the Service sudo service mongod stop
# Check status sudo service mongod status
The SOCK Issue
In case you see error having the message (code=exited, status=14) on restart or starting it again after stopping as shown below, you can follow the fix as mentioned in this section.
bravo@bravo:~$ sudo service mongod status ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2019-09-24 19:33:56 IST; 3s ago Docs: https://docs.mongodb.org/manual Process: 20291 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14) Main PID: 20291 (code=exited, status=14) Sep 24 19:33:56 bravo systemd[1]: Started MongoDB Database Server. Sep 24 19:33:56 bravo systemd[1]: mongod.service: Main process exited, code=exited, status=14/ Sep 24 19:33:56 bravo systemd[1]: mongod.service: Failed with result 'exit-code'.
Fix the above issue using the commands as shown below.
# Move to temp directory cd /tmp
# Find the mongodb sock file ls *.sock
# Change user and group sudo chown mongodb:mongodb <sock file>
# Start the server sudo service mongod start
# Check status sudo service mongod status
It should show the server status as shown below.
bravo@bravo:/tmp$ sudo service mongod status ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2019-09-24 19:41:25 IST; 5s ago Docs: https://docs.mongodb.org/manual Main PID: 20447 (mongod) CGroup: /system.slice/mongod.service └─20447 /usr/bin/mongod --config /etc/mongod.conf Sep 24 19:41:25 bravo systemd[1]: Started MongoDB Database Server.
Download and Install MongoDB Shell
Similar to the MongoDB server, we will install the MongoDB shell as shown in Fig 5.
It will show the installation progress as shown in Fig 6.
Getting Started With MongoDB
Start the MongoDB Shell as shown in Fig 7.
It shows the warning message indicating that the RBAC is not enabled. MongoDB provides options to create the Authentication Database to store user details and privileges across different databases. The same user having appropriate permissions can act on multiple databases using the Role-Based Access Control (RBAC). The RBAC provided by MongoDB governs access to a MongoDB system.
Now list the databases as shown below.
# List Databases show databases
# Output admin 0.000GB config 0.000GB local 0.000GB
We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.
# Switch to admin database use admin
# Create the admin user with root role db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )
# Output Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
# Exit the Shell exit
This is how we can add admin users to the admin database and assign a role to the user. We can start creating the databases and add data either using the shell or programmatically using the preferred language.
Summary
We have successfully installed the MongoDB Server and MongoDB Shell. We have also connected with the server using shell and added an admin user with the root role.