参考链接:
一、MongoDB副本集介绍
前面我们介绍了安装MongoDB及其相关的使用,但是其会存在下面的问题:
主节点挂了能否自动切换连接?目前需要手工切换。
主节点的读写压力过大如何解决?
从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大?
数据压力大到机器支撑不了的时候能否做到自动扩展?
这篇文章看完这些问题就可以搞定了。NoSQL的产生就是为了解决大数据量、高扩展性、高性能、灵活数据模型、高可用性。但是光通过主从模式的架构远远达不到上面几点,由此MongoDB设计了副本集和分片的功能。这篇文章主要介绍副本集。
注意:mongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模式。
1、简介
那什么是副本集呢?打魔兽世界总说打副本,其实这两个概念差不多一个意思。游戏里的副本是指玩家集中在高峰时间去一个场景打怪,会出现玩家暴多怪物少的情况,游戏开发商为了保证玩家的体验度,就为每一批玩家单独开放一个同样的空间同样的数量的怪物,这一个复制的场景就是一个副本,不管有多少个玩家各自在各自的副本里玩不会互相影响。 mongoDB的副本也是这个,主从模式其实就是一个单副本的应用,没有很好的扩展性和容错性。而副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了上面第一个问题“主节点挂掉了,整个集群内会自动切换”。难怪mongoDB官方推荐使用这种模式。我们来看看mongoDB副本集的架构图:
由图可以看到客户端连接到整个副本集,不关心具体哪一台机器是否挂掉。主服务器负责整个副本集的读写,副本集定期同步数据备份,一但主节点挂掉,副本节点就会选举一个新的主服务器,这一切对于应用服务器不需要关心。我们看一下主服务器挂掉后的架构:
副本集中的副本节点在主节点挂掉后通过心跳机制检测到后,就会在集群内发起主节点的选举机制,自动选举一位新的主服务器。看起来很牛X的样子,我们赶紧操作部署一下! 官方推荐的副本集机器数量为至少3个,那我们也按照这个数量配置测试。
二、环境部署
1、环境介绍
2、同步时间
[root@www.example.com ~]# ntpdate 202.120.2.101[root@node1.example.com ~]# ntpdate 202.120.2.101[root@node2.example.com ~]# ntpdate 202.120.2.101
3、安装mongodb
[root@www.example.com ~]# yum localinstall -y mongo-10gen-2.4.14-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.14-mongodb_1.x86_64.rpm [root@node1.example.com ~]# yum localinstall -y mongo-10gen-2.4.14-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.14-mongodb_1.x86_64.rpm [root@node2.example.com ~]# yum localinstall -y mongo-10gen-2.4.14-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.14-mongodb_1.x86_64.rpm
4、数据库初始化
[root@www.example.com ~]# mkdir /data/mongodb/[root@www.example.com ~]# chown -R mongod.mongod /data/mongodb/[root@www.example.com ~]# vim /etc/mongod.conf dbpath=/data/mongodbreplSet = testrs0rest = true[root@www.example.com ~]# service mongod restartStopping mongod: [确定]Starting mongod: about to fork child process, waiting until server is ready for connections.forked process: 5376all output going to: /var/log/mongo/mongod.logchild process started successfully, parent exiting [确定][root@node1.example.com ~]# vim /etc/mongod.confdbpath=/data/mongodbreplSet = testrs0rest = true[root@node1.example.com ~]# mkdir -p /data/mongodb[root@node1.example.com ~]# chown -R mongod.mongod /data/mongodb/[root@node1.example.com ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 4131all output going to: /var/log/mongo/mongod.logchild process started successfully, parent exiting [确定][root@node2.example.com ~]# vim /etc/mongod.confdbpath=/data/mongodbreplSet = testrs0rest = true[root@node2.example.com ~]# mkdir -p /data/mongodb[root@node2.example.com ~]# chown -R mongod.mongod /data/mongodb[root@node2.example.com ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 3143all output going to: /var/log/mongo/mongod.logchild process started successfully, parent exiting [确定]
主节点初始化:
> rs.initiate(){"info2" : "no configuration explicitly specified -- making one","me" : "www.example.com:27017","info" : "Config now saved locally. Should come online in about a minute.","ok" : 1}查看当前节点的信息:testrs0:PRIMARY> rs.status(){"set" : "testrs0","date" : ISODate("2015-09-26T10:49:29Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "www.example.com:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 511,"optime" : Timestamp(1443264538, 1),"optimeDate" : ISODate("2015-09-26T10:48:58Z"),"self" : true}],"ok" : 1}
查看节点的信息:
testrs0:PRIMARY> db.isMaster(){"setName" : "testrs0","ismaster" : true,"secondary" : false,"hosts" : ["www.example.com:27017"],"primary" : "www.example.com:27017","me" : "www.example.com:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-09-26T10:51:02.547Z"),"ok" : 1}
添加node1、node2节点并查看副本集的状态:
testrs0:PRIMARY> rs.add("192.168.1.9:27017"){ "ok" : 1 }testrs0:PRIMARY> rs.add("192.168.1.10:27017"){ "ok" : 1 }testrs0:PRIMARY> rs.status(){"set" : "testrs0","date" : ISODate("2015-09-26T10:55:13Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "www.example.com:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 855,"optime" : Timestamp(1443264886, 1),"optimeDate" : ISODate("2015-09-26T10:54:46Z"),"self" : true},{"_id" : 1,"name" : "192.168.1.9:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 131,"optime" : Timestamp(1443264886, 1),"optimeDate" : ISODate("2015-09-26T10:54:46Z"),"lastHeartbeat" : ISODate("2015-09-26T10:55:12Z"),"lastHeartbeatRecv" : ISODate("2015-09-26T10:55:13Z"),"pingMs" : 0,"syncingTo" : "www.example.com:27017"},{"_id" : 2,"name" : "192.168.1.10:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 27,"optime" : Timestamp(1443264886, 1),"optimeDate" : ISODate("2015-09-26T10:54:46Z"),"lastHeartbeat" : ISODate("2015-09-26T10:55:12Z"),"lastHeartbeatRecv" : ISODate("2015-09-26T10:55:12Z"),"pingMs" : 0,"syncingTo" : "www.example.com:27017"}],"ok" : 1}
查看节点的信息:
testrs0:PRIMARY> db.isMaster(){"setName" : "testrs0","ismaster" : true,"secondary" : false,"hosts" : ["www.example.com:27017","192.168.1.10:27017","192.168.1.9:27017"],"primary" : "www.example.com:27017","me" : "www.example.com:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-09-26T10:56:03.254Z"),"ok" : 1}
登录从节点查看各节点信息:
[root@node1.example.com ~]# mongoMongoDB shell version: 2.4.14connecting to: testtestrs0:SECONDARY> db.isMaster(){"setName" : "testrs0","ismaster" : false,"secondary" : true,"hosts" : ["192.168.1.9:27017","192.168.1.10:27017","www.example.com:27017"],"primary" : "www.example.com:27017","me" : "192.168.1.9:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-09-26T10:57:14.971Z"),"ok" : 1}
6、登录web窗口查看信息
登录web接口,可以听过Commands接口查看mongodb的信息:
点击replSetGetStatus所查看的信息:
点击isMaster所查看的信息:
7、验证副本集同步数据
主节点插入数据:
testrs0:PRIMARY> db.testcoll.insert({Name: "bols",Age: 25,Gender: "F"})testrs0:PRIMARY> db.testcoll.find(){ "_id" : ObjectId("56067c642896c9fb293b5892"), "Name" : "bols", "Age" : 25, "Gender" : "F" }
从节点需要使用可以读取数据的命令,才能查询数据:
testrs0:SECONDARY> rs.slaveOk()testrs0:SECONDARY> db.testcoll.find(){ "_id" : ObjectId("56067c642896c9fb293b5892"), "Name" : "bols", "Age" : 25, "Gender" : "F" }
[root@node2.example.com ~]# mongoMongoDB shell version: 2.4.14connecting to: testtestrs0:SECONDARY> db.testcoll.find()error: { "$err" : "not master and slaveOk=false", "code" : 13435 }testrs0:SECONDARY> rs.slaveOk()testrs0:SECONDARY> db.testcoll.find(){ "_id" : ObjectId("56067c642896c9fb293b5892"), "Name" : "bols", "Age" : 25, "Gender" : "F" }
8、验证副本集是否能做到故障转移
[
root@www.example.com ~]# service mongod stopStopping mongod: [确定]在node1节点上查看到主节点已转移到node2节点上:testrs0:SECONDARY> rs.status(){"set" : "testrs0","date" : ISODate("2015-09-26T11:14:02Z"),"myState" : 2,"syncingTo" : "192.168.1.10:27017","members" : [{"_id" : 0,"name" : "www.example.com:27017","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"optime" : Timestamp(1443265636, 1),"optimeDate" : ISODate("2015-09-26T11:07:16Z"),"lastHeartbeat" : ISODate("2015-09-26T11:14:00Z"),"lastHeartbeatRecv" : ISODate("2015-09-26T11:13:43Z"),"pingMs" : 0},{"_id" : 1,"name" : "192.168.1.9:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 1783,"optime" : Timestamp(1443265636, 1),"optimeDate" : ISODate("2015-09-26T11:07:16Z"),"errmsg" : "syncing to: 192.168.1.10:27017","self" : true},{"_id" : 2,"name" : "192.168.1.10:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 45,"optime" : Timestamp(1443265636, 1),"optimeDate" : ISODate("2015-09-26T11:07:16Z"),"lastHeartbeat" : ISODate("2015-09-26T11:14:01Z"),"lastHeartbeatRecv" : ISODate("2015-09-26T11:14:01Z"),"pingMs" : 1,"syncingTo" : "www.example.com:27017"}],"ok" : 1}
通过node2节点查看其是否为主:
testrs0:PRIMARY> db.isMaster(){"setName" : "testrs0","ismaster" : true,"secondary" : false,"hosts" : ["192.168.1.10:27017","192.168.1.9:27017","www.example.com:27017"],"primary" : "192.168.1.10:27017","me" : "192.168.1.10:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-09-26T11:15:48.726Z"),"ok" : 1}
node2上插入数据:
testrs0:PRIMARY> db.testcoll.insert({Name: "longls",Age: 24,Gender: "F"})testrs0:PRIMARY> db.testcoll.find(){ "_id" : ObjectId("56067c642896c9fb293b5892"), "Name" : "bols", "Age" : 25, "Gender" : "F" }{ "_id" : ObjectId("56067ec6e121af6dd6618634"), "Name" : "longls", "Age" : 24, "Gender" : "F" }
原主节点恢复使用,发现不能夺回成为主节点:
[root@www.example.com ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 5955all output going to: /var/log/mongo/mongod.logchild process started successfully, parent exiting [确定][root@www.example.com ~]# mongoMongoDB shell version: 2.4.14connecting to: testtestrs0:SECONDARY> rs.status(){ "set" : "testrs0", "date" : ISODate("2015-09-26T11:18:31Z"), "myState" : 2, "syncingTo" : "192.168.1.9:27017", "members" : [ { "_id" : 0, "name" : "www.example.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 20, "optime" : Timestamp(1443266246, 1), "optimeDate" : ISODate("2015-09-26T11:17:26Z"), "errmsg" : "syncing to: 192.168.1.9:27017", "self" : true }, { "_id" : 1, "name" : "192.168.1.9:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 20, "optime" : Timestamp(1443266246, 1), "optimeDate" : ISODate("2015-09-26T11:17:26Z"), "lastHeartbeat" : ISODate("2015-09-26T11:18:31Z"), "lastHeartbeatRecv" : ISODate("2015-09-26T11:18:30Z"), "pingMs" : 0, "syncingTo" : "192.168.1.10:27017" }, { "_id" : 2, "name" : "192.168.1.10:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 20, "optime" : Timestamp(1443266246, 1), "optimeDate" : ISODate("2015-09-26T11:17:26Z"), "lastHeartbeat" : ISODate("2015-09-26T11:18:31Z"), "lastHeartbeatRecv" : ISODate("2015-09-26T11:18:30Z"), "pingMs" : 0 } ], "ok" : 1}
查看是否能够查询刚才在node2节点上插入的数据:
testrs0:SECONDARY> rs.slaveOk()testrs0:SECONDARY> db.testcoll.find(){ "_id" : ObjectId("56067c642896c9fb293b5892"), "Name" : "bols", "Age" : 25, "Gender" : "F" }{ "_id" : ObjectId("56067ec6e121af6dd6618634"), "Name" : "longls", "Age" : 24, "Gender" : "F" }
查看复制的信息:
testrs0:SECONDARY> db.printReplicationInfo()configured oplog size: 990MBlog length start to end: 1708secs (0.47hrs)oplog first event time: Sat Sep 26 2015 18:48:58 GMT+0800 (CST)oplog last event time: Sat Sep 26 2015 19:17:26 GMT+0800 (CST)now: Sat Sep 26 2015 19:22:38 GMT+0800 (CST)
9、定义www.example.com节点一上线就为主节点
自定义配置文件并更改www.example.com节点的的权重:
testrs0:PRIMARY> mycfg=rs.conf(){"_id" : "testrs0","version" : 3,"members" : [{"_id" : 0,"host" : "www.example.com:27017"},{"_id" : 1,"host" : "192.168.1.9:27017"},{"_id" : 2,"host" : "192.168.1.10:27017"}]}testrs0:PRIMARY> mycfg.members[0].priority=22testrs0:PRIMARY> rs.reconfig(mycfg)Sat Sep 26 19:31:20.095 DBClientCursor::init call() failedSat Sep 26 19:31:20.096 trying reconnect to 127.0.0.1:27017Sat Sep 26 19:31:20.114 reconnect 127.0.0.1:27017 okreconnected to server after rs command (which is normal)
查看是否生效:
testrs0:SECONDARY> rs.config(){"_id" : "testrs0","version" : 4,"members" : [{"_id" : 0,"host" : "www.example.com:27017","priority" : 2},{"_id" : 1,"host" : "192.168.1.9:27017"},{"_id" : 2,"host" : "192.168.1.10:27017"}]}
验证是否生效:
[root@www.example.com ~]# service mongod stopStopping mongod: [确定][root@www.example.com ~]# service mongod startStarting mongod: about to fork child process, waiting until server is ready for connections.forked process: 6214all output going to: /var/log/mongo/mongod.logchild process started successfully, parent exiting [确定][root@www.example.com ~]# mongoMongoDB shell version: 2.4.14connecting to: testtestrs0:SECONDARY> db.isMaster(){ "setName" : "testrs0", "ismaster" : false, "secondary" : true, "hosts" : [ "www.example.com:27017", "192.168.1.10:27017", "192.168.1.9:27017" ], "me" : "www.example.com:27017", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "localTime" : ISODate("2015-09-26T11:35:32.082Z"), "ok" : 1}testrs0:PRIMARY> db.isMaster(){"setName" : "testrs0","ismaster" : true,"secondary" : false,"hosts" : ["www.example.com:27017","192.168.1.10:27017","192.168.1.9:27017"],"primary" : "www.example.com:27017","me" : "www.example.com:27017","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-09-26T11:36:37.230Z"),"ok" : 1}
通过上面可以发现一旦主节点故障下线再次上线之后并不能直接就是主节点,还需要等一段时间即需要出发选举机制才能再次成为主节点。