MongoDb Replica Set解决了容错和单点故障问题,但单台机器的存储和承受能力有限,Sharding就是为了海量存储和动态扩容而产生的。这才有了Replica Set+Sharding高可用架构。
Sharding Cluster主要包括如下三部分:
Shards:每个shard都是replica set,具有自动备份、容错、恢复能力,当然在开发环境你可配成单个mongod
Config Server:存储metadata,包括每个shard的基本信息和chunk信息,生产环境至少有3个config server
Mongos:集群的入口,客户端与之交互,复杂把请求转发到对应的shard上。
搭建MongoDB Replica Set+Sharding环境:
拓扑图:
说明:由于本地测试,部署将在一台机器完成,并且mongos和config server都只用了一个实例。
配置:
副本集rep1配置:
#master.conf配置 dbpath=/data/mongo_cluster/master logpath=/data/mongo_cluster/master/logs/master.log pidfilepath=/data/mongo_cluster/master/master.pid logappend=true replSet=rep1 bind_ip=192.168.15.130 port=10000 fork=true journal=true shardsvr=true #slave.conf配置 dbpath=/data/mongo_cluster/slave logpath=/data/mongo_cluster/slave/logs/slave.log pidfilepath=/data/mongo_cluster/slave/slave.pid logappend=true replSet=rep1 bind_ip=192.168.15.130 port=10001 fork=true journal=true shardsvr=true #arbiter.conf配置 dbpath=/data/mongo_cluster/arbiter logpath=/data/mongo_cluster/arbiter/logs/arbiter.log pidfilepath=/data/mongo_cluster/arbiter/arbiter.pid logappend=true replSet=rep1 bind_ip=192.168.15.130 port=10002 fork=true journal=true shardsvr=true
副本集rep2配置:
#master.conf配置 dbpath=/data/mongo_cluster2/master logpath=/data/mongo_cluster2/master/logs/master.log pidfilepath=/data/mongo_cluster2/master/master.pid logappend=true replSet=rep2 bind_ip=192.168.15.130 port=10004 fork=true journal=true shardsvr=true #slave.conf配置 dbpath=/data/mongo_cluster2/slave logpath=/data/mongo_cluster2/slave/logs/slave.log pidfilepath=/data/mongo_cluster2/slave/slave.pid logappend=true replSet=rep2 bind_ip=192.168.15.130 port=10005 fork=true journal=true shardsvr=true #arbiter.conf配置 dbpath=/data/mongo_cluster2/arbiter logpath=/data/mongo_cluster2/arbiter/logs/arbiter.log pidfilepath=/data/mongo_cluster2/arbiter/arbiter.pid logappend=true replSet=rep2 bind_ip=192.168.15.130 port=10006 fork=true journal=true shardsvr=true
配置Replica Set主、备、仲裁节点:(点击查看)
config server配置:
#config server配置configsvr.conf dbpath=/data/mongo_config_server logpath=/data/mongo_config_server/logs/configsvr.log pidfilepath=/data/mongo_config_server/configsvr.pid logappend=true bind_ip=192.168.15.130 port=10007 fork=true journal=true configsvr=true mongos配置: #mongos配置 mongos.conf logpath=/data/mongos/logs/mongos.log pidfilepath=/data/mongos/mongos.pid logappend=true bind_ip=192.168.15.130 port=10008 fork=true configdb=192.168.15.130:10007
启动:
#启动shard /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster/master/master.conf /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster/slave/slave.conf /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster/arbiter/arbiter.conf /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster2/master/master.conf /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster2/slave/slave.conf /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_cluster2/arbiter/arbiter.conf #启动config server /usr/local/mongodb3.0.5/bin/mongod -f /data/mongo_config_server/configsvr.conf #启动mongos /usr/local/mongodb3.0.5/bin/mongos -f /data/mongos/mongos.conf
配置mongos,设置分片规则:
(1)连接mongos与replica set
#进入mongos /usr/local/mongodb3.0.5/bin/mongo 192.168.15.130:10008 #连接mongos与replica set mongos> db.runCommand( { addshard : "rep1/192.168.15.130:10001"}); { "shardAdded" : "rep1", "ok" : 1 } mongos> db.runCommand( { addshard : "rep2/192.168.15.130:10004"}); { "shardAdded" : "rep2", "ok" : 1 } #查看分片配置 mongos> db.runCommand( { listshards : 1 } );
(2)设置分片规则并测试
#指定testdb分片生效 db.runCommand( { enablesharding :"testdb"}); #指定数据库里需要分片的集合和片键 db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) #测试 > use testdb; #插入测试数据 > for (var i = 1; i <= 1000; i++) db.table1.save({id:i,"test1":"testval1"}); #查看分片情况如下 > db.table1.stats();
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索配置
mongodb sharding、mongodb sharding集群、mongodb sharding key、mongodb sharding配置、mongodb 3.2 sharding,以便于您获取更多的相关知识。
时间: 2024-10-28 22:24:30