How to setup MongoDB Server on Ubantu

Click here for Govt Schemes

How to setup MongoDB Server on Ubantu

Introduction

MongoDB is a document database used in many modern web applications. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure.

Instead, it uses JSON-like documents with dynamic schemas, meaning that, unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to set up a new database with an updated schema.

In this tutorial you’ll install MongoDB on an Ubuntu 20.04 server, test it, and learn how to manage it as a systemd service.

Platform Support

MongoDB 7.0 Community Edition supports the following 64-bit Ubuntu LTS (long-term support) releases on x86_64 architecture:

  • 22.04 LTS ("Jammy")

  • 20.04 LTS ("Focal")

MongoDB only supports the 64-bit versions of these platforms. To determine which Ubuntu release your host is running, run the following command on the host's terminal:

cat /etc/lsb-release

MongoDB 7.0 Community Edition on Ubuntu also supports the ARM64 architecture on select platforms.

Install MongoDB Community Edition

Follow these steps to install MongoDB Community Edition using the apt package manager.

1. Import the public key used by the package management system

From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

To import the MongoDB public GPG key, run the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor

2. Create a list file for MongoDB

Create the list file /etc/apt/sources.list.d/mongodb-org-7.0.list for your version of Ubuntu.
 

Ubuntu 20.04 (Focal)

Create the /etc/apt/sources.list.d/mongodb-org-7.0.list file for Ubuntu 20.04 (Focal):

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

3. Reload local package database

Issue the following command to reload the local package database:

sudo apt-get update

4. Install the MongoDB packages

You can install either the latest stable version of MongoDB or a specific version of MongoDB.

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

Optional. Although you can specify any available version of MongoDB, apt-get will upgrade the packages when a newer version becomes available. To prevent unintended upgrades, you can pin the package at the currently installed version:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

To run and manage your mongod process, you will be using your operating system's built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

If you are unsure which init system your platform uses, run the following command:

ps --no-headers -o comm 1

Then select the appropriate tab below based on the result:

  • systemd - select the systemd (systemctl) tab below.

  • init - select the System V Init (service) tab below.

--------------------------------------------------------------------------------------------------------------------------------------------

1. Start MongoDB.

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod:

Failed to start mongod.service: Unit mongod.service not found.

Run the following command first:

sudo systemctl daemon-reload

Then run the start command above again.

2. Verify that MongoDB has started successfully.

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

3. Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod

4. Restart MongoDB.

You can restart the mongod process by issuing the following command:

sudo systemctl restart mongod

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

5. Begin using MongoDB.

Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

How to create User :
 

root@server:~# mongosh
Current Mongosh Log ID: XXXXXXXXXXXXXXXXXXXXXXX
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.6
Using MongoDB:          7.0.11
Using Mongosh:          2.2.6

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2024-06-05T07:28:51.344+02:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2024-06-05T07:28:51.515+02:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2024-06-05T07:28:51.515+02:00: vm.max_map_count is too low
------

test> use admin
switched to db admin
admin> db.createUser(
... {
... user: "username",
... pwd: "password",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
... }
... )
{ ok: 1 }
admin>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
admin> exit

 



Share on Pinterest
Share on LinkedIn
Share on WhatsApp
Share on Telegram