linux下mongodb安装及服务配置

安装可参考官方文档http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/

在官方下载解压即可运行(几乎等于绿色版)

默认情况数据库存储位置为/data/db,建立些目录即可以直接运行否则需要带上参数–dbpath,不然运行报以下错误

 

 

[[email protected] bin]# ./mongod
./mongod –help for help and startup options
2013-12-09T21:02:17.140+0800 [initandlisten] MongoDB starting : pid=24761 port=27017 dbpath=/data/db 64-bit host=portal
2013-12-09T21:02:17.140+0800 [initandlisten]
2013-12-09T21:02:17.140+0800 [initandlisten] ** NOTE: This is a development version (2.5.4) of MongoDB.
2013-12-09T21:02:17.140+0800 [initandlisten] **       Not recommended for production.
2013-12-09T21:02:17.141+0800 [initandlisten]
2013-12-09T21:02:17.141+0800 [initandlisten] db version v2.5.4
2013-12-09T21:02:17.141+0800 [initandlisten] git version: ffd52e5f46cf2ba74ba931c78da62d4a7f480d8e
2013-12-09T21:02:17.141+0800 [initandlisten] build info: Linux ip-10-149-11-126 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
2013-12-09T21:02:17.141+0800 [initandlisten] allocator: tcmalloc
2013-12-09T21:02:17.141+0800 [initandlisten] options: {}
2013-12-09T21:02:17.141+0800 [initandlisten] exception in initAndListen: 10296
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in –dbpath.
See
http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
dbexit: 2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: going to close listening sockets…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: going to flush diaglog…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: going to close sockets…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: waiting for fs preallocator…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: lock for final commit…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: final commit…
2013-12-09T21:02:17.141+0800 [initandlisten] shutdown: closing all files…
2013-12-09T21:02:17.141+0800 [initandlisten] closeAllFiles() finished


另一种运行方式是通过配置文件运行,这样就必每次运行都手动输入一堆参数

我的例子如下,

[[email protected] bin]# cat /etc/mongod.conf
fork = true
port = 27017
bind_ip = 127.0.0.1
quiet = true
dbpath = /mongodb/data
logpath = /mongodb/data/mongod.log
logappend = true
journal = true
auth = false
[[email protected] bin]#

参数意思大致如下(个人翻译哈,请官方文档为准,不对欢迎指正哈linux下mongodb安装及服务配置 - 失落的阳光 - 失落的阳光):
fork为false时(默认),执行数据库启动命令后,命令会挂在那,强行结束命令,数据库也跟着停了。为true则不会
port运行端口号,懂得就不多说了
bind_ip运行绑定IP,我的127.0.0.1,也就是只有本地可以访问数据库,如果要允许其他机器也能访问,则应该为连网IP地址,如果需要绑定多IP直接用,隔开(如127.0.0.1,192.168.0.1,通常是有多网卡或有其他虚拟网络访问是用到)
quiet静默运行,为true时将不输出日志,数据库运行时可通过更改属性改变是否输出日志
dbpath数据文件路径
logpath日志位置
logappend是否追加日志,为false(默认)时每次运行都覆盖上次一运行的日志
journal是否启用日志,64位系统默认是启用的(此设置可能多余)。(这条是找是谷歌翻译的linux下mongodb安装及服务配置 - 失落的阳光 - 失落的阳光)
auth是否启用认证,即访问数据库时是否需要用户名密码,认证需要先创建数据库的用户名密码

将mongodb配置为服务运行

在/etc/init.d/创建文件mongodb,并添加可执行权限(chmod +x /etc/init.d/mongodb)

 

[[email protected] bin]# cat /etc/init.d/mongodb
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongodb
start() {
/mongodb/mongodb/bin/mongod –config /etc/mongod.conf
}

stop() {
/mongodb/mongodb/bin/mongod –config /etc/mongod.conf –shutdown
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

注:#chkconfig: 2345 80 90和#description: mongodb必须有,否则在添加服务时将提示“mongodb 服务不支持 chkconfig  ”
接着添加服务
chkconfig --add mongodb	添加服务
chkconfig mongodb on	设置开机启动服务
service mongodb start	服务服务
致此mongodb安装配置完成

Tagged: , , ,

Comments are closed.