MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )

时间:2022-10-10 13:10:20

环境准备

1.本例使用3台Linux主机,IP地址如下:

一、 点击(此处)折叠或打开

Server A: 192.168.106.101
Server B: 192.168.106.102
Server C: 192.168.106.103

2.根据需要,开启相应主机防火墙的相关端口。本次需要用到3台主机,所以开启这3台主机的以下端口:
点击(此处)折叠或打开
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27017 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27018 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27019 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 20000 -j ACCEPT

二、搭建分布式集群

运用MongoDB的复制集(Replica Sets)+分片(Sharding)就能实现MongoDB分布式高可用的集群。
分片(sharding)能够增加更多的机器来应对不断增加的负载和数据,也不影响应用。
复制集(Replica Sets)能确保每个分片节点都具有自动备份、自动故障恢复能力。

1.集群的环境

架构图:
MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )

主机设计:
MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )

2.配置Replica Sets

创建数据目录:

#在serverA上
mkdir -p /data/{shard1-1,shard2-1,config}
#在serverB上
mkdir -p /data/{shard1-2,shard2-2,config}
#在serverC上
mkdir -p /data/{shard1-3,shard2-3,config}

2.1配置shard1的Replica Sets:

#在serverA上启动shard1-1
mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-1/ --logpath /data/shard1-1/shard1-1.log --logappend --replSet shard1 --directoryperdb
#在serverB上启动shard1-2
mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-2/ --logpath /data/shard1-2/shard1-2.log --logappend --replSet shard1 --directoryperdb
#在serverC上启动shard1-3
mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-3/ --logpath /data/shard1-3/shard1-3.log --logappend --replSet shard1 --directoryperdb

用mongo连接其中一台主机的27018端口的mongod,初始化复制集shard1:
mongo –port 27018

点击(此处)折叠或打开

>config={_id:'shard1',members:[
{_id:0,host:'192.168.106.101:27018',priority:2},
{_id:1,host:'192.168.106.102:27018',priority:1},
{_id:2,host:'192.168.106.103:27018',arbiterOnly:true}]
}

>rs.initiate(config)

2.2 配置shard2的Replica Sets

 #在serverA上启动shard2-1
mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-1/ --logpath /data/shard2-1/shard2-1.log --logappend --replSet shard2 --directoryperdb
#在serverB上启动shard2-2
mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-2/ --logpath /data/shard2-2/shard2-2.log --logappend --replSet shard2 --directoryperdb
#在serverC上启动shard2-3
mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-3/ --logpath /data/shard2-3/shard2-3.log --logappend --replSet shard2 --directoryperdb

用mongo连接其中一台主机(建议在ServerB上)的27019端口的mongod,初始化复制集shard2:

mongo --port 27019

>config={_id:'shard2',members:[
{_id:0,host:'192.168.106.101:27019',arbiterOnly:true},
{_id:1,host:'192.168.106.102:27019',priority:1},
{_id:2,host:'192.168.106.103:27019',priority:2}]
}

>rs.initiate(config)

3.配置config server

#在3台主机中分别启动配置服务
mongod --configsvr --port 20000 --dbpath /data/config/ --logpath /data/config/config.log --replSet docdetection --logappend --fork

用mongo连接其中一台主机的20000端口的mongod,初始化复制集docdetection:

mongo --port 20000

configdb1={_id:'docdetection',members:[
{_id:0,host:'192.168.106.101:20000',priority:3},
{_id:1,host:'192.168.106.102:20000',priority:1},
{_id:2,host:'192.168.106.103:20000',priority:2}]
}

>rs.initiate(configdb1)

4.配置router server

#在3台主机中分别运行mongos服务
mongos --configdb docdetection/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork

5.配置分片(shard cluster)

#连接到其中一台机器的端口27017的mongos进程,并切换到admin数据库添加分片shard1shard2
mongo --port 27017


>use admin
>db.runCommand({addshard:"shard1/192.168.106.101:27018,192.168.106.102:27018,192.168.106.103:27018"})
>db.runCommand({addshard:"shard2/192.168.106.101:27019,192.168.106.102:27019,192.168.106.103:27019"})
#激活数据库(work)和集合(status)的分片功能:
>db.runCommand({enablesharding:"work"})
>db.runCommand({shardcollection:"work.status",key:{_id:1}})

将准备好的mongodb的数据导入到mongodb中
cd /usr/local/mongodb/bin
mongorestore -h localhost:27017 -d docdetection --dir /usr/local/mongodb/docdetection/

6.验证

#查看shard1的集合work.status和shard2的文档数几乎均分,且之和等于路由节点下查看的文档数:
mongo --port 27018 work
>db.status.count()
./mongo --port 27019 work

>db.status.count()

./mongo work
>db.status.count()


#查看分片的状态:

需要在mongs进程下访问:
mongo --port 27017
> printShardingStatus()

三、常见问题
1.【问题描述】执行初始化副本集时报错:

点击(此处)折叠或打开

> rs.initiate(config)
{
"ok" : 0,
"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.106.102:27018 failed with No route to host, 192.168.106.103:27018 failed with No route to host",
"code" : 74,
"codeName" : "NodeNotFound"
}

【解决方法】
开启防火墙的相关端口

点击(此处)折叠或打开

-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 27018 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27019 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 20000 -j ACCEPT

2.【问题描述】初始化副本集报错:

点击(此处)折叠或打开

> rs.initiate(config)
{
"ok" : 0,
"errmsg" : "This node, 192.168.106.101:27019, with _id 0 is not electable under the new configuration version 1 for replica set shard2",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}

【解决方法】
如果设置主机A为仲裁节点,那么不要在A主机上执行初始化操作,否则会报上述错误。

3.【问题描述】启动路由器mongos报错:

点击(此处)折叠或打开

./mongos --configdb 192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --forkFailedToParse: mirrored config server connections are not supported; for config server replica sets be sure to use the replica set connection stringtry './mongos --help' for more information

【解决方法】
从Mongodb3.2之后,启动mongos时需使用副本集名称,否则会报上述错误。例如:

点击(此处)折叠或打开

./mongos –configdb 副本集名称/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork