一 . 读写分离
1. 登录主库:
./mongo 192.168.56.88:27017
插入一条数据: testrs:PRIMARY> db.person.insert({"name":"zw","sex":"M","age":19})
testrs:PRIMARY> db.person.find() --主库查询,ok数据出来了
{ "_id" : ObjectId("554c2f77478a8bbe95a474d9"), "name" : "zw", "sex" : "M", "age" : 19 }
{ "_id" : ObjectId("554c2f77478a8bbe95a474d9"), "name" : "zw", "sex" : "M", "age" : 19 }
2、登录进入备库
./mongo 192.168.56.88:27017
查询person下的数据: db.person.find()
testrs:SECONDARY> db.person.find()
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
报错了:说明从库不能执行查询操作
3. 让从库可以执行查询操作:
testrs:SECONDARY> db.getMongo().setSlaveOk()
testrs:SECONDARY> db.person.find() --备库已经可以查询啦
{ "_id" : ObjectId("554c2f77478a8bbe95a474d9"), "name" : "zw", "sex" : "M", "age" : 19 }
{ "_id" : ObjectId("554c2f77478a8bbe95a474d9"), "name" : "zw", "sex" : "M", "age" : 19 }
注意备库只能查询,不能插入数据
testrs:SECONDARY> db.person.insert({"name":"zw1","sex":"M","age":22})
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } }) --报错了额
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } }) --报错了额
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二.故障转移
Replica Sets 比传统的Master-Slave 有改进的地方就是它可以进行自动的故障转移,如果我们停掉Replica Sets 中的一个成员,
那么剩余的成员会再自动选举一个成员,作为主库。
例如:杀掉主库进程
[root@node1 ~]# ps -ef|grep mongo
root 10905 1 1 11:17 ? 00:00:50 ./mongod -f master.conf
root 10960 2563 0 11:20 pts/2 00:00:08 ./mongo 192.168.56.87:27017
root 11596 11570 0 12:02 pts/3 00:00:00 grep mongo
root 10905 1 1 11:17 ? 00:00:50 ./mongod -f master.conf
root 10960 2563 0 11:20 pts/2 00:00:08 ./mongo 192.168.56.87:27017
root 11596 11570 0 12:02 pts/3 00:00:00 grep mongo
[root@node1 ~] kill -9
10905
[root@node1 bin]# ./mongo 192.168.56.87:27017
--这时候主节点已经连不上了。
MongoDB shell version: 3.0.2
connecting to: 192.168.56.87:27017/test
2015-05-08T12:09:51.202+0800 W NETWORK Failed to connect to 192.168.56.87:27017, reason: errno:111 Connection refused
2015-05-08T12:09:51.203+0800 E QUERY Error: couldn't connect to server 192.168.56.87:27017 (192.168.56.87), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
MongoDB shell version: 3.0.2
connecting to: 192.168.56.87:27017/test
2015-05-08T12:09:51.202+0800 W NETWORK Failed to connect to 192.168.56.87:27017, reason: errno:111 Connection refused
2015-05-08T12:09:51.203+0800 E QUERY Error: couldn't connect to server 192.168.56.87:27017 (192.168.56.87), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
[root@node1 bin]# ./mongo 192.168.56.88:27017
--备用节点已经连上
MongoDB shell version: 3.0.2
connecting to: 192.168.56.88:27017/test
Server has startup warnings:
2015-05-08T11:18:09.719+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-05-08T11:18:09.719+0800 I CONTROL [initandlisten]
MongoDB shell version: 3.0.2
connecting to: 192.168.56.88:27017/test
Server has startup warnings:
2015-05-08T11:18:09.719+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-05-08T11:18:09.719+0800 I CONTROL [initandlisten]
testrs:PRIMARY> rs.status() --查看备用节点的健康状况
{
"set" : "testrs",
"date" : ISODate("2015-05-08T04:11:22.699Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.56.87:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)", --可以看到主节点已经显示为不健康的
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-08T04:11:21.636Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T04:08:13.900Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.56.87:27017; couldn't connect to server 192.168.56.87:27017 (192.168.56.87), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.56.88:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", -- 备用库已经变成了主库
"uptime" : 3193,
"optime" : Timestamp(1431057178, 1),
"optimeDate" : ISODate("2015-05-08T03:52:58Z"),
"electionTime" : Timestamp(1431058095, 1),
"electionDate" : ISODate("2015-05-08T04:08:15Z"),
"configVersion" : 1,
"self" : true
},
{
{
"set" : "testrs",
"date" : ISODate("2015-05-08T04:11:22.699Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.56.87:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)", --可以看到主节点已经显示为不健康的
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-08T04:11:21.636Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T04:08:13.900Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.56.87:27017; couldn't connect to server 192.168.56.87:27017 (192.168.56.87), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.56.88:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", -- 备用库已经变成了主库
"uptime" : 3193,
"optime" : Timestamp(1431057178, 1),
"optimeDate" : ISODate("2015-05-08T03:52:58Z"),
"electionTime" : Timestamp(1431058095, 1),
"electionDate" : ISODate("2015-05-08T04:08:15Z"),
"configVersion" : 1,
"self" : true
},
{