转载自:http://blog.csdn.net/bluejoe2000/article/details/41323051
完整的搭建mongodb集群(副本集+分片)的例子。。。
准备四台机器,分别是bluejoe1,bluejoe2,bluejoe3,以及bluejoe0
副本集及分片策略确定如下:
- 将创建3个副本集,命名为shard1,shard2,shard3;
- 以上3个副本集作为3个分片;
- 每个副本集包含3个副本(主、辅1、辅2);
- 副本分开存储,即shard1存在bluejoe1、bluejoe2、bluejoe3上各一份。。。以此类推
- 将创建3个配置库实例,一台机器一个
- bluejoe0上配置一个mongos(mongos一般可以配置在应用端)
画了一个图:
具体操作步骤如下:
- 在bluejoe1上下载mongdb安装包;
- 解压至/usr/local/mongdb(注意改名);
- 创建data/db1,db2,db3目录,启动3个mongod实例,注意replSet的名字:
- mkdir /usr/local/mongodb/data/db1 /usr/local/mongodb/data/db2 /usr/local/mongodb/data/db3
- nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db1 --port 10001 --replSet db1 --logpath=/usr/local/mongodb/log/db1.log &
- nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db2 --port 10002 --replSet db2 --logpath=/usr/local/mongodb/log/db2.log &
- nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db3 --port 10003 --replSet db3 --logpath=/usr/local/mongodb/log/db3.log &
- 使用scp命令,将mongodb目录复制至bluejoe2和bluejoe3机器,并按照第3步启动每台机器上的3个实例;
- 初始化副本:
- mongo bluejoe1:10001
- use admin
- db.runCommand(
- {
- "replSetInitiate":
- {
- "_id":"db1",
- "members":
- [
- {
- "_id":1,
- "host":"bluejoe1:10001"
- },
- {
- "_id":2,
- "host":"bluejoe2:10001"
- },
- {
- "_id":3,
- "host":"bluejoe3:10001"
- }
- ]
- }
- })
- mongo bluejoe1:10002
- use admin
- db.runCommand(
- {
- "replSetInitiate":
- {
- "_id":"db2",
- "members":
- [
- {
- "_id":1,
- "host":"bluejoe1:10002"
- },
- {
- "_id":2,
- "host":"bluejoe2:10002"
- },
- {
- "_id":3,
- "host":"bluejoe3:10002"
- }
- ]
- }
- })
- mongo bluejoe1:10003
- use admin
- db.runCommand(
- {
- "replSetInitiate":
- {
- "_id":"db3",
- "members":
- [
- {
- "_id":1,
- "host":"bluejoe1:10003"
- },
- {
- "_id":2,
- "host":"bluejoe2:10003"
- },
- {
- "_id":3,
- "host":"bluejoe3:10003"
- }
- ]
- }
- })
- 观察副本集的实时复制特性
- 连接上bluejoe1:10001,看到db1:PRIMARY>的字样,说明它是db1的主节点
- 创建一张新表,创建几条测试记录
- 连接上bluejoe3:10002,看到shard1:SECONDARY>的字样,说明它是shard1的辅助节点
- 注意此时无法查询前面新增的记录,会报错not master and slaveOk=false
- 回到bluejoe1:10001,设置slaveOk
- db.getMongo().setSlaveOk()
- 再回到bluejoe3:10002,即可看到主节点写入的记录
- 观察副本集的故障转移特性
- 杀掉bluejoe1:10001的进程
- 再次连接上bluejoe3:10002,看到shard1:PRIMARY>的字样,说明它已经变成shard1的主节点
- 如果这时再启动bluejoe1:10001,则发现它已经变成shard1:SECONDARY>
- 创建data/configdb,启动配置库实例:
- mkdir /usr/local/mongodb/data/configdb
- nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --port 20000 --logpath=/usr/local/mongodb/log/configdb.log &
如果需要关闭mongod服务,可使用--shutdown选项,如:- /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --shutdown
- 接下来管理分片,在bluejoe0上启动mongs:
- nohup /usr/local/mongodb/bin/mongos --port 30000 --configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000 &
- 连接上mongos,配置分片信息:
- mongo bluejoe0:30000
- mongos> use admin
- switched to db admin
- db.runCommand({"addShard":"db1/bluejoe1:10001"})
- db.runCommand({"addShard":"db2/bluejoe1:10002"})
- db.runCommand({"addShard":"db3/bluejoe1:10003"})
- 查看分片情况:
- mongos> db.runCommand({listshards:1})
- {
- <span style="white-space:pre"> </span>"shards" : [
- <span style="white-space:pre"> </span>{
- <span style="white-space:pre"> </span>"_id" : "db1",
- <span style="white-space:pre"> </span>"host" : "db1/bluejoe1:10001,bluejoe2:10001,bluejoe3:10001"
- <span style="white-space:pre"> </span>},
- <span style="white-space:pre"> </span>{
- <span style="white-space:pre"> </span>"_id" : "db2",
- <span style="white-space:pre"> </span>"host" : "db2/bluejoe1:10002,bluejoe2:10002,bluejoe3:10002"
- <span style="white-space:pre"> </span>},
- <span style="white-space:pre"> </span>{
- <span style="white-space:pre"> </span>"_id" : "db3",
- <span style="white-space:pre"> </span>"host" : "db3/bluejoe1:10003,bluejoe2:10003,bluejoe3:10003"
- <span style="white-space:pre"> </span>}
- <span style="white-space:pre"> </span>],
- <span style="white-space:pre"> </span>"ok" : 1
- }
- 对某个库开启分片功能:
- mongos> db.runCommand({"enablesharding":"test"})
- { "ok" : 1 }
- mongos> db.runCommand({"shardcollection":"test.person","key":{_id:'hashed'}})
- { "collectionsharded" : "test.person", "ok" : 1 }
- 插入测试数据:
- mongos> for(var i=0;i<10;i++){db.person.insert({name:"bluejoe"+i});}
- WriteResult({ "nInserted" : 1 })
- 查看数据分片存储情况:
- [root@hadoop0 ~]# mongo bluejoe3:10002
- MongoDB shell version: 2.6.5
- connecting to: bluejoe3:10002/test
- shard1:PRIMARY> db.person.find()
- { "_id" : ObjectId("546ff1013a0d0ec68b42a914"), "name" : "bluejoe0" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a915"), "name" : "bluejoe1" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a91a"), "name" : "bluejoe6" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a91b"), "name" : "bluejoe7" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a91c"), "name" : "bluejoe8" }
- shard1:PRIMARY> exit
- bye
- [root@hadoop0 ~]# mongo bluejoe2:10001
- MongoDB shell version: 2.6.5
- connecting to: bluejoe2:10001/test
- shard2:PRIMARY> db.person.find()
- { "_id" : ObjectId("546ff1013a0d0ec68b42a918"), "name" : "bluejoe4" }
- shard2:PRIMARY> exit
- bye
- [root@hadoop0 ~]# mongo bluejoe3:10001
- MongoDB shell version: 2.6.5
- connecting to: bluejoe3:10001/test
- shard3:PRIMARY> db.person.find()
- { "_id" : ObjectId("546ff1013a0d0ec68b42a916"), "name" : "bluejoe2" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a917"), "name" : "bluejoe3" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a919"), "name" : "bluejoe5" }
- { "_id" : ObjectId("546ff1013a0d0ec68b42a91d"), "name" : "bluejoe9" }
- shard3:PRIMARY>