一. 新建用户和组
# groupadd -r dba
# useradd -r -G dba mongodb
# mkdir /usr/local/mongodb
# chown -R mongodb.dba /usr/local/mongodb
# su - mongodb
$ cat .bash_profile
......
PATH=/usr/local/mongodb/bin:$PATH:$HOME/.local/bin:$HOME/bin
......
二. 内核参数优化
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
# cat /etc/rc.local
......
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
......
# cat /etc/sysctl.conf
......
vm.overcommit_memory=1
vm.overcommit_ratio=90
fs.aio-max-nr=1048576
fs.file-max= 7672460
net.ipv4.ip_local_port_range=9000 65500
net.core.rmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_default=262144
net.core.wmem_max=1048586
kernel.sem= 50100 64128000 50100 1280
kernel.shmall=5242880
kernel.shmmax=12884901888
......
# sysctl -p
三. 系统资源使用优化
# cat /etc/security/limits.conf
......
mongodb fsize unlimited unlimited
mongodb cpu unlimited unlimited
mongodb as unlimited unlimited
mongodb memlock unlimited unlimited
mongodb nproc 65536 65536
mongodb nofile 65536 65536
......
四. 系统依赖包安装
# yum -y install libcurl openssl
五. MongoDB安装
1. MongoDB软件安装
$ cd /usr/local/mongodb
$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
$ tar -zxf mongodb-linux-x86_64-rhel70-4.2.6.tgz
$ mv mongodb-linux-x86_64-rhel70-4.2.6/* .
$ rm -rf mongodb-linux-x86_64-rhel70-4.2.6*
$ mkdir {data,log}
$ ls -lh
total 320K
drwxrwxr-x. 2 mongodb mongodb 231 May 14 22:56 bin
drwxrwxr-x. 7 mongodb mongodb 278 May 15 00:05 data
-rw-r--r--. 1 mongodb mongodb 30K Apr 17 11:10 LICENSE-Community.txt
drwxrwxr-x. 2 mongodb mongodb 25 May 14 23:34 log
-rw-r--r--. 1 mongodb mongodb 17K Apr 17 11:10 MPL-2
-rw-r--r--. 1 mongodb mongodb 2.6K Apr 17 11:10 README
-rw-r--r--. 1 mongodb mongodb 74K Apr 17 11:10 THIRD-PARTY-NOTICES
-rw-r--r--. 1 mongodb mongodb 180K Apr 17 11:11 THIRD-PARTY-NOTICES.gotools
2.配置MongoDB启动参数文件
$ cat /usr/local/mongodb/mongodb.conf
listenBacklog = 1024
maxConns = 1000000
fork = true
port = 27017
pidfilepath = /usr/local/mongodb/mongodb_27017.pid
dbpath = /usr/local/mongodb/data
logpath = /usr/local/mongodb/log/mongodb.log
logappend = true
logRotate = rename
timeStampFormat = iso8601-local
bind_ip_all = true
directoryperdb = true
journal = true
3. 将MongoDB集成为systemctl管理
# cat /usr/lib/systemd/system/mongodb.service
[Unit]
Description=MongoDB document database.
Documentation=https://docs.mongodb.com/manual/
After=syslog.target
After=network.target
Wants=network-online.target
[Service]
Type=forking
User=mongodb
Group=dba
Restart=always
# Disable OOM kill on the MongoDB
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongodb.conf
KillMode=mixed
KillSignal=SIGINT
# Do not set any timeout value, so that systemd will not kill MongoDB
# during crash recovery.
TimeoutSec=0
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl enable mongodb
# systemctl start mongodb
# systemctl status mongodb
● mongodb.service - MongoDB document database.
Loaded: loaded (/usr/lib/systemd/system/mongodb.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-05-15 00:30:28 CST; 3min 18s ago
Docs: https://docs.mongodb.com/manual/
Main PID: 103753 (mongod)
CGroup: /system.slice/mongodb.service
└─103753 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
May 15 00:30:27 ec2-mongodb-01 systemd[1]: Starting MongoDB document database....
May 15 00:30:27 ec2-mongodb-01 mongod[103751]: about to fork child process, waiting until server is ready for connections.
May 15 00:30:27 ec2-mongodb-01 mongod[103751]: forked process: 103753
May 15 00:30:28 ec2-mongodb-01 mongod[103751]: child process started successfully, parent exiting
May 15 00:30:28 ec2-mongodb-01 systemd[1]: Started MongoDB document database..