Sunday, December 29, 2024
Google search engine
HomeData Modelling & AIHow To Install MongoDB 5.0 on Debian 11 / Debian 10

How To Install MongoDB 5.0 on Debian 11 / Debian 10

This guide provides a systematic demonstration of how to Install MongoDB 5.0 on Debian 11/ Debian 10 Linux systems. MongoDB is a general-purpose, object-oriented, simple, and dynamic database. It is a NoSQL document store. This means that data objects are stored as separate documents in a collection. This helps to overcome the relational databases approach. MongoDB is written in C++ for massive scalability and high performance. It is available for Windows, macOS, and Linux Operating Systems and is supported by both 32 and 64-bit architectures. MongoDB 5.0 brings new features such as:

  • Seamless data redistribution
  • Multi cloud-security tools
  • Serverless database on MongoDB Atlas
  • Native time-series features
  • Live Resharding
  • The Versioned API future-proofs your applications.

MongoDB is good for the following:

  • Evolving data requirements.
  • Blogs and content management.
  • Mobile and social networking sites
  • E-commerce product catalogue.
  • configuration management
  • Real-time analytics and high-speed logging

In MongoDB, there is a collection of concepts we need to be aware of. These are:

SQL Server MongoDB
Index Index
Database Database
Column Field
Joining Linking & Embedding
Row Document
Partition Sharding (Range Partition)
Replication ReplSet
Table Collection

Step 1: Add MongoDB APT repository

In this guide, we will install MongoDB 5.0 using the apt manager. Here we will use the official mongo-org package. This package is supported and maintained by MongoDB Inc. Mongo-org package always contains the latest available versions of MongoDB.

First, we import the MongoDB public GPG key. Download it using wget as below. Install wget on Debian 11/ Debian 10 using sudo apt install wget

curl -fsSL https://www.mongodb.org/static/pgp/server-5.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-5.gpg

This code should respond with OK as the output. However, if you get an error indicating that GnuPG is not installed, install it as below.

sudo apt update
sudo apt install gnupg2

The next step requires us to create a /etc/apt/sources.list.d/mongodb-org-5.0.list file for MongoDB 5.0. We will use the buster’s repo for both distros since, by the time of documenting this article, the one for bullseye did not exist.

echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Then update your local package database as below.

$ sudo apt update
Hit:1 http://deb.debian.org/debian buster InRelease                 
Hit:2 http://security.debian.org/debian-security buster/updates InRelease
Hit:3 http://deb.debian.org/debian buster-updates InRelease
Ign:4 http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 InRelease
Get:5 http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 Release [2,396 B]
Get:6 http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 Release.gpg [801 B]
Get:7 http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0/main amd64 Packages [6,551 B]
Fetched 9,748 B in 3s (3,461 B/s)
Reading package lists... Done

Step 2: Install MongoDB 5.0

In this guide, we will install a specific release i.e 5.0.2 and therefore we will specify each component package with the version number as below.

sudo apt install mongodb-org mongodb-org-database mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools

If you choose to install mongodb-org=5.0.2without specifying components’ version, the latest version of each component will be installed.

Dependency tree of the above command:

Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database-tools-extra
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 2 not upgraded.
Need to get 147 MB of archives.
After this operation, 449 MB of additional disk space will be used.

With MongoDB 5.0 installed, we are now set to make a few configurations to our system. First, start and enable the mongod service on Debian 11/ Debian 10 as below.

sudo systemctl start mongod
sudo systemctl enable mongod

Check the installed MongoDB version.

$ mongod --version
db version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1k  25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 3: Configure MongoDB

Verify if the service is running:

$ systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-08-21 07:49:24 EDT; 39s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 5029 (mongod)
   Memory: 71.4M
   CGroup: /system.slice/mongod.service
           └─5029 /usr/bin/mongod --config /etc/mongod.conf

Aug 21 07:49:24 ns1.neveropen.local systemd[1]: Started MongoDB Database Server.
Aug 21 07:49:29 ns1.neveropen.local systemd[1]: /lib/systemd/system/mongod.service:11: PIDFile= references path below legacy directory /var/run....

Secure MongoDB 5.0 Instance.

After installation, the MongoDB database has no security, One can access it without a password and make undesired changes to your database. Therefore, we need to secure this database by creating a username and password. Connect to the database by issuing the code:

$ mongo

Then add a user named mongouser, remember you can change this name to your desired one. with the below script added, you are required to set a password for the user.

use admin
db.createUser(
{
user: "mongouser",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

Sample output for the script.

> use admin
switched to db admin

> db.createUser(
... {
... user: "mongouser",
... pwd: passwordPrompt(), // or cleartext password
... roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Enter password:
Successfully added user: {
	"user" : "mongouser",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		},
		"readWriteAnyDatabase"
	]
}
> exit
bye

Next, edit the MongoDB configuration file and enable authentication.

sudo apt install vim
sudo vim /etc/mongod.conf

In the file, find the #security line, uncomment it then add the authentication. Ensure you add double space before authorization since the syntax is very important here. Your file should appear as below.

security:
  authorization: enabled

For the changes made to apply, restart the MongoDB service.

sudo systemctl restart mongod

Now clients need to authenticate themselves to access the database. The syntax used is as below.

mongo --port 27017 --authenticationDatabase "admin" -u "your-user" -p

Step 4: Using MongoDB 5.0 on Debian

MongoDB listens on a default port 27017. From the localhost connect to the created user using

mongo -u mongouser -p --authenticationDatabase admin

Enter the password you created earlier to connect to your database.

There are a couple of things you can do while in the MongoDB shell. Among them are:

List available databases in MongoDB.

> db
test

Create a database in MongoDB.

In MongoDB, database creation is done by switching to a non-existing database and specify the name of the database. The specified name becomes the name of the new database. Let us create a new database say mongotestdb

use mongotestdb

Sample Output:

> use mongotestdb
switched to db mongotestdb
> 

Create a collection in MongoDB

Now you can add data to your database. Here we are creating a table for user details.

db.userdetails.insertOne(
   { "F_Name" : "fname",
     "L_NAME" : "lname",
     "ID_NO" : "12345",
     "AGE" : "19",
     "TEL" : "+254654671"
   }
)

Show collections/tables in MongoDB database:

show collections

Sample Output:

> show collections
userdetails
> 

Create a User with read and write privileges.

Here let us create a database testdatabase and assign read and write privileges to a given user let’s say testuser.

use testdatabase

Then assign permissions to the created database(testdatabase)

db.createUser(
   {
     user: 'testuser',
     pwd: 'P@ssWord',
     roles: [ { role: 'readWrite', db: 'testdatabase' } ]
   }
 );

Use a specific database in MongoDB.

In case you want to use a specific database in MongoDB, run the command.

use database-name

Create an admin for a specific database in MongoDB.

Let us use the created database(testdatabase)

use testdatabase

Create an admin for the database.

db.createUser(
  {
    user: 'testadmin',
    pwd: 'P@ssW0rd',
    roles: [ { role: 'userAdmin', db: 'testdatabase' } ]
  }
);

Create an Overall Admin in MongoDB.

This step will work if you didn’t create an overall admin at first. But in this guide, we already created an overall admin with the name monguser

use newdatabase

Assign privileges and a password as below.

db.createUser(
{
user: "Your Username",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "newdatabase" }, "readWriteAnyDatabase" ]
}
)

Set your preferred password. Then you can connect to the user as we did earlier.

Step 5: Change MongoDB default Path

The default path used as a storage for MongoDB data is /var/lib/mongo. However, this directory can be altered as below.

  1. Stop the MongoDB service.
sudo systemctl stop mongod.service

2. Create a new directory for MongoDB data.

sudo mkdir -p /data/neveropen/mongo

Set the directory to be owned by MongoDB.

sudo chown -R mongodb:mongodb /data/neveropen/mongo

3. Copy contents to the new directory. Install Rsync using sudo apt install rsync

sudo rsync -av /var/lib/mongodb /data/neveropen/mongo

4. Rename the old directory for backup.

sudo mv /var/lib/mongodb /var/lib/mongodb.bak

5. Create a symbolic link to the new location.

sudo ln -s /data/neveropen/mongo /var/lib/mongodb

With these changes made, restart the MongoDB service. MongoDB will start using the new directory created to store its data.

sudo systemctl start mongod.service

Conclusion

We have come to the end of this systematic illustration on how to install MongoDB 5.0 on Debian 11 / Debian 10. I have also demonstrated a few configurations and how to use MongoDB. I hope you found it significant.

MongoDB Udemy Video courses

See more guides:

RELATED ARTICLES

Most Popular

Recent Comments