##安装
环境:CentOS 6.7
mongodb版本:3.2.8
安装文件:mongodb-linux-x86_64-3.2.8.tgz
安装目录:/usr/local/mongodb
安装步骤:
1、解压安装包:
[root@centos4 local]# tar -zxvf mongodb-linux-x86_64-3.2.8.tgz
mongodb-linux-x86_64-3.2.8/README
mongodb-linux-x86_64-3.2.8/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-3.2.8/MPL-2
mongodb-linux-x86_64-3.2.8/GNU-AGPL-3.0
mongodb-linux-x86_64-3.2.8/bin/mongodump
mongodb-linux-x86_64-3.2.8/bin/mongorestore
mongodb-linux-x86_64-3.2.8/bin/mongoexport
mongodb-linux-x86_64-3.2.8/bin/mongoimport
mongodb-linux-x86_64-3.2.8/bin/mongostat
mongodb-linux-x86_64-3.2.8/bin/mongotop
mongodb-linux-x86_64-3.2.8/bin/bsondump
mongodb-linux-x86_64-3.2.8/bin/mongofiles
mongodb-linux-x86_64-3.2.8/bin/mongooplog
mongodb-linux-x86_64-3.2.8/bin/mongoperf
mongodb-linux-x86_64-3.2.8/bin/mongod
mongodb-linux-x86_64-3.2.8/bin/mongos
mongodb-linux-x86_64-3.2.8/bin/mongo
2、修改mongodb的安装目录:
[root@centos4 local]# mv mongodb-linux-x86_64-3.2.8 mongodb
3、安装完成后mongodb目录如下所示:
[root@centos4 local]# cd mongodb/
[root@centos4 mongodb]# ls -all
总用量 108
drwxr-xr-x. 3 root root 4096 8月 11 13:02 .
drwxr-xr-x. 26 root root 4096 8月 11 13:03 ..
drwxr-xr-x. 2 root root 4096 8月 11 13:02 bin
-rw-r--r--. 1 1046 1046 34520 7月 13 02:07 GNU-AGPL-3.0
-rw-r--r--. 1 1046 1046 16726 7月 13 02:07 MPL-2
-rw-r--r--. 1 1046 1046 1359 7月 13 02:07 README
-rw-r--r--. 1 1046 1046 35910 7月 13 02:07 THIRD-PARTY-NOTICES
##配置
###将mongodb添加到环境变量中
1、如下操作:
[root@centos4 mongodb]# vim /etc/profile
2、添加MONGODB_HOME:
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
3、使配置生效:
[root@centos4 mongodb]# source /etc/profile
###创建mongodb数据库文件目录和日志文件目录
如下操作:
[root@centos4 mongodb]# mkdir db logs
[root@centos4 mongodb]# cd logs/
[root@centos4 logs]# touch mongodb.log
###创建mongodb的conf配置文件
如下操作:
[root@centos4 mongodb]# cd /etc
[root@centos4 etc]# touch mongod.conf
[root@centos4 etc]# vim mongod.conf
# 数据库文件位置
dbpath=/usr/local/mongodb/db
# 日志文件位置
logpath=/usr/local/mongodb/logs/mongodb.log
# 以追加方式写入日志
logappend=true
# 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
bind_ip=10.1.200.77
# 默认端口为27017
port=27017
###将mongodb配置成服务
1、在/etc/rc.d/init.d/
目录下创建mongodb
服务脚本:
[root@centos4 mongodb]# cd /etc/rc.d/init.d/
[root@centos4 init.d]# touch mongodb
[root@centos4 init.d]# vim mongodb
#!/bin/bash
# mongod - Startup script for mongod
# chkconfig: 35 80 15
# description: Mongo is a scalable, document-oriented database.
# processname: mongod
# config: /etc/mongod.conf
# pidfile: /var/run/mongo/mongo.pid
source /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# NOTE: if you change any OPTIONS here, you get what you pay for:
# this script assumes all options are in the config file.
CONFIGFILE="/etc/mongod.conf"
SYSCONFIG="/etc/sysconfig/mongod"
export PATH=$PATH:/usr/local/mongodb/bin
DBPATH=`awk -F= '/^dbpath/{print $2}' "$CONFIGFILE"`
OPTIONS=" --config $CONFIGFILE"
mongod=${MONGOD-/usr/local/mongodb/bin/mongod}
lockfile=/var/lock/subsys/mongod
echo "db path is: "$DBPATH
echo $mongod
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
start()
{
echo -n $"Starting mongod: "
$mongod $OPTIONS &
echo $mongod$OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
}
stop()
{
echo -n $"Stopping mongod: "
killproc $mongod -QUIT
# killproc -p "$DBPATH"/mongod.lock -d 300 /usr/local/mongodb/bin/mongod
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
}
restart () {
stop
start
}
ulimit -n 12000
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $lockfile ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL
2、以上配置操作完成后,便可将mongodb
注册成为服务:
[root@centos4 init.d]# chkconfig --add mongodb
3、添加文件执行权限:
[root@centos4 init.d]# chmod +x /etc/rc.d/init.d/mongodb
###启动和关闭mongodb服务
启动mongodb
服务 :
[root@centos4 init.d]# service mongodb start
db path is: /usr/local/mongodb/db
/usr/local/mongodb/bin/mongod
Starting mongod: /usr/local/mongodb/bin/mongod --config /etc/mongod.conf
[root@centos4 init.d]# ps -ef|grep mongodb
root 30238 1 13 13:31 pts/0 00:00:01 /usr/local/mongodb/bin/mongod --config /etc/mongod.conf
root 30258 29901 0 13:31 pts/0 00:00:00 grep mongodb
关闭mongodb
服务 :
[root@centos4 init.d]# service mongodb stop
db path is: /usr/local/mongodb/db
/usr/local/mongodb/bin/mongod
Stopping mongod: [确定]
[root@centos4 init.d]# ps -ef|grep mongodb
root 30282 29901 0 13:32 pts/0 00:00:00 grep mongodb
重启mongodb
服务 :
[root@centos4 init.d]# service mongodb restart
db path is: /usr/local/mongodb/db
/usr/local/mongodb/bin/mongod
Stopping mongod: [确定]
Starting mongod: /usr/local/mongodb/bin/mongod --config /etc/mongod.conf
[root@centos4 init.d]# ps -ef|grep mongodb
root 30342 1 3 13:33 pts/0 00:00:00 /usr/local/mongodb/bin/mongod --config /etc/mongod.conf
root 30363 29901 0 13:33 pts/0 00:00:00 grep mongodb
##测试安装
现在就可以直接使用mongo
命令测试mongodb
是否安装成功:
[root@centos4 init.d]# mongo
MongoDB shell version: 3.2.8
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten]
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten]
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten]
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten]
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3810 processes, 12000 files. Number of processes should be at least 6000 : 0.5 times number of files.
2016-08-11T13:33:16.593+0800 I CONTROL [initandlisten]
> show dbs
local 0.000GB
>