Installation configuration of MongoDB under CentOS 7

  • 2020-05-27 07:58:08
  • OfStack

1. Mongodb installation

The following installation steps are performed as an dp user

1. Download the installation package for mongodb 3.4.2:

"mongodb-linux-x86_64-rhel70-3.4.2.tgz", placed under ~ /software.

2. Unzip the file package:

$ cd  ~ /software
$ tar -xf mongodb-linux-x86_64-rhel70-3.4.2.tgz

Get the folder mongodb-linux-x86_64-rhel70-3.4.2 /, where the bin folder contains all the executable files for Mongodb.

3. Copy the bin folder to the default executable directory of the dp user ~ /bin

$ mv bin ~/

4. Create a database folder

Create the database file directory and place it under ~ /data/db

$ mkdir -p ~/data/db

Start the mongod service

$ mongod -dbpath ~/data/db -port 7974

If it can be started, the installation is successful. If not successful, an SELinux configuration may be required to allow MongoDB to be started on the appropriate port

# semanage port -a -t mongod_port_t -p tcp 7974

Note: 7974 is the port used by MongoDB in this project, instead of the default port 27017

2. MongoDB configuration

1. Write configuration scripts:

Create a script mongod conf, stored in/home/dp/data mongodb/config, content is

 destination: file
### Log storage location 
 path: /home/dp/data/mongodb/log/mongod.log
 logAppend: true
##journal configuration 
 enabled: true
## Data file storage location 
 dbPath: /home/dp/data/db/
## Whether or not 1 A library 1 A folder 
 directoryPerDB: true
## Data engine 
 engine: wiredTiger
##WT Engine configuration 
##WT The largest use cache Adjust according to the actual situation of the server 4 ~ 8 ) 
  cacheSizeGB: 4
## Whether the index is also stored separately by the database name 
  directoryForIndexes: true
## Table compression configuration , Data compression algorithm, optional values" none "," snappy "," zlib " 
  blockCompressor: none
## The index configuration 
  prefixCompression: true
## make mongo The process becomes the system integration (parent) id for 1 ) 
 fork: true
## Port configuration, setting the default port to 7974 (the default is 27017 ) 
 port: 7974

2. Cancel large transparent page:

MongoDB recommends turning off the large transparent page function that is enabled by default in the Linux system. You can edit the rc.local file to close it when the system starts:

Edit rc.local document:

[root@localhost ~]# vim /etc/rc.d/rc.local

Add the following:

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

Save exit, and then grant rc.local file execution rights:

[root@fx dp]# chmod +x /etc/rc.d/rc.local

Then restart the system.

3. Set MongoDB to boot

Configure MongoDB startup using systemctl service management under CentOS 7:

CentOS 7 service systemctl scripts in/usr lib systemd/MongoDB need boot can run without landing, will exist system services/usr lib systemd/system directory.

Each service ends with.service, such as mongod.service. The content is divided into three parts: [Unit], [Service] and [Install].

Description=MongoDB database server

#Type=forking #1 Secondary priming, mongod The background is kept by mongod Defined in its own configuration file 
ExecStart=/usr/bin/numactl --interleave=all /home/dp/bin/mongod -f /home/dp/data/mongodb/config/mongod.conf


Note: MongoDB recommends turning off NUMA and starting the mongod process with numactl --interleave=all
In 754, after the completion of script writing according to above permissions stored in the/usr lib/systemd system directory, then you can use systemctl configuration:

Reload the configuration file using systemctl daemon-reload Use systemctl start mongod to test whether the service can run successfully If it doesn't work, you can use systemctl status mongod to view error messages and other service information.

Can check, if you cannot start MongoDB log/home dp/data mongodb/log/mongod log, if it is due to the following error:

WiredTiger error (13) [1488260221:910792][6102:0x7f33bf806dc0], file:WiredTiger.wt, connection: /home/dp/data/db/WiredTiger.turtle: handle-open: open: Permission denied

The chmod, chown, chgrp commands can be used to modify the properties of the file, either because WiredTiger.turtle does not have sufficient permissions, or because the owner is not an dp user.

You can use systemctl enable mongod to add the written service to the boot.

3. Security configuration of MongoDB

1. Modify the login port number and IP

In the configuration file/home/dp/data mongodb/config/mongod conf, definition:

$ mv bin ~/

2. Add an authenticated user

At the beginning of the installation, MongoDB has one admin database by default. At this time, admin database is empty and no information related to permissions is recorded. When the collection admin.system.users has no one user, even if the -auth parameter is added when mongod is started, you can do anything without authentication (whether started with the -auth parameter or not) until a user is added to admin.system.users, even if the -auth parameter is not added to admin database. The core of the consolidation is that the authentication of mongodb and the authorization service can only take effect after the user is added to admin.system.users.

Step 1: log into the database without authentication enabled

$ mv bin ~/

Step 2: switch to the admin database

$ mv bin ~/

Step 3: create the administrator account


The new users are in db.system.users

$ mv bin ~/


1)MongoDB will cancel the use of addUser method starting from version V3 and create users using db.createUser method;

2) the account should not be set as a common account, and the password should meet a certain complexity of at least 8 digits, including a mixture of upper and lower case letters, Numbers and special characters. Common passwords such as birthday, name and id number should not be used.

Step 4: verify that the user was created successfully

$ mv bin ~/

Step 5: kill the process and restart the mongoDB service

4. Installation of Mongodb C drive

1. Download Mongodb C driver file unzip

Download from: mongo-c-driver 1.61

2. Run the configuration script

$ mv bin ~/

3. The compilation

$ mv bin ~/

4. Install

$ sudo make install

Note: before using sudo, the sudo permission of dp user should be set. The command is

$ mv bin ~/

Or use root users

# make install

Install the generated header file in/usr local/include/libmongoc - 1.0, the library files under/usr local/lib.

Related articles: