MongoDB学习十一 --MongoDB的Replication实践

很牛X的主从集群,不过跟上面的主从复制还是有两点区别的。
<1>:  该集群没有特定的主数据库。

<2>:  如果哪个主数据库宕机了,集群中就会推选出一个从属数据库作为主数据库顶上,这就具备了自动故障恢复功能,很牛X的啊。好,我们现在就来试一下,首先把所有的cmd窗口关掉重新来,清掉db下的所有文件。

第一步:  建立集群,就得取个集群名字"rs0",在192.168.0.148上运行mongod --replSet "rs0"

1

第二步:以集群方式启动另一个数据库,在192.168.0.149上运行mongod --replSet "rs0"

2

第三步:初始化集群,Use rs.initiate() on the replica set member:,集群中的每台机都要执行这个,初始化完成后,最后一台机器就就了主服务器了primary

initial

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.

第四步,验证下配置,Verify the initial replica set configuration.Use rs.conf() to display the replica set configuration object:


第五步:添加其他的集群成员,这个要在PRIMARY那台机器上才能做。

Add the remaining members with the rs.add() method.示例rs.add("192.168.0.148")

第六步:检查状态 rs.status()

8

第七步:添加仲裁服务器,那么mongodb中跟sql server一样,仲裁只参与投票选举,这里我们把192.168.0.202(我的这台是windows的机器)的mongodb作为仲裁服务器

Create a data directory (e.g. dbPath) for the arbiter. The mongod instance uses the directory for configuration data. The directory will not hold the data set. For example, create the directory:D:\ProgramFiles\MongoDB\arb

Start the arbiter. Specify the data directory and the replica set name. The following, starts an arbiter using the /data/arb dbPath for the rs replica set:

mongod --port 30000 --dbpath D:\ProgramFiles\MongoDB\arb --replSet "rs0"

回到PRIMARY机器,添加促裁服务器,Connect to the primary and add the arbiter to the replica set. Use the rs.addArb() method, as in the following example:

rs.addArb("192.168.0.202:30000")

This operation adds the arbiter running on port 30000 on the 192.168.0.202 host.

9

添加完成后可以在促裁服务器上看到哪台是PRIAMRY,哪台是SECONDARY

sd

再次看最后的状态

10


注意:

由于上面的主服务器不是IP:PORT,而是机器名,所以需要在windows的那台仲裁服务器上配置hosts,

C:\Windows\System32\drivers\etc\hosts,添加192.168.0.149 JfbIpadServer02

也可以修改集群中的主机名为IP地址

在PRIMARY服务器上,要能查看到该memeber的配置,才能修改

cfg = rs.conf()
cfg.members[0].host = "192.168.0.149:27017"
rs.reconfig(cfg)

最后就都变成了IP+PORT的host了

arb2


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值