blog posts

How to install MongoDB database in Linux

The amount of information is staggering! The range of data in the computer world has increased to such an extent that it is impossible to manage and interact with big data through ordinary databases. This data needs to have a more detailed analysis, and therefore, we will need NoSQL databases. In this blog post, we intend to teach how to install the MongoDB database, which is one of the most famous of these databases.

To learn more about this database, you can read the article What is MongoDB?

Thanks to these databases (such as MongoDB), information becomes scalable and can be accessed with greater performance. In this case, the MongoDB database supports popular platforms such as Windows, Linux, and Macintosh and is fully compatible with most programming languages.

First, let’s get to know this database a little more.

General information about MongoDB

This tutorial covers installing the commercial version of MongoDB (MongoDB Enterprise) in GNU/Linux Red Hat Enterprise or CentOS versions 6 and 7 through rpm packages. In this tutorial, we only refer to 64-bit systems.

Packages

MongoDB officially supports enterprise packages in its repositories. These repositories include the following packages:

Explanation of the function of the package Package name
This package automatically installs the following four packages. MongoDB-enterprise
Contents of mongod daemon and related settings and scripts MongoDB-enterprise-server
Mongos Daemon content MongoDB-enterprise-mangos
Mongo Shell content MongoDB-enterprise-shell
Contains mongoimport tools braindump, mongodump, mango export, mongo files, mongorestore, mongostat, and mono top. mongodb-enterprise-tools

The settings are default applied to the IP address 127.0.0.1 in /etc/mongod.conf, which you must change before installation.

Mongo DB packages

In the next section, we will teach you how to install MongoDB on Centos7.

How to install MongoDB on Centos7

Centos7 is one of the most popular Linux distributions. Next, you’ll learn how to install a MongoDB database on this distribution.

First, log into your CentOS 7 Linux terminal.

Because the MongoDB package is not in the default Repository list of centos7, you must first install its Repository list by running the following command.

 nano  /etc/yum.repos.d/mongodb-org.repo

Then, please copy the following information into it and save the file.

[mongodb-org-4.0]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Next, check your Linux repository list with the following command.

yum repolist

In the output of the above command, you should see the name of the repository you added.

After completing the above steps, enter the following command to install MongoDB.

 yum install mongodb-org

After executing this command, the installation of the package will start. Be careful that the installation and download confirmation will be taken from you during the installation process! This way, you will be asked a question to which you must choose Yes.

Be sure to read:   What points to pay attention to when transferring a website domain name?

Wait for MongoDB to finish installing.

systemctl start mongod

systemctl reload mongod

Note that MongoDB can only be installed on servers outside of Iran.

Now everything is ready to install the next packages.

Install MongoDB Enterprise Edition

database In this section, we will look at different methods of installing MongoDB Enterprise :

Installation through the rpm package.

This method is done using the described packages. These packages automatically install all the necessary dependencies, so we recommend that you install the MongoDB Enterprise version in this way.

If you intend to use this method, do the following steps in order:

1. Configure the repository

Create a file with the name and address /etc/yum.repos.d/MongoDB-enterprise.repo using the Yum command: in the path mentioned so that you can install MongoDB Enterprise

[mongodb-enterprise]name=MongoDB Enterprise Repositorybaseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.0/$basearch/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

nosql databases

2. Installing packages

In the following, we will teach the installation instructions for each package separately.

Install MongoDB Enterprise version 4.0

Run the following command:

sudo yum install -y mongodb-enterprise

Install a custom version of MongoDB Enterprise

To install a specific version of MongoDB Enterprise, you must specify each package separately and its version. To make this clearer, take a look at this example:

sudo yum install -y mongodb-enterprise-4.0.3 mongodb-enterprise-server-4.0.3 mongodb-enterprise-shell-4.0.3 mongodb-enterprise-mongos-4.0.3 mongodb-enterprise-tools-4.0.3

Suppose you installed MongoDB version 4.0.3 and did not include component packages. In that case, the latest version of MongoDB packages will be installed regardless of the version you specified. If you don’t want this to happen, read the next section.

Prevent installation of new versions of MongoDB Enterprise

As mentioned, you can install any of the available versions of MongoDB Enterprise; on the other hand, Yum will update the installed packages to newer versions if there is a new package. You can add the exclude command file to the /etc/yum to prevent this package upgrade process.con f :

exclude=mongodb-enterprise,mongodb-enterprise-server,mongodb-enterprise-shell,mongodb-enterprise-mongos,mongodb-enterprise-tools

The manual installation process of MongoDB via zip files

If you are going to install Mongo DB manually, use the following methods:

Install via the zip files.

.tar.gz zip files, To install MongoDB, you need to install the following dependencies:

Red Hat Enterprise version 6:

yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs libcurl libpcap net-snmp openldap openssl

Red Hat Enterprise version 7:

yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl libpcap lm_sensors-libs net-snmp net-snmp-agent-libs openldap openssl rpm-libs tcp_wrappers-libs

Then do the following in order:

1. Download MongoDB Enterprise compressed files in .tar.gz format
After installing the prerequisite packages, refer to this address to download the link to the compressed files of MongoDB.
2. Open the zip files you downloaded.
To open these files, follow the example below using the tar command :

tar -zxvf mongodb-linux-*-4.0.3.tgz

3. Make sure the binary files are in the target directory.

Binaries MongoDB must be located in the /bin directory after they are unzipped; otherwise, do the following:

  • Copy the binary files into the directory in the path, for example: /usr/local/bin
  • Create symbolic links for each of these binary files from the directory located in the path.
  • Change the path to include the directory you want.
  • For example, you can add the following line to the beginning of the executable script (bashrc./):
export PATH=<mongodb-install-directory>/bin:$PATH

Change the MongoDB-install-directory to the path you defined and continue the process of extracting the Mongo DB zip files.

Be sure to read:   MailEnable security issue

Apply the necessary MongoDB Enterprise settings

Now it’s time to apply the necessary settings to Mongo DB.

SELinux setting

Important note:

If you are using SELinux, you must configure it to allow running and booting on Red Hat Enterprise or CentOS distributions. To configure SELinux, there are three options available to the system administrator:

If SELinux is in enforcing mode, you must grant access to the corresponding ports for Mongo DB to use (for example, 27017). Run the following command to apply the default settings:

semanage port -a -t mongod_port_t -p tcp 27017

Needless to say, after executing this command, the system must be restarted to apply the commands.

Set SELinux settings in /etc/selinux/config mode to Permissive; this is done by running the following command:

SELINUX=permissive

To apply this command, the system must be restarted.

Using set enforce also changes SELinux to Permissive mode, and this does not require a restart, but the result of this method is not permanent.

On the other hand, you can choose not to install the SELinux package during the installation of the GNU/Linux distribution or to remove the relevant packages; Of course, this work has its problems and is not recommended.

Directory data and permissions

Warning:
If you change the data installation path in Red Hat version 7.0, the default SELinux policies do not prevent mongod from accessing the new path; Unless you change the security settings.

By default, MongoDB instances are located in these paths:
Data files at /var/lib/mongo
Log files at /var/log/mongo

If you installed MongoDB through the package manager, these directories would be created during installation. Still, if you have started Mongo DB through its installer zip files, you can create these directories with <mkdir -p <directory or sudo mkdir -p (directory) commands, depending on where you plan to run Mongo DB.

By default, MongoDB uses the MongoDB account; If you want to change its user, you must change the access permission to /var/lib/mongo and /var/log/MongoDB so that the desired user has access to these directories.

Allocate unlimited resources to MongoDB

Most Linux distribution system resources depend on sessions limit, which negatively affects MongoDB’s performance. Therefore, we suggest you read the limit settings documentation for more information.

Be sure to read:   DNS settings training in WHM.

Allocate unlimited resources to MongoDB

In the next section, we’ll explore interacting with MongoDB.

The process of running and interacting with MongoDB

In this section, we will teach MongoDB in 5 sections.

1. Run the MongoDB service

To start MongoDB, you need to issue the following command:

service mongod start

2. Controlling the successful execution of MongoDB

To ensure the successful execution of the MongoDB database, you can refer to its log file in /var/log/mongodb/mongod.log.

<initandlisten] waiting for connections on port <port]

The <port> field defaults in 27017 /etc/mongod.conf

After the system restarts, you can MongoDB is running by running the following command: make sure

chkconfig mongod on

3. Stop MongoDB service

If you want to stop mongod for any reason, you can use the following command:

service mongod stop

4. service Restart the MongoDB

To restart the Mongod service, just run the following command:

service mongod restart

You can see the restart process and possible error messages in the log file:

/var/log/mongodb/mongod.log

5. Getting started with MongoDB

Start the Mongo shell on the same machine you use mongod on. The execution of the Mongo shell is done without any settings in the command line, and it will be launched to mongod located on the local host with the default port 27017:

mongo

In addition, even if you want to delete this database, you won’t have a hard time.

Remove MongoDB

To completely remove MongoDB from your installed system, your applications need to delete MongoDB along with configuration files and all directories that contain files and logs.

Warning: The following commands completely remove MongoDB from the target system and delete all databases! This process is irreversible, and before starting, we recommend you back up all settings and information created in MongoDB.

The MnogoDB removal process is done in 3 steps.

1. stopped MongoDB has

by issuing the following command: MongoDB Stop

sudo service mongod stop

2. packages Remove MongoDB

In the second step, you should delete all the MongoDB databa packages you have installed before. The following command will do this for you:

(sudo yum erase $(rpm -qa | grep mongodb-enterprise

3. Delete the data directory

In the last step, you should delete all databases and log files with the following command:

sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongo

In this way, the MongoDB database will be completely deleted.

The last word

in a very simple In this article, we tried to provide you with MongoDB training way. From installation to its complete removal