最近一直在搭建MongoDB,写的搭建指导书一直没有时间完善,今天先写一篇快速搭建的方法,后续再补充完整的。按照这个方法,在一台机器上可以快速的完成分片、副本集、1个config和1个router的搭建,并以keyfile的加密方式启动进程来提供服务。


1、创建用户

# groupadd -g 20001 mongodb
# useradd -u 20001 -g mongodb mongodb
# passwd mongodb


2、创建数据目录和日志目录

# mkdir -p /export/servers/mongodb
# mkdir -p /export/servers/mongodb/shardserver1/shard1/replcaset1
# mkdir -p /export/servers/mongodb/shardserver1/shard1/log


3、解压mongoDB安装包

# tar -xzvf mongodb-linux-x86_64-2.4.3.tgz
# cp -R -p mongodb-linux-x86_64-2.2.4/* /export/servers/mongodb/


4、启动数据服务器

# ./mongod --shardsvr --replSet shard1 --port 18010 --oplogSize 100 \
--dbpath /export/servers/mongodb/shardserver1/shard1/replcaset1 \
--logpath /export/servers/mongodb/shardserver1/shard1/log/shard1_replcaset1.log \
--logappend --fork --rest


5、初始化分片集的成员(即副本集成员)

# ./mongo admin -port 18010

MongoDB shell version: 2.4.3

connecting to: 127.0.0.1:18010/admin

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

>config_s1 = {_id:"shard1",members:[{_id:0,host:"127.0.0.1:18010"}]} //这里只有一个成员,只添加一个

{

"_id" : "shard1",

"members" : [

{

"_id" : 0,

"host" : "127.0.0.1:18010"

}

]

}

> rs.initiate(config_s1);

{

"info" : "Config now saved locally. Should come online in about a minute.",

"ok" : 1

}


6、创建日志文件

# mkdir -p/export/servers/mongodb/log


7、配置config server

# mkdir -p /export/servers/mongodb/configserver


8、启动config server(非安全模式)

# ./mongod --configsvr --dbpath /export/servers/mongodb/configserver \
--port 40000 --logpath /export/servers/mongodb/configserver/config.log \
--logappend -fork --rest


9、启动router(非安全模式)

# ./mongos --configdb 127.0.0.1:40000 --port 50000 --chunkSize 5 \
--logpath /export/servers/mongodb/log/route.log --logappend --fork


10、添加对应数据库的用户名和密码

# ./mongo --port 50000

MongoDB shell version: 2.4.3

connecting to: 127.0.0.1:50000/test

mongos> use admin

switched to db admin

mongos>db.addUser("admin","admin")

{

"user" : "admin",

"readOnly" : false,

"pwd" : "7c67ef13bbd4cae106d959320af3f704",

"_id" : ObjectId("51ad80e1174d0d05b35dca0f")

}

mongos> db.runCommand({addshard:"shard1/127.0.0.1:18010"})

{ "shardAdded" : "shard1", "ok" : 1 }

mongos> db.runCommand({addshard:"shard2/127.0.0.1:18030"})

{ "shardAdded" : "shard2", "ok" : 1 }

mongos> use test_demo

witched to db test_demo

mongos> db.addUser("sa","sa")

{

"user" : "sa",

"readOnly" : false,

"pwd" : "7e4236991eace9765c3184c2f2e004fa",

"_id" : ObjectId("51ad8187b438ccf427ee2da3")

}

mongos>exit

到这里,非安全模式已经可以启动mongoDB了,不过还没有完,通常自己测试使用时都是使用auth模式就可以了,很少会用到keyfile的方式,在正式的web部署环境中,都是使用的keyfile的方式,这样的方式更安全一些。


11、生成keyfile文件(符合base64格式)

touch /export/servers/mongodb/keyfile
echo "`openssl rand -base64 753`" > /export/servers/mongodb/keyfile
chmod 600 /export/servers/mongodb/keyfile
cp -R -p /export/servers/mongodb/keyfile /export/servers/mongodb/keyfile1
cp -R -p /export/servers/mongodb/keyfile /export/servers/mongodb/keyfile2
cp -R -p /export/servers/mongodb/keyfile /export/servers/mongodb/keyfile3


有人会问,为什么这里要生成三个文件呢?在一台服务器上三个进程用同一个keyfile是没有问题的,但我在配置多台服务器集群时,发现使用一个文件时也是报了LOCK失败的告警的,所以这里才会使用每个进程不同的文件,文件内容其实是一样的,就是为了规避这个问题。

到此,搭建已经完成了,如果觉得一堆参数启动很麻烦,可以使用SHELL脚本或conf配置文件的方式启动,虽然两种方式我都在使用,但我个人推荐后者,具体配置这里不再详述。