一 目录说明
按照官方建议各个文件存放路径如下所示:
可执行文件存放路径 /usr/local/bin(Ubuntu) /usr/bin(CentOS)
数据库文件存放路径 /var/lib/mongo
日志文件存放路径 /var/log/mongo/mongod.log
配置文件存放路径 /etc/mongod.conf
启动脚本存放路径 /etc/init.d/mongod
二 手动按照过程
1.首先从官网 http://www.mongodb.org/downloads 下载MongoDB
选择合适的版本
2.添加mongod用户
root@zhangzhe-laptop:/home/zhangzhe# passwd mongod
3.解压文件
root@zhangzhe-laptop:/home/zhangzhe/下载# tar -zxvf mongodb-linux-i686-2.4.8.tgz
root@zhangzhe-laptop:/home/zhangzhe/下载# mv mongodb-linux-i686-2.4.8 mongo
root@zhangzhe-laptop:/home/zhangzhe/下载/mongod/bin# chown -R mongod:mongod ./
注意:修改mongod下所有可执行文件的权限
4.将可执行文件放到 /usr/local/bin 目录下
root@zhangzhe-laptop:/home/zhangzhe/下载/mongod/bin# mv * /usr/local/bin
5. 创建数据库存储目录 /var/lib/mongo
root@zhangzhe-laptop:/usr/local/bin# mkdir -p /var/lib/mongo
root@zhangzhe-laptop:/usr/local/bin# chown -R mongod:mongod /var/lib/mongo
注意:修改相应文件的权限
6.创建日志 /var/log/mongo
root@zhangzhe-laptop:/var/lib# mkdir -p /var/log/mongo
root@zhangzhe-laptop:/var/lib# chown -R mongod:mongod /var/log/mongo
7. 在 /etc/mongod.conf目录下新建配置文件
root@zhangzhe-laptop:/etc# vi mongod.conf
#where to log
logpath=/var/log/mongo/mongod.log
logappend = true
#fork and run in background
fork = true
#port = 27017
dbpath=/var/lib/mongo
#location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid
8. 在 /etc/init.d 目录下新建mongod文件
root@zhangzhe-laptop:/home/zhangzhe# vi /etc/init.d/mongod
编写开机启动脚本
#!/bin/sh . /lib/lsb/init-functions CONFIG="/etc/mongod.conf" PROGRAM="/usr/local/bin/mongod" MONGOPID=`ps -ef | grep ‘mongod‘ | grep -v grep | awk ‘{print $2}‘` test -x $PROGRAM || exit 0 case "$1" in start) ulimit -n 3000 log_begin_msg "Starting MongoDB Server" $PROGRAM -f $CONFIG & log_end_msg 0 ;; stop) log_begin_msg "Stopping MongoDB Server" if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi log_end_msg 0 ;; *) log_success_msg "Usage:/etc/init.d/mongodb {start|stop}" exit 1 esac exit 0
修改文件权限
chmod +x /etc/init.d/mongod
设置开机启动
update-rc.d mongod defaults
root@zhangzhe-laptop:/etc/init.d# update-rc.d mongod defaults
update-rc.d: warning: /etc/init.d/mongod missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/mongod ...
/etc/rc0.d/K20mongod -> ../init.d/mongod
/etc/rc1.d/K20mongod -> ../init.d/mongod
/etc/rc6.d/K20mongod -> ../init.d/mongod
/etc/rc2.d/S20mongod -> ../init.d/mongod
/etc/rc3.d/S20mongod -> ../init.d/mongod
/etc/rc4.d/S20mongod -> ../init.d/mongod
/etc/rc5.d/S20mongod -> ../init.d/mongod
三 测试
启动和停止