MongoDB复制原理就是主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
那什么是副本集呢?主从模式一主一从就是一个副本。MongoDB复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性;复制还允许您从硬件故障和服务中断中恢复数据。副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了之前的问题“主节点挂掉了,整个集群内会自动切换”。
· N 个节点的集群
· 任何节点可作为主节点
· 所有写入操作都在主节点上
· 自动故障转移
· 自动恢复
副本集还有以下几个需要注意的地方:
1. 最小构成是:primary,secondary,arbiter,一般部署是:primary,2 secondary。
2. 成员数应该为奇数,如果为偶数的情况下添加arbiter,arbiter不保存数据,只投票。
3. 最大50 members,但是只能有 7 voting members,其他是non-voting members。
借用网上找的两张架构图说明一下:
由图可以看到客户端连接到整个副本集,不关心具体哪一台机器是否挂掉。主服务器负责整个副本集的读写,副本集定期同步数据备份,一但主节点挂掉,副本节点就会选举一个新的主服务器,这一切对于应用服务器不需要关心。我们看一下主服务器挂掉后的架构:
副本集中的副本节点在主节点挂掉后通过心跳机制检测到后,就会在集群内发起主节点的选举机制,自动选举一位新的主服务器。更加详细的副本机制请参考搭建高可用mongodb集群(三)—— 深入副本集内部机制http://www.lanceyan.com/tech/mongodb_repset2.html
官方推荐的副本集机器数量至少为3个,我这里就以三个节点为例介绍。
1. 环境准备
192.168.221.161:27017作为主节点,192.168.221.161:27018,192.168.221.161:27019作为两个副本节点
2. 分别创建三个节点所需要的文件夹
mkdir /data/db{1..3} #存放三个节点数据文件 touch /usr/local/mongodb/mongodb{1..3}.log #存放三个节点的日志
3. 创建三个节点的配置文件
[root@MidApp mongodb]# cat mongodb1.conf dbpath=/data/db1 logpath=/usr/local/mongodb/logs/mongodb1.log logappend=true port=27017 fork=true auth=false nohttpinterface=false bind_ip=192.168.221.161 journal=true quiet=true replSet=repset
[root@MidApp mongodb]# cat mongodb2.conf dbpath=/data/db2 logpath=/usr/local/mongodb/logs/mongodb2.log logappend=true port=27018 fork=true auth=false nohttpinterface=false bind_ip=192.168.221.161 journal=true quiet=true replSet=repset
[root@MidApp mongodb]# cat mongodb3.conf dbpath=/data/db3 logpath=/usr/local/mongodb/logs/mongodb3.log logappend=true port=27019 fork=true auth=false nohttpinterface=false bind_ip=192.168.221.161 journal=true quiet=true replSet=repset
4. 分别启动三个节点
mongod -f mongodb1.conf mongod -f mongodb2.conf mongod -f mongodb3.conf
查看日志输出,没有找到replica的配置信息
2017-11-13T16:34:46.212-0800 I CONTROL [initandlisten] options: { config: "mongodb1.conf", net: { bindIp: "192.168.221.160", http: { enabled: true }, port: 27017 }, processManagement: { fork: true }, replication: { replSet: "repset" }, security: { authorization: "disabled" }, storage: { dbPath: "/data/db1", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/usr/local/mongodb/logs/mongodb1.log", quiet: true } } 2017-11-13T16:34:46.214-0800 I INDEX [initandlisten] allocating new ns file /data/db1/local.ns, filling with zeroes... 2017-11-13T16:34:46.216-0800 I JOURNAL [journal writer] Journal writer thread started 2017-11-13T16:34:46.492-0800 I STORAGE [FileAllocator] allocating new datafile /data/db1/local.0, filling with zeroes... 2017-11-13T16:34:46.492-0800 I STORAGE [FileAllocator] creating directory /data/db1/_tmp 2017-11-13T16:34:46.501-0800 I STORAGE [FileAllocator] done allocating datafile /data/db1/local.0, size: 64MB, took 0.001 secs 2017-11-13T16:34:46.520-0800 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument Did not find replica set configuration document in local.system.replset
5. 初始化副本集
任选一个节点登录进去,可以看到show dbs是不可用,因为还没初始化副本集
[root@MidApp mongodb]# mongo 192.168.221.161 MongoDB shell version: 3.0.6 connecting to: 192.168.221.161/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > show dbs 2017-11-13T16:39:33.235-0800 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at Error (<anonymous>) at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 > use admin switched to db admin
定义副本集配置变量,这里的 _id:”repset” 和上面配置文件的参数“ –replSet repset” 要保持一样
>config= {_id:"repset",members:[ {_id:0,host:"192.168.221.161:27017"}, {_id:1,host:"192.168.221.161:27018"}, {_id:2,host:"192.168.221.161:27019"}] }
查看输出信息:
{ "_id" : "repset", "members" : [ { "_id" : 0, "host" : "192.168.221.161:27017" }, { "_id" : 1, "host" : "192.168.221.161:27018" }, { "_id" : 2, "host" : "192.168.221.161:27019" } ] }
初始化副本集配置
> rs.initiate(config); { "ok" : 1 }
查看集群节点状态:
repset:OTHER> rs.status() { "set" : "repset", "date" : ISODate("2017-11-14T00:49:25.782Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "192.168.221.161:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 695, "optime" : Timestamp(1510620540, 1), "optimeDate" : ISODate("2017-11-14T00:49:00Z"), "electionTime" : Timestamp(1510620544, 1), "electionDate" : ISODate("2017-11-14T00:49:04Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "192.168.221.161:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 25, "optime" : Timestamp(1510620540, 1), "optimeDate" : ISODate("2017-11-14T00:49:00Z"), "lastHeartbeat" : ISODate("2017-11-14T00:49:24.739Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T00:49:24.762Z"), "pingMs" : 0, "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.221.161:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 25, "optime" : Timestamp(1510620540, 1), "optimeDate" : ISODate("2017-11-14T00:49:00Z"), "lastHeartbeat" : ISODate("2017-11-14T00:49:24.739Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T00:49:24.762Z"), "pingMs" : 0, "configVersion" : 1 } ], "ok" : 1 }
整个副本及已经搭建成功了!
6. 测试副本集数据复制功能
在主节点登录mongodb
[root@MidApp mongodb]# mongo 192.168.221.161:27017 MongoDB shell version: 3.0.6 connecting to: 192.168.221.161:27017/test repset:PRIMARY> use test; switched to db test repset:PRIMARY> db.testdb.insert({"test1":"item1"}) WriteResult({ "nInserted" : 1 }) repset:PRIMARY> exit bye
在副本节点登录查看数据:
[root@MidApp logs]# mongo 192.168.221.161:27018 MongoDB shell version: 3.0.6 connecting to: 192.168.221.161:27018/test repset:SECONDARY> use test switched to db test repset:SECONDARY> db.getMongo().setSlaveOk();#mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读 repset:SECONDARY> db.testdb.find() { "_id" : ObjectId("5a0a3e8d40637405ab003b39"), "test1" : "item1" }
7. 测试副本集failover功能
手动把现在的主节点停掉,查看集群状态:
repset:SECONDARY> rs.status() { "set" : "repset", "date" : ISODate("2017-11-14T00:57:14.753Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "192.168.221.161:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2017-11-14T00:57:13.304Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T00:57:09.285Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to 192.168.221.161:27017; couldn‘t connect to server 192.168.221.161:27017 (192.168.221.161), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.221.161:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1167, "optime" : Timestamp(1510620813, 2), "optimeDate" : ISODate("2017-11-14T00:53:33Z"), "infoMessage" : "could not find member to sync from", "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "192.168.221.161:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 17, "optime" : Timestamp(1510620813, 2), "optimeDate" : ISODate("2017-11-14T00:53:33Z"), "lastHeartbeat" : ISODate("2017-11-14T00:57:13.317Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T00:57:13.330Z"), "pingMs" : 0, "electionTime" : Timestamp(1510621032, 1), "electionDate" : ISODate("2017-11-14T00:57:12Z"), "configVersion" : 1 } ], "ok" : 1 }
可以看到原来的主节点状态现在已经变成了不可达,192.168.221.161:27019已经变成新的主节点
8. 再次启动原来的主节点,发现192.168.221.161:27017还是SECONDARY
repset:SECONDARY> rs.status() { "set" : "repset", "date" : ISODate("2017-11-14T17:02:13.837Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "192.168.221.161:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5, "optime" : Timestamp(1510620813, 2), "optimeDate" : ISODate("2017-11-14T00:53:33Z"), "lastHeartbeat" : ISODate("2017-11-14T17:02:12.642Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T17:02:11.902Z"), "pingMs" : 0, "configVersion" : 1 }, { "_id" : 1, "name" : "192.168.221.161:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 59066, "optime" : Timestamp(1510620813, 2), "optimeDate" : ISODate("2017-11-14T00:53:33Z"), "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "192.168.221.161:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 57916, "optime" : Timestamp(1510620813, 2), "optimeDate" : ISODate("2017-11-14T00:53:33Z"), "lastHeartbeat" : ISODate("2017-11-14T17:02:12.308Z"), "lastHeartbeatRecv" : ISODate("2017-11-14T17:02:12.080Z"), "pingMs" : 0, "electionTime" : Timestamp(1510621032, 1), "electionDate" : ISODate("2017-11-14T00:57:12Z"), "configVersion" : 1 } ], "ok" : 1 }
现在,mongodb的副本集群已经完成了。
关于mongodb的读写分离和分片的内容以后有时间继续学习···
本文出自 “清淼淡写” 博客,请务必保留此出处http://qingmiao.blog.51cto.com/7286083/1981700
原文:http://qingmiao.blog.51cto.com/7286083/1981700