Original content, please indicate the source for reprinting
Use this tutorial to install MongoDB Community Edition on Red Hat Enterprise Linux or CentOS Linux version 6 and 7 using the .rpm
package.
This installation guide only supports 64-bit systems. For more information, see Supported Platforms.
Production notes
Before deploying MongoDB in a production environment, please consider the Production Notes document.
note
To install a different version of MongoDB, please refer to the documentation for that version. To install the previous version, please refer to the tutorial of version 3.6.
This installation guide only supports 64-bit systems. For more information, see Supported Platforms.
The official introduction has two ways to install
First, let's take a look at the yum installation method.
yum
). ¶Create a /etc/yum.repos.d/mongodb-org-4.0.repo
file so that you can install MongoDB directly with the following command of yum
:
The mongodb-org-4.0.repo
file is as follows:
[ 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
Write yum source files on the server
note
You can find the files for each version of.repo
in the repository itself. The odd-numbered minor version (such as 3.5) is a development version and is not suitable for production use.
To install the latest stable version of MongoDB, issue the following command:
sudo yum install -y mongodb-org
Installed
Check what version of mongo is after installation:
[ root@server81 yum.repos.d]# mongo
mongo mongodump mongofiles mongorestore mongostat
mongod mongoexport mongoimport mongos mongotop
[ root@server81 yum.repos.d]# mongo --version
MongoDB shell version v4.0.4
git version: f288a3bdf201007f3693c58e140056adf8b04839
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
[ root@server81 yum.repos.d]#
So what if a specific version number needs to be installed?
You can uninstall sudo yum erase -y mongodb-org
first, and then install it.
To install a specific version of
MongoDB
, specify each component package individually and append the version number to the package name, as shown in the following example:
sudo yum install -y mongodb-org-4.0.4 mongodb-org-server-4.0.4 mongodb-org-shell-4.0.4 mongodb-org-mongos-4.0.4 mongodb-org-tools-4.0.4
Although this installation method is convenient, there are cases where yum source solves it. In order to ensure that the installation can be repeated easily, it is best to offline the rpm package of the yum source.
You can specify any available
MongoDB
version. Butyum
, when a newer version is available, please upgrade the package. To prevent accidental upgrades, please secure the packaging. To fix the package, add the followingexclude
directive to your/etc/yum.conf
file:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
In order to facilitate the installation of the same version again next time, I am used to download the rpm package of the version installed by yum, which can be used directly during the next installation.
yum install -y mongodb-org --downloadonly --downloaddir=/work/mongodb
The downloaded rpm package is as follows:
[ root@server81 mongodb]# ls -ll -h
total 77M
- rw-r--r--1 root root 5.9K Nov 704:12 mongodb-org-4.0.4-1.el7.x86_64.rpm
- rw-r--r--1 root root 13M Nov 704:12 mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm
- rw-r--r--1 root root 22M Nov 704:12 mongodb-org-server-4.0.4-1.el7.x86_64.rpm
- rw-r--r--1 root root 14M Nov 704:12 mongodb-org-shell-4.0.4-1.el7.x86_64.rpm
- rw-r--r--1 root root 30M Nov 704:12 mongodb-org-tools-4.0.4-1.el7.x86_64.rpm
[ root@server81 mongodb]#
[ root@server81 mongodb]# rpm -ivh mongodb-org-server-4.0.4-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...1:mongodb-org-server-4.0.4-1.el7 ################################# [100%]
Created symlink from/etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.[root@server81 mongodb]#
[ root@server81 mongodb]# rpm -ivh mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...1:mongodb-org-mongos-4.0.4-1.el7 ################################# [100%][root@server81 mongodb]#
[ root@server81 mongodb]# rpm -ivh mongodb-org-shell-4.0.4-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...1:mongodb-org-shell-4.0.4-1.el7 ################################# [100%][root@server81 mongodb]#
[ root@server81 mongodb]# rpm -ivh mongodb-org-tools-4.0.4-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...1:mongodb-org-tools-4.0.4-1.el7 ################################# [100%][root@server81 mongodb]#
[ root@server81 mongodb]# rpm -ivh mongodb-org-4.0.4-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...1:mongodb-org-4.0.4-1.el7 ################################# [100%][root@server81 mongodb]#
[ root@server81 mongodb]# rpm -qa | grep mongodb
mongodb-org-shell-4.0.4-1.el7.x86_64
mongodb-org-4.0.4-1.el7.x86_64
mongodb-org-tools-4.0.4-1.el7.x86_64
mongodb-org-server-4.0.4-1.el7.x86_64
mongodb-org-mongos-4.0.4-1.el7.x86_64
[ root@server81 mongodb]#
Organize the installed shell scripts.
Installation script install_mongodb.sh
[ root@server81 mongodb]# vim install_mongodb.sh
rpm -ivh mongodb-org-server-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-mongos-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-tools-4.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-4.0.4-1.el7.x86_64.rpm
Uninstall script erase_mongodb.sh
[ root@server81 mongodb]# vim erase_mongodb.sh
rpm -qa | grep mongodb |while read line
do
# echo $line
rpm -e --nodeps $line
done
important
If you use SELinux, you must configure SELinux to allow MongoDB to start on Red Hat Linux-based systems (Red Hat Enterprise Linux or CentOS Linux).
To configure SELinux, the administrator has three options:
enforcing
mode, enable access to the relevant ports that will be used by the MongoDB deployment (for example, 27017
). For more information about MongoDB default port, please refer to Default MongoDB port. For the default settings, it can be done by runningsemanage port -a -t mongod_port_t -p tcp 27017
/etc/selinux/config
by setting the SELINUX setting to disabled.SELINUX=disabled
The system must be rebooted for the changes to take effect.
/etc/selinux/config
SELINUX=permissive
The system must be rebooted for the changes to take effect.
You can use setenforce to change to permissive mode instead. setenforce does not need to be restarted but is not persistent.
Or, you can choose not to install the SELinux software package when installing the Linux operating system, or choose to delete the relevant software package. This option is the most intrusive and is not recommended.
caveat
On RHEL 7.0, if you change the data path, the default SELinux policy will prevent mongod from having write access to the new data path without changing the security context.
By default, MongoDB instance stores:
/var/lib/mongo
/var/log/mongodb
If installing via a package manager, the directory will be created during installation.
If you install it manually by downloading a tarball, you can use or create a directory based on the user who will run MongoDB. (For information on sums, see the Linux man page.) mkdir -p<directory> ``sudo mkdir -p<directory> ``mkdir``sudo
By default, MongoDB runs under the mongod
user account. If you change the user running the MongoDB process, you must also** modify the permissions of the /var/lib/mongo
and /var/log/mongodb
directories to grant this user permission to access these directories.
To specify a different log file directory and data file directory, please edit systemLog.path
and storage.dbPath
to set /etc/mongod.conf
. Make sure that the user running MongoDB can access these directories.
Most Unix-like operating systems limit the system resources that a session may use. These restrictions may negatively affect MongoDB operations. For more information, see UNIX ulimit settings.
[ root@server81 ~]# ulimit -n
1024[ root@server81 ~]#
[ root@server81 ~]# ulimit -a
core file size(blocks,-c)0
data seg size(kbytes,-d) unlimited
scheduling priority(-e)0
file size(blocks,-f) unlimited
pending signals(-i)15012
max locked memory(kbytes,-l)64
max memory size(kbytes,-m) unlimited
open files(-n)1024
pipe size(512 bytes,-p)8
POSIX message queues(bytes,-q)819200
real-time priority(-r)0
stack size(kbytes,-s)8192
cpu time(seconds,-t) unlimited
max user processes(-u)15012
virtual memory(kbytes,-v) unlimited
file locks(-x) unlimited
[ root@server81 ~]#
It can be seen from the above that the default ulimit of centos7 can only open the number of files of 1024. If it is a production environment, the number of open files should be adjusted to optimize.
sudo service mongod start
[ root@server81 ~]# sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
[ root@server81 ~]#
[ root@server81 ~]# sudo service mongod status
Redirecting to /bin/systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded:loaded(/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active:active(running) since Sat 2018-12-1516:33:26 HKT; 6min ago
Docs: https://docs.mongodb.org/manual
Process:1031 ExecStart=/usr/bin/mongod $OPTIONS(code=exited, status=0/SUCCESS)
Process:1029 ExecStartPre=/usr/bin/chmod 0755/var/run/mongodb(code=exited, status=0/SUCCESS)
Process:1025 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb(code=exited, status=0/SUCCESS)
Process:1014 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb(code=exited, status=0/SUCCESS)
Main PID:1489(mongod)
Memory:22.9M
CGroup:/system.slice/mongod.service
└─1489/usr/bin/mongod -f /etc/mongod.conf
Dec 1516:33:05 server81 systemd[1]: Starting MongoDB Database Server...
Dec 1516:33:20 server81 mongod[1031]:2018-12-15T16:33:20.851+0800 I CONTROL [m...e'
Dec 1516:33:20 server81 mongod[1031]: about to fork child process, waiting until...s.
Dec 1516:33:20 server81 mongod[1031]: forked process:1489
Dec 1516:33:26 server81 systemd[1]: Started MongoDB Database Server.
Hint: Some lines were ellipsized, use -l to show in full.[root@server81 ~]#
Check the mongoDB configuration file /etc/mongod.conf
, you can know the port number where the service is started. Of course, it can also be modified.
[ root@server81 ~]# vim /etc/mongod.conf
# where to write logging data.
systemLog:
destination: file
logAppend:true
path:/var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath:/var/lib/mongo
journal:
enabled:true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork:true # fork and run in background
pidFilePath:/var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo:/usr/share/zoneinfo
# network interfaces
net:
port:27017
bindIp:127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
# security:
# operationProfiling:
# replication:
# sharding:
## Enterprise-Only Options
# auditLog:
# snmp:
It can be seen from the above that the service of mongoDB is only bound to port 27017 of 127.0.0.1, and the local network card IP does not provide services.
You can test it below.
[ root@server81 ~]# nc -vv 127.0.0.127017
Ncat: Version 7.50( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in/usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from/usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2():nsi_new(IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 127.0.0.1:27017(IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT SUCCESS for EID 8[127.0.0.1:27017]
Ncat: Connected to 127.0.0.1:27017.
libnsock nsi_new2():nsi_new(IOD #2)
libnsock nsock_read(): Read request from IOD #1[127.0.0.1:27017](timeout:-1ms) EID 18
libnsock nsock_readbytes(): Read request for0 bytes from IOD #2[peer unspecified] EID 26
libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 26[peer unspecified](1 bytes):.
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 35[127.0.0.1:27017]
libnsock nsock_readbytes(): Read request for0 bytes from IOD #2[peer unspecified] EID 42^C
[ root@server81 ~]#
[ root@server81 ~]# nc -vv 172.16.5.18127017
Ncat: Version 7.50( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in/usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from/usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2():nsi_new(IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 172.16.5.181:27017(IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT ERROR [Connection refused(111)]for EID 8[172.16.5.181:27017]
Ncat: Connection refused.[root@server81 ~]#
Then if the external server needs to access the mongodb service, you need to modify the bound IP to 0.0.0.0
. Now modify the configuration file as follows:
[ root@server81 ~]# vim /etc/mongod.conf
# network interfaces
net:
port:27017
bindIp:0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
# Restart the mongoDB service
[ root@server81 ~]# service mongod restart
Redirecting to /bin/systemctl restart mongod.service
[ root@server81 ~]#
# Test again if you can use the network card IP for port access
[ root@server81 ~]# nc -vv 172.16.5.18127017
Ncat: Version 7.50( https://nmap.org/ncat )
NCAT DEBUG: Using system default trusted CA certificates and those in/usr/share/ncat/ca-bundle.crt.
NCAT DEBUG: Unable to load trusted CA certificates from/usr/share/ncat/ca-bundle.crt: error:02001002:system library:fopen:No such file or directory
libnsock nsi_new2():nsi_new(IOD #1)
libnsock nsock_connect_tcp(): TCP connection requested to 172.16.5.181:27017(IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT SUCCESS for EID 8[172.16.5.181:27017]
Ncat: Connected to 172.16.5.181:27017.
libnsock nsi_new2():nsi_new(IOD #2)
libnsock nsock_read(): Read request from IOD #1[172.16.5.181:27017](timeout:-1ms) EID 18
libnsock nsock_readbytes(): Read request for0 bytes from IOD #2[peer unspecified] EID 26
libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 26[peer unspecified](1 bytes):.
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 35[172.16.5.181:27017]
libnsock nsock_readbytes(): Read request for0 bytes from IOD #2[peer unspecified] EID 42
libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 42[peer unspecified](1 bytes):.
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 51[172.16.5.181:27017]
libnsock nsock_readbytes(): Read request for0 bytes from IOD #2[peer unspecified] EID 58^C
[ root@server81 ~]#
As can be seen from the above steps, after changing the 127.0.0.1
in the configuration file to the IP binding of 0.0.0.0
, the IP bound to the network card can be used normally to provide external access.
You can check whether the service has started successfully according to the method of viewing the log (tail -f /var/log/mongodb/mongod.log
).
[ root@server81 ~]# tail -f /var/log/mongodb/mongod.log
2018- 12- 15 T16:33:26.285+0800 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: c996783b-c85f-427a-93cb-43437199d3c7
2018- 12- 15 T16:33:26.415+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.02018-12-15T16:33:26.564+0800 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: be8aa834-dcb2-4129-a030-b0f6f7739157
2018- 12- 15 T16:33:26.623+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongo/diagnostic.data'2018-12-15T16:33:26.671+0800 I NETWORK [initandlisten] waiting for connections on port 270172018-12-15T16:33:26.735+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: de45dec3-03a6-4225-bb18-83d4953cc2fc
2018- 12- 15 T16:33:26.801+0800 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties:{ v:2, key:{ lastUse:1}, name:"lsidTTLIndex", ns:"config.system.sessions", expireAfterSeconds:1800}2018-12-15T16:33:26.801+0800 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018- 12- 15 T16:33:26.804+0800 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records.0 secs
2018- 12- 15 T16:33:26.822+0800 I COMMAND [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes:"system.sessions", indexes:[{ key:{ lastUse:1}, name:"lsidTTLIndex", expireAfterSeconds:1800}], $db:"config"} numYields:0 reslen:114 locks:{ Global:{ acquireCount:{ r:2, w:2}}, Database:{ acquireCount:{ w:2, W:1}}, Collection:{ acquireCount:{ w:2}}} protocol:op_msg 129ms
[ root@server81 ~]# sudo chkconfig mongod on
Note: Forwarding request to 'systemctl enable mongod.service'.[root@server81 ~]#
[ root@server81 ~]# sudo service mongod stop
Redirecting to /bin/systemctl stop mongod.service
[ root@server81 ~]#
[ root@server81 ~]# sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service
[ root@server81 ~]#
You can track the progress status of errors or important messages by observing the output in the /var/log/mongodb/mongod.log
file.
To completely remove MongoDB from the system, you must delete the MongoDB application itself, configuration files, and any directories containing data and logs. The following sections will guide you through the necessary steps.
- caveat*
This process will completely delete MongoDB, its configuration and all databases. This process is irreversible, so make sure to back up all configuration and data before proceeding.
sudo service mongod stop
sudo yum erase $(rpm -qa | grep mongodb-org)
Delete the MongoDB database and log files.
sudo rm -r /var/log/mongodb``sudo rm -r /var/lib/mongo
Enter the mongod command and use the default port 27017 to connect to the shell running on localhost:
[ root@server81 mongodb]# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session {"id":UUID("f684453b-2799-4f0c-b4c4-80a82af6e4f5")}
MongoDB server version:4.0.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** WARNING: Access control is not enabled for the database.2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** Read and write access to data and configuration is unrestricted.2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** WARNING:/sys/kernel/mm/transparent_hugepage/enabled is 'always'.2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** We suggest setting it to 'never'2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** WARNING:/sys/kernel/mm/transparent_hugepage/defrag is 'always'.2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]** We suggest setting it to 'never'2018-12-15T16:54:51.953+0800 I CONTROL [initandlisten]---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment(disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()--->
For more information about using the mongo shell to connect, such as connecting to an instance of mongod running on a different host and/or port, see mongo Shell.
MongoDB provides [Getting Started Guide] (https://docs.mongodb.com/manual/tutorial/getting-started/#getting-started) for various driver versions. For the available versions, see "Getting Started".
Recommended Posts