第35章:MongoDB-集群--Master Slave(主从复制)

①主从复制

最基本的设置方式就是建立一个主节点和一个或多个从节点,每个从节点要知道主节点的地址。采用双机备份后主节点挂掉了后从节点可以接替主机继续服务,所以这种模式比单节点的高可用性要好很多。

②注意点

1)在数据库集群中要明确的知道谁是主服务器,主服务器只有一台.

2)从服务器要知道自己的数据源也就是对应的主服务是谁.

3)--master用来确定主服务器,--slave 和 --source 来控制从服务器

③主从复制的原理

在主从结构中,主节点的操作记录成为oplog(operation log)。oplog存储在一个系统数据库local的集合oplog.$main中,这个集合的每个文档都代表主节点上执行的一个操作。

从服务器会定期从主服务器中获取oplog记录,然后在本机上执行!对于存储oplog的集合,MongoDB采用的是固定集合,也就是说随着操作过多,新的操作会覆盖旧的操作!

主从复制搭建--单机

 

1:先创建几个存放数据的文件夹,比如在前面的dbs下面创建db1,db2

同理在前面的logs下面创建logs1,logs2

2:在启动MongoDB服务器的时候,使用--replSet副本集名称选项,如

./mongod --dbpath ../dbs/db1 --logpath ../logs/logs1 --port 20001 --fork --master

3:然后再启动两个,端口为20002,当然要修改相应的数据文件路径和日志路径,副本集名称

./mongod --dbpath ../dbs/db2 --logpath ../logs/logs2 --port 20002 --fork --slave  --source localhost:20001

4:

./mongo localhost:20001

./mongo localhost:20002

5:添加和删除源

除了可以在启动从节点的时候,制定source外,也可以在从节点的local数据库的sources集合里面添加主节点的信息,如:

db.sources.insert({“host”:”ip:port”});

不用了,就remove就好了

 

主从复制搭建--多机

 

1)机器环境

 

182.48.115.238    master-node

182.48.115.236    slave-node

两台机器都关闭防火墙和selinux

2)主从配置

.............master-node节点配置.............

[root@master-node ~]# vim /usr/local/mongodb/mongodb.conf

port=27017

bind_ip = 182.48.115.238

dbpath=/usr/local/mongodb/data

logpath=/usr/local/mongodb/log/mongo.log

logappend=true

journal = true

fork = true

master = true        //确定自己是主服务器

 

 

[root@slave-node ~]# bin/mongod -f mongodb.conf

./mongo IP:PORT

 

.............slave-node节点配置.............

[root@slave-node ~]# vim /usr/local/mongodb/mongodb.conf

port=27017

dbpath=/usr/local/mongodb/data

logpath=/usr/local/mongodb/log/mongo.log

logappend=true

journal = true

fork = true

bind_ip = 182.48.115.236            

source = 182.48.115.238:27017

slave = true

 

 

 

[root@slave-node ~]# bin/mongod -f mongodb.conf

./mongo IP:PORT

 

 

 

 

⑥测试

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Lucky-stars/p/10556065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值