CentOS 7.9 MongoDB installation and use

CentOS 7.9 MongoDB installation and use

1, Software download and installation

Note: CentOS versions include RedHat / CentOS 7.0 and RedHat / CentOS 7.2 s390x. It is recommended to download software installation packages consistent with the CPU architecture of the operating system to ensure compatibility and high performance. Enter arch to view the operating system CPU architecture.

Note: CentOS 7.9 is incompatible with mongodb 5.0.x. please install mongodb 4.4.x

S390x architecture: s390x is a mainframe hardware platform of IBM System z series (zSeries). It is used by banks, large enterprises or scientific research institutions. Most users can't touch it.

Download address

mongodb official website installation manual

cat > /etc/yum.repos.d/mongodb-org-4.4.repo <<\EOF
name=MongoDB Repository

sudo yum install -y mongodb-org

To install the specified version, use the following command

sudo yum install -y mongodb-org-4.4.8 mongodb-org-server-4.4.8 mongodb-org-shell-4.4.8 mongodb-org-mongos-4.4.8 mongodb-org-tools-4.4.8
Dependency resolution

 Package                                             framework                      edition                               source                                  size
 mongodb-org                                         x86_64                    4.4.10-1.el7                       mongodb-org-4.4                    6.2 k
 Install for dependency:
 mongodb-org-database-tools-extra                    x86_64                    4.4.10-1.el7                       mongodb-org-4.4                     19 k
 mongodb-org-mongos                                  x86_64                    4.4.10-1.el7                       mongodb-org-4.4                     17 M
 mongodb-org-server                                  x86_64                    4.4.10-1.el7                       mongodb-org-4.4                     22 M
 mongodb-org-shell                                   x86_64                    4.4.10-1.el7                       mongodb-org-4.4                     14 M
 mongodb-org-tools                                   x86_64                    4.4.10-1.el7                       mongodb-org-4.4                    6.1 k

Transaction summary
Install 1 package (+5 Dependent packages)

Total downloads: 52 M
 Installation size: 192 M

MongoDB Community Edition Packages

MongoDB Community Edition is available from its own dedicated repository, and contains the following officially-supported packages:

Package NameDescription
mongodb-orgA metapackage that automatically installs the component packages listed below.
mongodb-org-databaseA metapackage that automatically installs the component packages listed below.Package NameDescriptionmongodb-org-serverContains the mongod daemon, associated init script, and a configuration file (/etc/mongod.conf). You can use the initialization script to start mongod with the configuration file. For details, see the "Run MongoDB Community Edition" section, above.mongodb-org-mongosContains the mongos daemon.mongodb-org-shellContains the legacy mongo shell.
mongodb-mongoshContains the MongoDB Shell (mongosh).
mongodb-org-toolsA metapackage that automatically installs the component packages listed below:Package NameDescriptionmongodb-database-toolsContains the following MongoDB database tools:mongodumpmongorestorebsondumpmongoimportmongoexportmongostatmongotopmongofilesmongodb-org-database-tools-extraContains the install_compass script

2, System configuration

Starting in MongoDB 4.4, a startup error is generated if the ulimit value for number of open files is under 64000.

1.limits setting

cat > /etc/security/limits.d/mongod.conf <<\EOF
mongod soft nproc 2047
mongod hard nproc 16384
mongod soft nofile 10240
mongod hard nofile 65535


2. Firewall settings

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

3. Close SeLinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

selinux configuration

4. Disable transparent_hugepage

Transparent tiger pages (THP) has been introduced since CentOS6. Since CentOS7, this feature will be enabled by default. Although THP is intended to improve memory performance, some database manufacturers recommend that THP (such as ORACLE, MariaDB, MongoDB, etc.) be turned off directly, otherwise performance may be degraded.

cat >> /etc/rc.d/rc.local <<\EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag

source /etc/rc.d/rc.local

3, Software configuration

1. Data directory

  • /var/lib/mongo (the data directory)
  • /var/log/mongodb (the log directory)
mkdir -p /home/data/mongodb/log
mkdir -p /home/data/mongodb/data
sudo chown -R mongod:mongod /home/data/mongodb

Put the in / etc/mongod.conf storage.dbPath and systemLog.path Change to the above path respectively.

2. Open access IP

Change bindIp in / etc/mongod.conf from to

# network interfaces
  port: 27017
  bindIp:  # Enter,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

4, Start and shut down services

1. Start

sudo systemctl start mongod

2. Status

sudo systemctl status mongod

3. Close

sudo systemctl stop mongod

4. Startup

sudo systemctl enable mongod

5, Unload

sudo yum erase $(rpm -qa | grep mongodb-org)
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

6, Use

1. Free cloud monitoring

Open Cloud monitoring db.enableFreeMonitoring(). Note that the monitoring page is on the official website of mongodb, so the server needs to be connected to the Internet.

> db.enableFreeMonitoring()
        "state" : "enabled",
        "message" : "To see your monitoring data, navigate to the unique URL below. Anyone you share the URL with will also be able to view this page. You can disable monitoring at any time by running db.disableFreeMonitoring().",
        "url" : "https://cloud.mongodb.com/freemonitoring/cluster / random ID ",
        "userReminder" : "",
        "ok" : 1

2. Add user and security authentication




> db
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin
switched to db admin
> db
> db.createUser(
    user: "root",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

> use test
> db.createUser(
    user: "test",
    pwd:  passwordPrompt(),   // or cleartext password
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]

> db.shutdownServer()
server should be down...
  • Enable authentication

vim /etc/mongod.conf add the following

    authorization: enabled
  • client authentication
mongo -umyUserAdmin -p
mongo --port 27017 -u "test" --authenticationDatabase "test" -p

Note: first switch to the corresponding database (such as use admin), and then execute db.auth() authentication (the same is true for query, deletion, etc.).

> db.auth('test')
  • Query user
mongo -uroot -p
> use admin
switched to db admin
> db.getUsers()
  • delete user
> db.dropUser('test')
  • Change Password
db.updateUser('test',{pwd: passwordPrompt()})

7, Client connection tool


nosqlbooster is the most intelligent IDE tool for MongoDB. It has a free use period of 30 days and automatically turns to free version when it expires (function limitation).

9, Problems encountered

  • CentOS 7.9 cannot start after mongodb 5.0.x is installed. The following error is reported:
Nov 11 12:14:02 node-1 systemd: Starting MongoDB Database Server...
Nov 11 12:14:02 node-1 kernel: traps: mongod[83285] trap invalid opcode ip:5568d174ddfa sp:7ffebad6dfc0 error:0 in mongod[5568cd810000+5055000]
Nov 11 12:14:02 node-1 abrt-hook-ccpp: Process 83285 (mongod) of user 991 killed by SIGILL - dumping core
Nov 11 12:14:02 node-1 systemd: mongod.service: control process exited, code=dumped status=4
Nov 11 12:14:02 node-1 systemd: Failed to start MongoDB Database Server.
Nov 11 12:14:02 node-1 systemd: Unit mongod.service entered failed state.
Nov 11 12:14:02 node-1 systemd: mongod.service failed.
Nov 11 12:14:02 node-1 abrt-server: Package 'mongodb-org-server' isn't signed with proper key
Nov 11 12:14:02 node-1 abrt-server: 'post-create' on '/var/spool/abrt/ccpp-2021-11-11-12:14:02-83285' exited with 1
Nov 11 12:14:02 node-1 abrt-server: Deleting problem directory '/var/spool/abrt/ccpp-2021-11-11-12:14:02-83285'
  • terms of settlement

Reduce the mongodb version number and use mongodb 4.4.x (or install it with tar.gz package).

reference resources:

i386, x86_64 and ppc refer to the architecture of CPU](https://www.cnblogs.com/wangwangever/articles/7649603.html)

mongodb linux official website installation tutorial

Install the latest version of mongodb database for Linux (the thinnest in the whole network)

Tags: CentOS MongoDB nosql

Posted on Thu, 11 Nov 2021 21:19:57 -0500 by invinate