进入下载目录
- cd /usr/local
下载MongoDB
- wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.7.tgz
解压缩
- tar -zxvf mongodb-linux-x86_64-3.0.7.tgz
创建mongodb需要的文件
- cd mongodb-linux-x86_64-3.0.7
- mkdir data
- mkdir log
- cd log
- touch mongodb.log
启动mongod
- cd /usr/local/mongodb-linux-x86_64-3.0.7/bin
- mongod --dbpath data --logpath log/mongod.log --logappend --fork
如何开机自启动
把mongodb目录加入PATH
- vi /etc/profile
加入如下内容
- export PATH=$PATH:$JAVA_HOME/bin:/usr/local/mongodb-linux-x86_64-3.0.7/bin
立即生效
- source /etc/prifile
创建开机启动脚本
- cd /etc/rc.d/init.d
- touch mongodb
- vi mongodb
加入如下内容
- #!/bin/bash
- #
- # /etc/rc.d/init.d/mongodb
- # init script for tomcat precesses
- #
- # processname: mongodb
- # description:mongodb auto start
- # chkconfig: 2345 86 16
- # description: Start up the Tomcat servlet engine.
- if [ -f /etc/init.d/functions ]; then
- . /etc/init.d/functions
- elif [ -f /etc/rc.d/init.d/functions ]; then
- . /etc/rc.d/init.d/functions
- else
- echo -e "/amongodb: unable to locate functions lib. Cannot continue."
- exit -1
- fi
- RETVAL=$?
- MONGODB_HOME="/usr/local/mongodb-linux-x86_64-3.0.7"
- MONGODB_CONF="/usr/local/mongodb-linux-x86_64-3.0.7/mongodb.conf"
- case "$1" in
- start)
- if [ -f $MONGODB_HOME/bin/mongod ]; then
- echo $"Starting MongoDB"
- #&表示后台启动,也可以使用fork参数
- $MONGODB_HOME/bin/mongod -f $MONGODB_CONF &
- fi
- ;;
- stop)
- if [ -f $MONGODB_HOME/bin/mongod ]; then
- echo $"Stopping MongoDB"
- pkill mongod
- fi
- ;;
- *)
- echo $"Usage: $0 {start|stop}"
- exit 1
- ;;
- esac
- exit $RETVAL
在上面的shell脚本中我们看到了一个配置文件,记得加上,内容如下
- #port=27027
- #MongoDB数据文件目录
- dbpath=/usr/local/mongodb-linux-x86_64-3.0.7/data
- #MongoDB日志文件目录
- logpath=/usr/local/mongodb-linux-x86_64-3.0.7/log/mongodb.log
- #日志文件自动累加
- logappend=true
给启动脚本赋予执行权限
- chmod +x /etc/init.d/mongodb
添加服务
- chkconfig --add mongodb
设置开机启动
- chkconfig --level 35 mongodb on
查看是否设置成功
- chkconfig --list | grep mongodb
此状态下表面开机启动成功
启动服务
- service mongodb start
查看是否启动成功
- mongo
看到如下内容
表明mongodb服务就装好啦。
另外mongodb的默认端口号27017在服务器中防火墙中是没有开放的,需要打开,可以参考我的这篇文章 CentOS/Linux 开放80、3306端口或者开放某个端口