In previous installments, mongodb was added, deleted, modified, and queried in detail. This installment will explain how to install mongodb. This tutorial uses the yum package manager on your system to install MongoDB version 4.4 for the following reasons: Although MongoDB can be manually installed by downloading the. TGZ tarball file, you need to install MongoDB dependencies yourself. Therefore, it is recommended to use the yum package manager on your system to install MongoDB, and use the package manager to automatically install all required dependencies. A sample mongod. Conf file is provided to start to simplify future upgrade and maintenance tasks.

Note:

MongoDB supports only 64-bit versions after version 3.4.

1. Configure package management system (YUM)

CD /etc/yum.repos. D, create the mongodb-org-4.4.repo file and write the following information:

[mongo – org – 4.4]

name=MongoDB Repository

Baseurl=repo.mongodb.org/yum/redhat/…

gpgcheck=1

enabled=1

Gpgkey=www.mongodb.org/static/pgp/…

2. Install MongoDB

To install the latest stable version of MongoDB, issue the following command:

sudo yum install -y mongodb-org

Additionally, to install a specific version of MongoDB, specify each component package separately and append the version number to the package name, as shown in the following example:

Sudo yum install -y mongodb-org-4.4.2 mongodb-org-server-4.4.2 mongodb-org-shell-4.4.2 mongodb-org-mongos-4.4.2 sudo yum install -y mongodb-org-4.4.2 mongodb-org-server-4.4.2 Mongo – org – tools – 4.4.2

You can specify any available MongoDB version. However, yum updates these packages when new versions become available. To prevent unexpected upgrades, secure the package. To fix a package, add the following exclude directive to the /etc/ym.conf file:

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

This tutorial installs the latest stable version of MongoDB

3. Run the mongo

3.1. Prerequisites

ulimit

Most Unix-like operating systems limit the system resources that processes can use. These limitations may have a negative impact on MongoDB operation and need to be adjusted.

Note:

When MongoDB 4.4 is started, a startup error occurs if the ulimit of the number of open files is less than 64000

The ulimit -a command displays the open files of the current system, as shown in the following figure:

If the value is smaller than 64000, run the ulimit -n < value > command to set the value.

3.2. Directory path

Use the default directory

By default, MongoDB runs as the mongod user and uses the following default directories:

/var/lib/mongo data directory

/var/log/mongodb log directory

If you install through package manager,

Create default directories and set the owner and group of these directories to Mongod.

If you install it by downloading the Tarball,

The default MongoDB directory is not created. Create MongoDB data and log directories.

sudo mkdir -p /var/lib/mongo

sudo mkdir -p /var/log/mongodb

By default, MongoDB is run as the mongod user. Once created, set the owner and group of these directories to Mongod:

sudo chown -R mongod:mongod

3.2.1. Use a non-default directory

Use data and/or log directories other than the default directory:

1. Create one or more directories. For example, run the following command to create the mongodb folder in the /usr/local/soft directory:

mkdir mongodb

2. Edit the configuration file **/etc/mongod. Conf and ** modify the following fields:

Data storage. DbPath: specify a new directory path (for example, / usr/local/soft/mongo/data)

SystemLog. Specify a new path: log file path (such as/usr/local/soft/mongo/log/mongod log)

3. Ensure that users running MongoDB can access the following directories:

sudo chown -R mongod:mongod

Example: sudo chown -r mongod:mongod usr/local/soft/mongodb

3.3. How to play the mongo

3.3.1. Start the mongo

Perform:

sudo systemctl start mongod

If you receive an error like the following when starting Mongod:

Failed to start Mongod. Service: Unit mongod. Service not found.

Run the following command:

sudo systemctl daemon-reload

3.3.2. Check whether MongoDB is started successfully

Perform:

sudo systemctl status mongod

The following figure indicates that the operation is successful

3.3.3. Set MongoDB to start automatically upon startup

Perform:

sudo systemctl enable mongod

3.3.4. Stop the mongo

Perform:

sudo systemctl stop mongod

3.3.5. Restart MongoDB

Perform:

sudo systemctl restart mongod

3.3.6. Start connecting to MongoDB

On the host where mongodb was installed, run the following command:

mongo

You can see that mongodb has been connected. The default port is 27017

4. CentOS uninstalls MongoDB

4.1. Stop mongo

Perform:

sudo service mongod stop

4.2. Remove the package

Delete the MongoDB package that was previously installed:

sudo yum erase $(rpm -qa | grep mongodb-org)

4.3. Delete data and log directories

Perform:

sudo rm -r /usr/local/soft/mongodb/data

sudo rm -r /usr/local/soft/mongodb/log/mongod.log

5. Pay attention to the point

Note:

By default, MongoDB starts with bindIp set to 127.0.0.1 and binds to the localhost network interface. This means that Mongod can only accept connections from clients running on the same machine. Remote clients will not be able to connect to Mongod, and MonGod will not be able to initialize the replication set unless the value is set to a valid network interface.

This can be changed either by modifying the bindIp field in the MongoDB configuration file (edit the /etc/mongod.conf configuration file) or by using the — bind_ip command line parameter at startup