一、读写分离
从库能进行查询,这样可以分担主库的大量的查询请求。
1、先向主库中插入一条测试数据
[[email protected] bin]# ./mongo --port 28010 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:28010/test rs1:PRIMARY> db.c1.insert({age:30}) db.c2rs1:PRIMARY> db.c1.find() { "_id" : ObjectId("4fc77f421137ea4fdb653b4a"), "age" : 30 }
2、在从库中进行查询等操作
[[email protected] bin]# ./mongo --port 28011 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:28011/test rs1:SECONDARY> show collections Thu May 31 22:27:17 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 } rs1:SECONDARY>
查询报错,说明是个从库且不能执行查询的操作
3、让从库可以读,分担主库的压力
rs1:SECONDARY> db.getMongo().setSlaveOk() not master and slaveok=false rs1:SECONDARY> show collections c1 system.indexes rs1:SECONDARY> db.c1.find() { "_id" : ObjectId("4fc77f421137ea4fdb653b4a"), "age" : 30 } rs1:SECONDARY>
我们做到了。
二、故障转移
复制集比传统的Master-Slave 有改进的地方就是他可以进行故障的自动转移,如果我们停掉复制集中的一个成员,那么剩余成员会再自动选举出一个成员,做为主库,例如:我们将28010 这个主库停掉,然后再看一下复制集的状态
测试:
1、kill 28010端口的MongoDB
[[email protected] bin]# ps aux|grep mongod root 6706 1.6 6.9 463304 6168 Sl 21:49 0:26 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010 root 6733 0.4 6.7 430528 6044 ? Sl 21:50 0:06 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011 root 6747 0.4 4.7 431548 4260 ? Sl 21:50 0:06 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012 root 7019 0.0 0.7 5064 684 pts/2 S+ 22:16 0:00 grep mongod [[email protected] bin]# kill -9 6706
2、再查看复制集状态
[[email protected] bin]# ./mongo --port 28011 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:28011/test rs1:SECONDARY> rs.status() { "set" : "rs1", "date" : ISODate("2012-05-31T14:17:03Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 0, "state" : 1, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "t" : 1338472279000, "i" : 1 }, "optimeDate" : ISODate("2012-05-31T13:51:19Z"), "lastHeartbeat" : ISODate("2012-05-31T14:16:42Z"), "errmsg" : "socket exception" }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "optime" : { "t" : 1338472279000, "i" : 1 }, "optimeDate" : ISODate("2012-05-31T13:51:19Z"), "self" : true }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1528, "optime" : { "t" : 1338472279000, "i" : 1 }, "optimeDate" : ISODate("2012-05-31T13:51:19Z"), "lastHeartbeat" : ISODate("2012-05-31T14:17:02Z") } ], "ok" : 1 } rs1:SECONDARY> "stateStr" : "(not reachable/healthy)",
可以看到28010 这个端口的MongoDB 出现了异常,而系统自动选举了28012 这个端口为主,所以这样的故障处理机制,能将系统的稳定性大大提高。
时间: 2024-10-10 06:48:54