在上一篇我们学习了集群配置--副本集的配置, 副本集是集群配置的方式, 分片也是集群配置的方式, 那么这两种方式的区别是什么?
分析上面的区别可以看出,副本集和分片各有优点,因此高可用的服务集群应该时replSet+sharding。
节点分布信息:关于仲裁节点
端口号 | 192.168.226.130 | 192.168.226.131 | 192.168.226.132 |
---|---|---|---|
30000 | mongos | mongos | mongos |
27100 | config server | config server | config server |
27017 | shard1(primary) | shard1(secondary) | shard1(仲裁节点) |
27018 | shard2(primary) | shard2(secondary) | shard2(仲裁节点) |
27019 | shard3(primary) | shard3(secondary) | shard3(仲裁节点) |
config server是存储数据的位置的节点,路由route通过该节点获取数据位置。只设一个节点,config server宕机,就会导致服务整体不可用,因此也设为副本集形式 在/data目录下分别创建config文件夹做数据库文件夹, 并创建配置文件 mongodb-config.conf
内容如下:
dbpath=/data/config
logpath=/data/log/mongodb-config.log
logappend=true
fork=true
bind_ip=192.168.226.130
port=27100
configsvr=true
replSet=configs
关于configsvr属性配置后,config server节点启动失败问题
本来我是用的3.0.6版本的mongdob,config server节点一直启动失败,如果不配置configsvr这个属性就能正常启动,但是无法配置mongos副本集,后来升级到3.4.6版本后就可以正常启动了。
复制到其他两个节点并修改配置文件的内容
[root@moggledb config]# scp mongodb-config.conf root@192.168.226.131:/data/config/ 100% 149 0.2KB/s 00:00
[root@moggledb config]# scp mongodb-config.conf root@192.168.226.132:/data/config/
启动config库,初始化副本集:
[root@moggledb config]# mongo --host 192.168.226.130 --port 27100
...
> var conf={_id:"configs",members:[{_id:0,host:"192.168.226.130:27100"},{_id:1,host:"192.168.226.131:27100"},{_id:2,host:"192.168.226.132:27100"}]}
> rs.initiate(conf)
dbpath=/data/shard1
logpath=/data/log/mongodb-shard1.log
logappend=true
fork=true
bind_ip=192.168.226.130
port=27017
replSet=shard1
# 设置分片
shardsvr=trueZZZZ
分别复制到其他几个节点并修改相应节点信息ip/port/replSet等。 启动节点。 进入节点,初始化副本集:
[root@mongodb2 data]# mongo --host 192.168.226.130 --port 27017
.....
> var conf={_id:"shard1",members:[{_id:0,host:"192.168.226.130:27017"},{_id:1,"host":"192.168.226.131:27017"},{_id:2,host:"192.168.226.132:27017",arbiterOnly:true}]}
> rs.initiate(conf)
{ "ok" : 1 }
shard1:OTHER> exit
bye
[root@mongodb2 data]# mongo --host 192.168.226.130 --port 27018
....
> var conf={_id:"shard2",members:[{_id:0,host:"192.168.226.130:27018"},{_id:1,"host":"192.168.226.131:27018"},{_id:2,host:"192.168.226.132:27018",arbiterOnly:true}]}
> rs.initiate(conf)
{ "ok" : 1 }
shard2:OTHER> exit
bye
[root@mongodb2 data]# mongo --host 192.168.226.130 --port 27019
.....
> var conf={_id:"shard3",members:[{_id:0,host:"192.168.226.130:27019"},{_id:1,"host":"192.168.226.131:27019"},{_id:2,host:"192.168.226.132:27019",arbiterOnly:true}]}
> rs.initiate(conf)
{ "ok" : 1 }
启动路由: 创建mongos文件夹,编辑mongodb-mongos.conf
port=30000
bind_ip=192.168.226.130
logpath=/data/log/mongodb-route.log
logappend=true
configdb=configs/192.168.226.130:27100,192.168.226.131:27100,192.168.226.132:27100
fork=true
启动路由,配置分片副本集
[root@moggledb mongos]# mongos -f mongodb-mongos.conf
[root@moggledb mongos]# mongo --host 192.168.226.130 --port 30000
...
mongos> use admin
switched to db admin
mongos> sh.addShard("shard1/192.168.226.130:27017,192.168.226.131:27017,192.168.226.132:27017")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/192.168.226.130:27018,192.168.226.131:27018,192.168.226.132:27018")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/192.168.226.130:27019,192.168.226.131:27019,192.168.226.132:27019")
{ "shardAdded" : "shard3", "ok" : 1 }
至此集群配置完毕,下面测试集群配置是否可用:
存入500条数据
[root@moggledb ~]# mongo -host 192.168.226.130 -port 30000
......
mongos> show dbs
admin 0.000GB
config 0.000GB
mongos> use admin
switched to db admin
mongos> db.runCommand({enablesharding:"study"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"study.book",key:{id:1}})
{ "collectionsharded" : "study.book", "ok" : 1 }
mongos> use study
switched to db study
mongos> for(var i=1;i<=500;i++){db.book.insert({id:i,"name":"java"})}
WriteResult({ "nInserted" : 1 })
## 查看表数据存储状态
mongos> db.book.find().count()
500
mongos> db.book.stats()
{
"sharded" : true,
"capped" : false,
"ns" : "study.book",
"count" : 500,
"size" : 24500,
"storageSize" : 53248,
"totalIndexSize" : 102400,
"indexSizes" : {
"_id_" : 49152,
"id_1" : 53248
},
"avgObjSize" : 49,
"nindexes" : 2,
"nchunks" : 3,
"shards" : {
"shard1" : {
"ns" : "study.book",
"size" : 23471,
"count" : 479,
"avgObjSize" : 49,
"storageSize" : 20480,
"capped" : false,
...
"nindexes" : 2,
"totalIndexSize" : 36864,
"indexSizes" : {
"_id_" : 16384,
"id_1" : 20480
},
"ok" : 1
},
"shard2" : {
"ns" : "study.book",
"size" : 49,
"count" : 1,
"avgObjSize" : 49,
"storageSize" : 16384,
"capped" : false,
...
"nindexes" : 2,
"totalIndexSize" : 32768,
"indexSizes" : {
"_id_" : 16384,
"id_1" : 16384
},
"ok" : 1
},
"shard3" : {
"ns" : "study.book",
"size" : 980,
"count" : 20,
"avgObjSize" : 49,
"storageSize" : 16384,
"capped"