標籤:mongodb安裝文檔
首先從官網下載mongodb的安裝包http://www.mongodb.org/downloads我的系統是redhat5.8所有選擇相應的版本包就ok現在最新版是3.0.3
解壓mongodb-linux-x86_64-rhel55-3.0.3.tgz
[[email protected] ~]# tar zxvf mongodb-linux-x86_64-rhel55-3.0.3.tgz -C /opt/mongodb-linux-x86_64-rhel55-3.0.3/READMEmongodb-linux-x86_64-rhel55-3.0.3/THIRD-PARTY-NOTICESmongodb-linux-x86_64-rhel55-3.0.3/GNU-AGPL-3.0mongodb-linux-x86_64-rhel55-3.0.3/bin/mongodumpmongodb-linux-x86_64-rhel55-3.0.3/bin/mongorestoremongodb-linux-x86_64-rhel55-3.0.3/bin/mongoexportmongodb-linux-x86_64-rhel55-3.0.3/bin/mongoimportmongodb-linux-x86_64-rhel55-3.0.3/bin/mongostatmongodb-linux-x86_64-rhel55-3.0.3/bin/mongotopmongodb-linux-x86_64-rhel55-3.0.3/bin/bsondumpmongodb-linux-x86_64-rhel55-3.0.3/bin/mongofilesmongodb-linux-x86_64-rhel55-3.0.3/bin/mongooplogmongodb-linux-x86_64-rhel55-3.0.3/bin/mongoperfmongodb-linux-x86_64-rhel55-3.0.3/bin/mongodmongodb-linux-x86_64-rhel55-3.0.3/bin/mongosmongodb-linux-x86_64-rhel55-3.0.3/bin/mongo[[email protected] opt]# ln -sv mongodb-linux-x86_64-rhel55-3.0.3/ mongodbcreate symbolic link `mongodb‘ to `mongodb-linux-x86_64-rhel55-3.0.3/‘
3.建立mongodb使用者
[[email protected] /]# useradd mongodb
4.建立相應檔案目錄並修改許可權
[[email protected] /]# mkdir /data/db -p[[email protected] /]# chown mongodb:mongodb /data/ -R[[email protected] opt]# chown -R mongodb:mongodb /opt/*
5.添加PATH環境變數在shell下正常訪問mongodb的應用工具
[[email protected] bin]# vim /etc/profile.d/mongodb.shexport PATH=$PATH:/opt/mongodb/bin[[email protected] bin]# source /etc/profile.d/mongodb.sh [[email protected] bin]# echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/mongodb/bin
6.建立mongodb的PID存放目錄
[[email protected] run]# mkdir /var/run/mongodb[[email protected] run]# chown mongodb:mongodb /var/run/mongodb/ -R[[email protected] opt]# mongod --dbpath /data/db/ --logpath /var/log/mongodb.log --logappend --port 27017 --pidfilepath /var/run/mongodb/mongodb.pid --maxConns 250 --rest --httpinterface --forkabout to fork child process, waiting until server is ready for connections.forked process: 4060child process started successfully, parent exiting--maxConns 250 :表示最大串連數是250個--rest --httpinterface : web介面,指定這項28017連接埠才能啟用--fork :後台運行
7.安裝過程中遇見的問題
裝好之後串連出現以下警告
(1).[[email protected] ~]# mongoMongoDB shell version: 3.0.3connecting to: testServer has startup warnings: 2015-06-15T20:27:49.892+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-15T20:27:49.892+0800 I CONTROL [initandlisten]
出現這個問題就查看關於mongodb的檔案許可權,主要是/opt /data/db/ /var/run/mongodb/ /var/log/mongodb.log的許可權因為啟動初始化時以root使用者的許可權在運行,所以這些目錄下的許可權可能會變成root root
(2).[[email protected] db]$ mongod --dbpath /data/db/ --logpath /var/log/mongodb.log --logappend --port 27017 --pidfilepath /var/run/mongodb/mongodb.pid --maxConns 250 --rest --httpinterface --fork2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] 2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
修改兩個參數即可
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
8.安裝成功並登入
[[email protected] ~]# ss -tanlp | grep mongod0 0 *:27017 *:* users:(("mongod",4095,6))0 0 *:28017 *:* users:(("mongod",4095,8)) [[email protected] ~]# mongoMongoDB shell version: 3.0.3connecting to: test> help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, ‘global‘ is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell
登入web介面 28017連接埠
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/7C/wKioL1V-Z8OwoPTiAAdSMuA5uvk007.jpg" title="mongodb.png" alt="wKioL1V-Z8OwoPTiAAdSMuA5uvk007.jpg" />
9.關閉伺服器
(1).[[email protected] ~]# mongod --shutdown killing process with pid: 3936(2).[[email protected] ~]# killall mongod
10.給mongodb編寫設定檔/etc/mongodb.conf
[[email protected] ~]# grep -v ^# /etc/mongodb.conf logpath=/var/log/mongodb.loglogappend=truefork = truedbpath=/data/dbpidfilepath = /var/run/mongodb/mongodb.pidrest = truehttpinterface = true
啟動
[[email protected] ~]# mongod -f /etc/mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 4169child process started successfully, parent exiting[[email protected] ~]# ss -tanlp | grep mongod0 0 *:27017 *:* users:(("mongod",4169,6))0 0 *:28017 *:* users:(("mongod",4169,8))
11.用service mongodb * 來啟動mongodb
啟動指令碼:/etc/rc.d/init.d/mongod
#!/bin/bash# mongod - Startup script for mongod# chkconfig: 35 85 15# description: Mongo is a scalable, document-oriented database.# processname: mongod# config: /etc/mongod.conf# pidfile: /var/run/mongo/mongod.pid. /etc/rc.d/init.d/functions# things from mongod.conf get there by mongod reading itCONFIGFILE="/etc/mongodb.conf"OPTIONS=" -f $CONFIGFILE"SYSCONFIG="/etc/sysconfig/mongod"DBPATH=`awk -F= ‘/^dbpath=/{print $2}‘ "$CONFIGFILE"`PIDFILE=`awk -F= ‘/^dbpath\s=\s/{print $2}‘ "$CONFIGFILE"`mongod=${MONGOD-/opt/mongodb/bin/mongod}MONGO_USER=mongodbMONGO_GROUP=mongodbif [ -f "$SYSCONFIG" ]; then . "$SYSCONFIG"fi# Handle NUMA access to CPUs (SERVER-3574)# This verifies the existence of numactl as well as testing that the command worksNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen NUMACTL="numactl $NUMACTL_ARGS"else NUMACTL=""fistart(){ echo -n $"Starting mongod: " daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod}stop(){ echo -n $"Stopping mongod: " killproc -p "$PIDFILE" -d 300 /opt/mongodb/bin/mongod RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod}restart () { stop start}ulimit -n 12000RETVAL=0case "$1" in start) start ;; stop) stop ;; restart|reload|force-reload) restart ;; condrestart) [ -f /var/lock/subsys/mongod ] && restart || : ;; status) status $mongod RETVAL=$? ;; *) echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" RETVAL=1esacexit $RETVAL
測試啟動
[[email protected] ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 4426child process started successfully, parent exiting [ OK ][[email protected] init.d]# ss -tanlp | grep mongod0 0 *:27017 *:* users:(("mongod",4426,6))0 0 *:28017 *:* users:(("mongod",4426,8)) [[email protected] ~]# mongoMongoDB shell version: 3.0.3connecting to: test> [[email protected] ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 4426child process started successfully, parent exiting [ OK ][[email protected] ~]# service mongod stopStopping mongod: [ OK ]
本文出自 “LoveFish” 部落格,請務必保留此出處http://mictiger.blog.51cto.com/4854014/1661997
mongodb 安裝配置實踐