MongoDB安装比较简单,centos可以直接用yum或rpm包安装,ubuntu或debian可以直接用apt或deb包安装。不过个人还是习惯直接下载官方源码或二进制文件安装。以下我就从官方下载安装文件安装,步骤类似如下。
1、修改主机名,添加hosts信息
echo "mongodb1" >/etc/hostname
hostname mongodb1
修改添加/etc/hosts,例如:
192.168.1.106 mongodb1
2、创建mongo用户和数据目录
useradd mongo
mkdir -pv /data/mongodb
chown -R mongo.mongo /data/mongodb
chmod go-rwx /data/mongodb
3、下载安装
下载的是3.2的最新版本。
mkdir /root/softbak
cd /root/softbak
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.21.tgz
tar zxf mongodb-linux-x86_64-3.2.21.tgz
mv mongodb-linux-x86_64-3.2.21 /usr/local/mongodb
增加环境变量
vim /etc/profile
export PATH=/usr/local/mongodb/bin:$PATH
source /etc/profile
配置目录
mkdir /usr/local/mongodb/etc
chown mongo.mongo /usr/local/mongodb/etc
chmod go-rwx /usr/local/mongodb/etc
4、配置启动
su - root
mkdir -pv /var/log/mongodb
mkdir -pv /var/run/mongodb
chown -R mongo.mongo /var/log/mongodb/
chown -R mongo.mongo /var/run/mongodb/
关闭hugepage的动态分配,默认centos 7开启。
参考:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
vim /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides: disable-transparent-hugepages
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before: mongod mongodb-mms-automation-agent
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description: Disable Linux transparent huge pages, to improve
# database performance.
### END INIT INFO
case $1 in
start)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
echo 'never' > ${thp_path}/enabled
echo 'never' > ${thp_path}/defrag
re='^[0-1]+$'
if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
then
# RHEL 7
echo 0 > ${thp_path}/khugepaged/defrag
else
# RHEL 6
echo 'no' > ${thp_path}/khugepaged/defrag
fi
unset re
unset thp_path
;;
esac
添加开机自动启动
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
修改ulimit限制
参考:https://docs.mongodb.com/manual/reference/ulimit/
默认需要修改的地方是open files、nproc和lock in memory size。
vim /etc/security/limits.conf
# 推荐配置如下
-f (file size): unlimited
-t (cpu time): unlimited
-v (virtual memory): unlimited
-l (locked-in-memory size): unlimited
-n (open files): 64000
-m (memory size): unlimited
-u (processes/threads): 64000
配置类似如下
su - mongo
cd /usr/local/mongodb/etc
cat /usr/local/mongodb/etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /data/mongodb
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
replication:
replSetName: rs1
net:
port: 27017
bindIp: 0.0.0.0
su - mongo
mongod -f /usr/local/mongodb/etc/mongod.conf
su - mongo
mongo