一、系統(tǒng)環(huán)境
CentOS 6.8_x64
官方參考文檔https://docs.MongoDB.org/manual/reference/glossary/#term-init-script
呼圖壁網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)從2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
二、添加官方y(tǒng)um庫(kù)
#cd /etc/yum.repo.d/
#vim mongodb.repo
[mongodb-org-3.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/ gpgcheck=0 enabled=1
三、安裝配置
1、安裝并創(chuàng)建數(shù)據(jù)目錄
#yum install -y mongodb-org #mkdir -p /Data/mongodb #chown mongod.mongod /Data/mongodb -R
2、配置mongod.conf
#vim /etc/mongod.conf
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /Data/mongodb/mongod.log #需要自定義 # Where and how to store data. storage: dbPath: /Data/mongodb/db #需要自定義 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 # network interfaces net: port: 27017 bindIp: 10.1.0.7 # Listen to local interface only, comment to listen on all interfaces. 需要自定義 #security: #operationProfiling: #replication: #sharding: # Enterprise-Only Options #auditLog:
啟動(dòng)mongod
#service mongod start
四、測(cè)試
登錄mongodb
#mongo --host 10.1.0.7
> db.version();
3.0.7
> show dbs
com_ylt_plat_passport 0.078GB
local 0.078GB
chown mongod.mongod /Data/mongodb -R
service mongod start
五、排錯(cuò)
故障描述 :
service mongod stop 時(shí)發(fā)現(xiàn) 并沒(méi)有 關(guān)閉mongod服務(wù) 進(jìn)程依然在
通過(guò)排查發(fā)現(xiàn)問(wèn)題出在/etc/mongod.conf中第24行
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
把后面的# location of pidfile 刪除掉 即可,這個(gè)是一個(gè)小bug
六、mogond備份與還原腳本
#cat mongodb_bak.sh
#!/bin/sh DUMP=/usr/bin/mongodump OUT_DIR=/data1/backup/mongodb/mongod_bak_now BAK_DIR=/data1/backup/mongodb/mongod_bak_list DATE=`date +%F_%H%M%d` #DB_USER=username #DB_PASS= DAYS=7 TAR_BAK="mongodb_bak_$DATE.tar.gz" [ -d $OUT_DIR ] || mkdir -v $OUT_DIR [ -d $BAK_DIR ] || mkdir -v $BAK_DIR BAK_DB(){ cd $OUT_DIR rm -rf $OUT_DIR/* mkdir -p $DATE #$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE $DUMP -o $OUT_DIR/$DATE tar czvf $BAK_DIR/$TAR_BAK $OUT_DIR/$DATE find $BAK_DIR/ -mtime +$DAYS -delete } RESTORE_ALL(){ cd $OUT_DIR for d in `ls`; do echo $OUT_DIR/$d /usr/bin/mongorestore -d $OUT_DIR/$d done } RESTORE_Choose(){ while true do echo "when you choose 'quit|exit' exit to restore!" read -p "What's your choose?(Enter continue!)" choose if [[ $choose == 'quit' || $choose == 'exit' ]] then echo "You choose exit!" && exit 2 fi cd $OUT_DIR d=`ls` cd $OUT_DIR/$d ls read -p "What's db your choose?" whatdb if [ "$whatdb" != '' ]; then /usr/bin/mongorestore -d $whatdb else echo "choose is empty,exit~" && exit 0 fi done } case $1 in back) BAK_DB ;; resall) RESTORE_ALL ;; resone) RESTORE_Choose ;; *) echo "USGE:back|resall|resone" ;; esac
使用說(shuō)明:
back 備份全部的mongod數(shù)據(jù)庫(kù)
resall 還原所有的數(shù)據(jù)庫(kù)
resone可以指定還原某一個(gè)數(shù)據(jù)庫(kù)
七、解決警告提示
1、問(wèn)題描述
解決登錄mongo --host 10.1.0.7 --port 27017 類似如下提示
** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 和 ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
MongoDB shell version: 3.0.7
connecting to: 10.1.0.7:27017/test
Server has startup warnings:
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten]
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten]
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-08T16:10:15.638+0800 I CONTROL [initandlisten]
由于環(huán)境為CentOS6.8 所以解決方法如下,其他平臺(tái)及版本請(qǐng)參考官方文檔:https://docs.mongodb.org/manual/tutorial/transparent-huge-pages/
2、解決方法:
添加如下腳本
#vim /etc/init.d/disable-transparent-hugepages
#!/bin/sh### 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 unset thp_path ;; esac
添加到開機(jī)自啟服務(wù)
#chmod +x /etc/init.d/disable-transparent-hugepages #chkconfig --add disable-transparent-hugepages
3、修改系統(tǒng)參數(shù)
#mkdir -p /etc/tune-profiles/no-thp #cd /etc/tune-profiles/no-thp #echo "set_transparent_hugepages never" > ktune.sh #chmod +x ktune.sh #tuned-adm profile no-thp 如果提示找不到命令請(qǐng)執(zhí)行yum install tuned -y
reboot 系統(tǒng)
4、驗(yàn)證:
$mongo --host 10.1.0.7 --port 27017
MongoDB shell version: 3.0.7
connecting to: 10.1.0.7:27017/test
>
5、出現(xiàn)如下錯(cuò)誤:
** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
#vim /etc/security/limits.conf
添加:
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
重啟mongod
到此mongod安裝完成~如有錯(cuò)誤之處歡迎指正!