MongoDB安裝比較簡單,centos能夠直接用yum或rpm包安裝,ubuntu或debian能夠直接用apt或deb包安裝。不過我的仍是習慣直接下載官方源碼或二進制文件安裝。如下我就從官方下載安裝文件安裝,步驟相似以下。html
echo "mongodb1" >/etc/hostname hostname mongodb1
修改添加/etc/hosts,例如:linux
192.168.1.106 mongodb1
useradd mongo mkdir -pv /data/mongodb chown -R mongo.mongo /data/mongodb chmod go-rwx /data/mongodb
下載的是3.2的最新版本。mongodb
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
增長環境變量ubuntu
vim /etc/profile export PATH=/usr/local/mongodb/bin:$PATH source /etc/profile
配置目錄vim
mkdir /usr/local/mongodb/etc chown mongo.mongo /usr/local/mongodb/etc chmod go-rwx /usr/local/mongodb/etc
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開啓。centos
參考:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/bash
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
添加開機自動啓動ide
chmod 755 /etc/init.d/disable-transparent-hugepages chkconfig --add disable-transparent-hugepages
修改ulimit限制ui
參考:https://docs.mongodb.com/manual/reference/ulimit/spa
默認須要修改的地方是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
鏈接本地mongodb
su - mongo mongo