MongoDB 架构(四)

MongoDB 架构

主从(过时)

搭建选项

Master/slave options (old; use replica sets instead):
  --master                              master mode
  --slave                               slave mode
  --source arg                          when slave: specify master as 
                                        <server:port>
  --only arg                            when slave: specify a single database 
                                        to replicate
  --slavedelay arg                      specify delay (in seconds) to be used 
                                        when applying master ops to slave
  --autoresync                          automatically resync if slave data is 
                                        stale

优点:数据读写分离,数据冗余备份

缺点:无法实现自动故障转移、单机存储瓶颈(无法解决海量数据存储)

[root@centos ~]# mkdir master slave
#主机
[root@centos ~]# mongod --dbpath /root/master/ --port 27017 --fork --syslog --bind_ip_all --master
[root@centos ~]# mongod --dbpath /root/slave/ --port 27018 --fork --syslog --bind_ip_all --slave --source centos:27017 --autoresync

当连接从机需要执行rs.slaveOk()才可以查看从机器,从机器只读,可以通过db.isMaster()查看机器信息

副本集(升级版本主从)

优点:数据读写分离,数据冗余备份、自动故障转移

缺点:单机存储瓶颈(无法解决海量数据存储)

primary:主机模式,可以支持读写操作

Secondary:从机模式,同步主机数据,负责选举,能过在主机宕机自动选举出新的primary节点

Arbiter:仲裁节点,不参与存储,只负责在主机宕机的时候参与选举

No Voting:剥夺选举权,只负责存储数据和被选举

Secondary Only:只负责选举别的节点和数据同步。

扫描二维码关注公众号,回复: 5819244 查看本文章
[root@centos ~]# mkdir repl1 repl2 repl3
[root@centos ~]# mongod --dbpath /root/repl1/ --port 27017 --fork --syslog --bind_ip_all --replSet rs1
[root@centos ~]# mongod --dbpath /root/repl2/ --port 27018 --fork --syslog --bind_ip_all --replSet rs1
[root@centos ~]# mongod --dbpath /root/repl3/ --port 27019 --fork --syslog --bind_ip_all --replSet rs1
#随意连接一台机器,对集群做初始化,一般这一台机器一定primary节点,所有有关集群配置,都必须走primary节点
[root@centos ~]# mongo --host centos --port 27017
> var conf={
   _id : "rs1",
   members: [
      { _id: 0, host: "centos:27017" },
      { _id: 1, host: "centos:27018" },
      { _id: 2, host: "centos:27019" }
   ]
}
> rs.initiate(conf)
...
rs1:PRIMARY>
#添加节点
rs1:PRIMARY>rs.add("host:port")|rs.addArb("host:port")
#删除节点
rs1:PRIMARY>rs.remove("host:port")
rs1:PRIMARY> var cfg=rs.conf()
rs1:PRIMARY> cfg.members[0].priority=2
rs1:PRIMARY> rs.reconfig(cfg)

分片+副本集

ConfigServer:存储集群的元数据信息,mongodb3.4版本配置服务器必须部署成为副本集

ShardServer:存储实际的物理数据,存储集合中的部分信息,每一个shardserver也必须部署成为副本集

router:路由服务器,做为集群的访问代理服务,将用户的请求转发给配置服务器或者shard服务器。可以将一个庞大的mongodb的集群伪装成为单个mongod服务实例。

[root@centos ~]# mkdir cf1 cf2 cf3 shard1_1 shard1_2 shard1_3 shard2_1 shard2_2 shard2_3

#搭建副本集
[root@centos ~]# mongod --dbpath /root/shard1_1/ --port 27017 --fork --syslog --bind_ip_all --replSet rs1 --shardsvr 
[root@centos ~]# mongod --dbpath /root/shard1_2/ --port 27018 --fork --syslog --bind_ip_all --replSet rs1 --shardsvr 
[root@centos ~]# mongod --dbpath /root/shard1_3/ --port 27019 --fork --syslog --bind_ip_all --replSet rs1 --shardsvr 
[root@centos ~]# mongo --host centos --port 27017
> var conf={
   _id : "rs1",
   members: [
      { _id: 0, host: "centos:27017" },
      { _id: 1, host: "centos:27018" },
      { _id: 2, host: "centos:27019" }
   ]
}
> rs.initiate(conf)

[root@centos ~]# mongod --dbpath /root/shard2_1/ --port 37017 --fork --syslog --bind_ip_all --replSet rs2 --shardsvr 
[root@centos ~]# mongod --dbpath /root/shard2_2/ --port 37018 --fork --syslog --bind_ip_all --replSet rs2 --shardsvr 
[root@centos ~]# mongod --dbpath /root/shard2_3/ --port 37019 --fork --syslog --bind_ip_all --replSet rs2 --shardsvr 
[root@centos ~]# mongo --host centos --port 37017
> var conf={
   _id : "rs2",
   members: [
      { _id: 0, host: "centos:37017" },
      { _id: 1, host: "centos:37018" },
      { _id: 2, host: "centos:37019" }
   ]
}
> rs.initiate(conf)

[root@centos ~]# mongod --dbpath /root/cf1/ --port 47017 --fork --syslog --bind_ip_all --replSet cf --configsvr 
[root@centos ~]# mongod --dbpath /root/cf2/ --port 47018 --fork --syslog --bind_ip_all --replSet cf --configsvr 
[root@centos ~]# mongod --dbpath /root/cf3/ --port 47019 --fork --syslog --bind_ip_all --replSet cf --configsvr 
[root@centos ~]# mongo --host centos --port 47017
> var conf={
   _id : "cf",
   members: [
      { _id: 0, host: "centos:47017" },
      { _id: 1, host: "centos:47018" },
      { _id: 2, host: "centos:47019" }
   ]
}
> rs.initiate(conf)
#启动路由服务,用户可以通过连接该路由服务操作mongodb集群
[root@centos ~]# mongos --configdb cf/centos:47017,centos:47018,centos:47019 --fork --syslog --port 8000 --bind_ip_all

#配置分片
[root@centos ~]# mongo --host centos --port 8000
mongos> sh.addShard("rs1/centos:27017,centos:27018,centos:27019")
mongos> sh.addShard("rs2/centos:37017,centos:37018,centos:37019")
mongos> sh.enableSharding('baizhi')# 对该库下的表做分片
mongos> sh.shardCollection("baizhi.t_user",{_id:"hashed"})#对t_user做hashshard让数据均匀分布
mongos> sh.shardCollection( "baizhi.t_order", {_id:1,name:1})#做range方式,写性能不高,有利于区间检索
mongos> use baizhi
mongos> for(var i=0;i<1000;i++){
	db.t_user.insert({name:"user"+i})
	db.t_order.insert({name:"user"+i})
}
> db.t_user|t_order.stats();

上一篇:MongoDB之简单数据的统计与索引(三)

猜你喜欢

转载自blog.csdn.net/qq_42806727/article/details/89093476