mongodb官网上下载最新版本的mongodb,按照官网上的说明进行安装。
首先运行如下命令:
1
| curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.3.tgz
|
其次,运用tar 进行解压缩,按照如下命令
1
| tar -zxvf mongodb-linux-x86_64-2.6.3.tgz
|
第三,新建mongodb文件夹,把解压了的mongodb-linux-x86_64-2.6.3复制到mongodb下面。
按照如下命令
1 2
| mkdir -p mongodb cp -R -n mongodb-linux-x86_64-2.6.3/ mongodb
|
第四,更改环境变量,在路径 /etc/下面,找到bashrc文件
1
| export PATH=<mongodb-install-directory>:$PATH
|
替换成你的mongodb路径就可以了
第五,创建mongodb的dbdata的路径
1 2
| mkdir -p /usr/local/mongo/data mongod --dbpath /usr/local/mongo/data
|
第六,进入mongo的文件夹,运行mongo命令,启动mongo就可以了。
上面的安装方法比较简单,但是有个问题,就是mongo启动必须同时制定data,也就是要运行mongod –dbpath /usr/local/mongo/data
,比较繁琐,下面介绍一种一劳永逸的安装办法,把mongo的启动添加到服务当中。
创建配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mkdir -p /usr/local/mongo/etc /usr/local/mongo/data /usr/local/mongo/log/ /usr/local/mongo/repair vim /usr/local/mongo/etc/mongo.conf 在mongo.conf中添加下面的内容 dbpath = /usr/local/mongo/data logpath = /usr/local/mongo/mongodb.log repairpath = /usr/local/mongo/repair pidfilepath = /usr/local/mongo/mongodb.pid directoryperdb = true logappend = true noauth = true port = 27017 maxConns = 1024 fork = true rest = true quota = true quotaFiles = 1024 nssize = 16
|
启动mongodb
1 2
| ln -s /usr/local/mongo/bin/mongod /usr/bin/mongod mongod -f /usr/local/mongo/etc/mongo.conf
|
看看是不是启动起来了,但是使用这种方式管理mongodb服务器很不明智,我们完善一下:
1 2
| mkdir -p /usr/local/mongo/srv vim /usr/local/mongo/srv/mongodb-start
|
添加下面的内容
1 2
| #!/bin/sh mongod -f /usr/local/mongo/etc/mongo.conf
|
1
| vim /usr/local/mongo/srv/mongodb-stop
|
添加下面的内容
1 2 3 4 5
| #!/bin/bash pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`; if [ "${pid}" != "" ]; then kill -2 ${pid}; fi
|
添加执行权限
1 2 3
| chmod a+x /usr/local/mongo/srv/mongodb-start chmod a+x /usr/local/mongo/srv/mongodb-stop vim /etc/rc.d/init.d/mongodb
|
添加下面的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #! /bin/sh
PATH=/usr/local/mongo/bin:/sbin:/bin:/usr/sbin:/usr/bin NAME=mongodb test -x $DAEMON || exit 0 set -e case "$1" in start) echo -n "Starting MongoDB... " /usr/local/mongo/srv/mongodb-start ;; stop) echo -n "Stopping MongoDB... " /usr/local/mongo/srv/mongodb-stop ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop}" >&2 exit 1 ;; esac exit 0
|
添加服务
1 2 3 4
| chmod a+x /etc/rc.d/init.d/mongodb chkconfig --add mongodb chkconfig --level 345 mongodb on /etc/rc.d/init.d/mongodb start
|