本篇文章为大家展示了mongodb中怎么搭建shard_replica集群,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。参考官方文档,大致步骤
第一步:创建Config Server实例,配置Config Server实例成Replica Set第二步:创建Shard Server实例,配置Shard Server实例成Replica Set第三步:创建Route Server实例,把Config Server实例和Shard Server实例配置进来第四步:创建数据库并执行db.printShardingStatus()命令验证涉及replSet、shardsvr、configsvr几个参数replSet:配置replica set,replica set中的所有主机必须具有相同的replica set名称。shardsvr:将此mongod实例配置为分片集群中的分片。默认端口是27018。configsvr:声明此mongod实例作为分片集群的配置服务器。默认端口是27019,默认的dbpath目录是/data/configdb,配置服务器存储集群的元数据和配置设置。从MongoDB 3.4开始,配置服务器必须部署为一个replicate set副本集,为了防止这个配置服务是单点服务。如果你的集群只有一个配置服务器,一旦这个配置服务器出现故障则集群将不可用涉及Config Server、Shard Server、Route Server几个实例1. Config Server: mongod实例,存储了整个Cluster Metadata,其中包括Chunk信息。2. Shard Server: mongod实例,用于存储实际的数据块,实际生产环境中一个Shard Server角色可由几台机器组个一个Replica Set承担,防止主机单点故障。3. Route Server: mongos实例,前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。部署规划2台物理设备服务器,ip分别为172.22.138.157、172.22.138.158两台服务器上分别创建端口是29001的Config Server实例,这两台服务器上的Config Server实例组成Replica Set模式两台服务器上分别创建端口分别是28001、28002、28003的shard server实例,两台服务器上相同端口的shard server实例组成Replica Set模式两台服务器上分别创建端口是27001的Route Server实例部署架构外部程序–>Route Server实例–>Config Server实例–>shard server实例Route Server实例不是单点,两台服务器上都有,配置都是一样,都是指向一样的Config Server实例Config Server实例不是单点,是Replica Set模式shard server实例不是单点,是Replica Set模式第一步:创建Config Server实例,配置Config Server实例成Replica Set1.1、在两台服务器上分别编辑如下文件(两个重点参数configsvr、replSet)cat /mongodb/configsvr29001.confport=29001dbpath=/mongodb/mongoconfig29001logpath=/mongodb/log/mongoconfig29001.logconfigsvr=truereplSet=config29001fork=truelogappend=truedirectoryperdb=truebind_ip=0.0.0.0pidfilepath=/mongodb/mongoconfig29001.pid1.2、两台服务器上分别启动Config Server实例mongod -f /mongodb/configsvr29001.conf1.3、配置两台服务器的Config Server实例成Replica Set 登录任意一台服务器执行如下操作即可mongo –host 172.22.138.157 –port 29001rs.initiate( { _id: “config29001”, configsvr: true, members: [ { _id : 0, host : “172.22.138.157:29001” }, { _id : 1, host : “172.22.138.158:29001” } ] })第二步创建Shard Server实例,配置Shard Server实例成Replica Set2.1、在两台服务器上分别编辑如下三个文件(两个重点参数shardsvr、replSet)cat /mongodb/shardsvr28001.confport=28001dbpath=/mongodb/mongoshard28001logpath=/mongodb/log/mongoshard28001.logshardsvr=truereplSet=shard28001fork=truelogappend=truedirectoryperdb=trueverbose=truebind_ip=0.0.0.0pidfilepath=/mongodb/mongoshard28001.pidcat /mongodb/shardsvr28002.confport=28002dbpath=/mongodb/mongoshard28002logpath=/mongodb/log/mongoshard28002.logshardsvr=truereplSet=shard28002fork=truelogappend=truedirectoryperdb=trueverbose=truebind_ip=0.0.0.0pidfilepath=/mongodb/mongoshard28002.pidcat /mongodb/shardsvr28003.confport=28003dbpath=/mongodb/mongoshard28003logpath=/mongodb/log/mongoshard28003.logshardsvr=truereplSet=shard28003fork=truelogappend=truedirectoryperdb=trueverbose=truebind_ip=0.0.0.0pidfilepath=/mongodb/mongoshard28003.pid2.2、两台服务器上分别启动Shard Server实例mongod -f /mongodb/shardsvr28001.confmongod -f /mongodb/shardsvr28002.confmongod -f /mongodb/shardsvr28003.conf2.3、配置两台服务器的3个Shard Server实例成Replica Set 登录任意一台服务器执行如下操作即可mongo –host 172.22.138.157 –port 28001rs.initiate( { _id: “shard28001”, members: [ { _id : 0, host : “172.22.138.157:28001” }, { _id : 1, host : “172.22.138.158:28001” } ] })mongo –host 172.22.138.157 –port 28002rs.initiate( { _id: “shard28002”, members: [ { _id : 0, host : “172.22.138.157:28002” }, { _id : 1, host : “172.22.138.158:28002” } ] })mongo –host 172.22.138.157 –port 28003rs.initiate( { _id: “shard28003”, members: [ { _id : 0, host : “172.22.138.157:28003” }, { _id : 1, host : “172.22.138.158:28003” } ] })第三步创建Route Server实例3.1、在两台服务器上分别编辑如下文件(重点参数configdb,把上面第一步的Config Server实例配置进来)cat /mongodb/Routesvr27001.confport=27001logpath=/mongodb/log/mongoRoute27001.logconfigdb=config29001/172.22.138.157:29001,172.22.138.158:29001fork=truebind_ip=0.0.0.0pidfilepath=/mongodb/mongoRoute27001.pid3.2、两台服务器上分别启动Config Server实例mongos -f /mongodb/Routesvr27001.conf3.3、添加shard分片到集群,把上面第二步的Shard Server实例配置进来 登录任意一台服务器执行如下操作即可mongo –host 172.22.138.157 –port 27001sh.addShard( “shard28001/172.22.138.157:28001,172.22.138.158:28001”)sh.addShard( “shard28002/172.22.138.157:28002,172.22.138.158:28002”)sh.addShard( “shard28003/172.22.138.157:28003,172.22.138.158:28003”)第四步验证登录任意一台mongos实例,创建四个数据库mongo –host 172.22.138.157 –port 27001mongos> use TDB1mongos> db.createCollection(“test01″)mongos> db.test01.insert({hid:1,hname:”w001”})mongos> use TDB2mongos> db.createCollection(“test02″)mongos> db.test02.insert({hid:11,hname:”x001”})mongos> use TDB3mongos> db.createCollection(“test03″)mongos> db.test03.insert({hid:111,hname:”y001”})mongos> use TDB4mongos> db.createCollection(“test04″)mongos> db.test04.insert({hid:1111,hname:”z001”})连接任意一台mongos实例,都可以看到这四个数据库,执行db.printShardingStatus()看到四个数据库被分到了不同的shard上了,TDB1分到了shard28001上,TDB2TDB3分到了shard28002上,TDB4分到了shard28003上,登录mongod实例shard集群分片28001端口只看到TDB1数据库,登录mongod实例集群分片28002端口只看到TDB2、TDB3数据库,登录mongod实例集群分片28003端口只看到TDB4数据库,通过mongos,用户看到所有数据库,底下的shard server对用户而言是可以不用了解的mongo –host 172.22.138.158 –port 27001mongos> show dbsTDB1 0.000GBTDB2 0.000GBTDB3 0.000GBTDB4 0.000GBadmin 0.000GBconfig 0.001GBmongos> db.printShardingStatus()— Sharding Status — sharding version: { “_id” : 1, “minCompatibleVersion” : 5, “currentVersion” : 6, “clusterId” : ObjectId(“5d09cd1c38fe676e9fb94a92”) } shards: { “_id” : “shard28001”, “host” : “shard28001/172.22.138.157:28001,172.22.138.158:28001”, “state” : 1 } { “_id” : “shard28002”, “host” : “shard28002/172.22.138.157:28002,172.22.138.158:28002”, “state” : 1 } { “_id” : “shard28003”, “host” : “shard28003/172.22.138.157:280免费云主机域名03,172.22.138.158:28003″, “state” : 1 } active mongoses: “3.6.12” : 2 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { “_id” : “TDB1”, “primary” : “shard28001”, “partitioned” : false } { “_id” : “TDB2”, “primary” : “shard28002”, “partitioned” : false } { “_id” : “TDB3”, “primary” : “shard28002”, “partitioned” : false } { “_id” : “TDB4”, “primary” : “shard28003”, “partitioned” : false } { “_id” : “config”, “primary” : “config”, “partitioned” : true } config.system.sessions shard key: { “_id” : 1 } unique: false balancing: true chunks: shard28001 1 { “_id” : { “$minKey” : 1 } } –>> { “_id” : { “$maxKey” : 1 } } on : shard28001 Timestamp(1, 0)mongo –host 172.22.138.157 –port 28001shard28001:PRIMARY> show dbsTDB1 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GBmongo –host 172.22.138.157 –port 28002shard28002:PRIMARY> show dbsTDB2 0.000GBTDB3 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GBmongo –host 172.22.138.157 –port 28003shard28003:PRIMARY> show dbsTDB4 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GB第五步如果想对某个数据库里面的表分片存储,即A表一部分放入shared1,一部分放入shard2,就需要先使用sh.enableSharding(“db_name”)对这个数据库启用分片,数据库启用分片后,上面第四步验证结果”partitioned” : false就变成了”partitioned” : true。如果该表已包含数据,则必须在使用shardCollection()对表分片之前使用db.collection.createIndex()方法在分片键上创建索引。如果表为空,MongoDB将创建索引作为shardCollection()的一部分。mongo –host 172.22.138.158 –port 27001mongos> use testdbmongos> db.createCollection(“table1″)mongos> db.printShardingStatus() –此时testdb对应的数据库”partitioned” : falsemongos> sh.enableSharding(“testdb”)mongos> db.printShardingStatus() –此时testdb对应的数据库”partitioned” : true对表testdb.table1按age字段范围分片(代号为1),指定散列分片键对空集合进行分片时最初要创建的块数为5mongos> sh.shardCollection(“testdb.table1”,{age:1},{numInitialChunks:5})mongos> for (var i=0;i
对表testdb.table2按age字段范围分片(代号为1),指定散列分片键对空集合进行分片时最初要创建的块数为1mongos> db.createCollection(“table2”)mongos> sh.shardCollection(“testdb.table2”,{age:1},{numInitialChunks:1})mongos> for (var i=0;i
对表testdb.table3按age字段范围分片(代号为hashed),指定散列分片键对空集合进行分片时最初要创建的块数为1mongos> db.createCollection(“table3”)mongos> sh.shardCollection(“testdb.table2″,{age:”hashed”},{numInitialChunks:1})mongos> for (var i=0;i
上述内容就是mongodb中怎么搭建shard_replica集群,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注百云行业资讯频道。
相关推荐: Oracle Study 之–AIX5.3安装Oracle 11gR2
Oracle Study 之–AIX5.3安装Oracle 11gR2系统环境: 操作系统: AIX5300-09 Oracle: Oracle 10gR2(64) 对于AIX5.3的版本,如果安装Oracle 11g需要将系统升级到AIX 5300-09的…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。