To advertise with us contact on Whatsapp: +923041280395 For guest post email at: itsaareez1@gmail.com

MongoDB on Ubuntu 20.04 Server – Alibaba Cloud

MongoDB on Ubuntu 20.04 Server – Alibaba Cloud

MongoDB is a free and 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.

In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and data structure can be changed over time.

This tutorial describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.

The standard Ubuntu repositories include an outdated MongoDB version. Installing the latest MongoDB on Ubuntu is fairly straightforward. We’ll enable the MongoDB repository, import the repository GPG key, and install the MongoDB server.

Prerequisites

  • You must be registered with Alibaba Cloud.
  • You must have added and verified your payment method.
  • If you are a new user, after payment method verification you can claim free $450 – $1300 credits. You can register new account and claim your free credits.
  • To setup up your ECS for the first time, you can refer to this tutorial or quick-start guide.
  • Domain registration is recommended for setting up FQDN hostname of your server. But if you are willing to use IP address directly, you may skip this.
  • If you have registered domain with any 3rd party, you may transfer into Alibaba Cloud.
  • If you are using the domain name, remember to point your domain name to IP address of your server.
  • You should set up your server’s hostname.
  • Access to VNC console in your Alibaba Cloud or SSH client installed in your PC.
  • Login as root user and create a user with sudo privileges.

Installing MongoDB on Ubuntu 20.04

Perform the following steps as root or user with sudo privileges to install MongoDB on Ubuntu:

Install the dependencies necessary to add a new repository over HTTPS:

$ sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
Import the repository’s GPG key and add the MongoDB repository with:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

At the time of writing this article, the latest version of MongoDB is version 4.4. To install another version, replace 4.4 with your preferred version.

Once the repository is enabled, install the mongodb-org meta-package by typing:

$ sudo apt install mongodb-org

The following packages will be installed on your system:

  • mongodb-org-server – The mongod daemon and corresponding init scripts and configurations.
  • mongodb-org-mongos – The mongos daemon.
  • mongodb-org-shell – The mongo shell, an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks thought the command line.
  • mongodb-org-tools – Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.

Start the MongoDB daemon and enable it to start on boot by typing:

$ sudo systemctl enable --now mongod

To verify whether the installation has completed successfully, connect to the MongoDB database server using the mongo tool, and print the connection status:

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

The output will look something like below:

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: 4.4.0
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}

A value of 1 for the ok field indicates success.

Configuring MongoDB

The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format.

The default configuration settings are sufficient in most cases. However, for production environments, we recommend uncommenting the security section and enabling authorization, as shown below:

$ sudo nano /etc/mongod.conf

Update the following lines.

security:
authorization: enabled
The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to all databases and perform any action.

When editing the MongoDB configuration file, restart the mongod service for changes to take effect:

$ sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.4, visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance.

Access the mongo shell:

$ mongo

From inside the MongoDB shell type the following command to connect to the admin database:

$ use admin

Run the following command to create a new user named mongoAdmin, with password changeMe and userAdminAnyDatabase role:

db.createUser(
  {
    user: "mongoAdmin",
    pwd: "changeMe",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Once done, exit the mongo shell with:

quit()

To test the changes, access the mongo shell using the administrative user you have previously created:

$ mongo -u mongoAdmin -p --authenticationDatabase admin
use admin

Run show users and you should see information about the newly created user:

show users

You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.

Conclusion

We’ve shown you how to install and configure MongoDB on Ubuntu 20.04. For more information on this topic, visit the MongoDB Manual .

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *