How to install MongoDB on Ubuntu 18.04

s2 0

MongoDB is a free, open source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.

MongoDB stores data in flexible, JSON-like
Documents where fields can vary from document to document. It does not require a predefined schema and the data structure can change over time.

In this tutorial, we cover the process of installing and configuring the latest version of MongoDB Community Edition on an Ubuntu 18.04 computer from the official MongoDB repositories.

requirements

Before proceeding with this tutorial, make sure that you are logged in as a user with sudo permissions.

Install MongoDB

At the time of this writing, the latest version of MongoDB is version 4.0.

Before proceeding with the installation process, go to the Install on Ubuntu
Section of MongoDB documentation and see if a new version is available.

The following steps describe how to install MongoDB on your Ubuntu server:

  1. Add the MongoDB GPG key to your system with the following command:

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

    After the key is imported, add the MongoDB repository with:

    sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

    If you get an error message that says add-apt-repository command not found, install that software-properties-common Package.

    To install an older version of MongoDB, replace 4.0 with your preferred version.

  2. Update the package list and install the mongodb-org meta package by entering:

    sudo apt updatesudo apt install mongodb-org

    The following packages will be installed on your system as part of the mongodb-org Package:

    • mongodb-org-server – The mongod Daemon and corresponding init scripts and configurations.
    • mongodb-org-mongos – The mongos Demon.
    • mongodb-org-shell – The Mongo Shell is an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks from the command line.
    • mongodb-org-tools – Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
  3. Once the installation is complete, start the MongoDB daemon and enable it to start at boot by typing:

    sudo systemctl start mongodsudo systemctl enable mongod
  4. To check that the installation has been completed successfully, we will connect to the MongoDB database server via the mongo Tool and print the connection status:

    mongo --eval 'db.runCommand({ connectionStatus: 1 })'

    The output looks like this:

    MongoDB shell version v4.0.10
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 4.0.10
    {
      "authInfo" : {
        "authenticatedUsers" : [ ],
        "authenticatedUserRoles" : [ ]
      },
      "ok" : 1
    }

    A value of 1 for the ok Field indicates success.

Configure MongoDB

MongoDB uses a YAML
formatted configuration file, /etc/mongod.conf . You can configure your MongoDB instance by editing this file.

The default configuration settings are sufficient for most users. However, for production environments, it is recommended to uncomment the security section and enable authorization as shown below:

/etc/mongod.conf
security:
  authorization: enabled

the authorization Option activated Role Based Access Control (RBAC)
governing user access to database resources and operations. If this option is deactivated, every user has access to all databases and can take any action.

After making changes to the MongoDB configuration file, restart the mongod service with:

sudo systemctl restart mongod

For more information about the configuration options available in MongoDB 4.0, see Configuration file options
Documentation page.

Create a MongoDB administrative user

If you’ve enabled MongoDB authentication, create a MongoDB administrative user that will be used to access and manage the MongoDB instance.

First access the Mongo Shell with:

mongo

Once you’re in the MongoDB shell, enter the following command to connect to the admin Database:

use admin
switched to db admin

Issue the following command to create a new user named. to create mongoAdmin with the userAdminAnyDatabase Role:

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
You can name the MongoDB administrative user whatever you want.

Exit the Mongo Shell with:

quit()

To test the changes, access the Mongo shell with the administrator user created earlier:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin

Now print the users with:

show users
{
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

You can also try accessing the Mongo shell with no arguments (just type mongo) and see if you can list the users using the same commands as above.

diploma

You learned how to install and configure MongoDB 4.0 on your Ubuntu 18.04 server. visit the MongoDB 4.0 manual
for more information on this.

If you run into any problem or have any feedback, please leave a comment below.