读写分离实现步骤:
从库能够进行查询就更好了,这样可以分担主库的大量的查询请求。
1) 先向主库中插入一条测试数据
rs1:PRIMARY> db.c1.insert({age:30});db.c1.insert({age:30});
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> db.c1.find() db.c1.find()
{ "_id" : ObjectId("5791ef011f4c6277726192b1"), "age" : 30 }
rs1:PRIMARY>
2)在从库进行查询等操作
[[email protected] bin]# ./mongo --port 28011
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> show collectionsshow collections
2016-07-22T18:02:47.038+0800 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {} }",
"code" : 13
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/db.js:773:1
[email protected]/mongo/shell/db.js:785:19
[email protected]/mongo/shell/db.js:796:16
[email protected]/mongo/shell/utils.js:754:9
[email protected]/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
rs1:SECONDARY>
当查询时报错了,说明是个从库,目前不能执行查询的操作。
3)让从库可以读,分担主库的压力
> db.getMongo().setSlaveOk()
> show collections还是不行,
> exit
查询官方手册https://docs.mongodb.org/manual/tutorial/enable-authentication/发现登录方式应该用
[[email protected] bin]# ./mongo --port 28011 -u "bbb" -p "bbb" --authenticationDatabase "admin"
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28011/test
Server has startup warnings:
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten]
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten]
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten]
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2016-07-22T17:36:41.302+0800 I CONTROL [initandlisten]
rs1:SECONDARY> show collectionsshow collections
2016-07-22T18:06:17.354+0800 E QUERY [thread1] Error: listCollections failed: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/db.js:773:1
[email protected]/mongo/shell/db.js:785:19
[email protected]/mongo/shell/db.js:796:16
[email protected]/mongo/shell/utils.js:754:9
[email protected]/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
rs1:SECONDARY> use adminuse admin
switched to db admin
rs1:SECONDARY> show dbsshow dbs
admin 0.000GB
local 0.000GB
rs1:SECONDARY> db.getMongo().setSlaveOk() db.getMongo().setSlaveOk()
rs1:SECONDARY> show collectionsshow collections
system.users
system.version
rs1:SECONDARY>
OK了。执行 db.getMongo().setSlaveOk() 可以将从库的查询功能打开。
故障转移测试:
复制集比传统的master-slave有改进的地方就是它可以进行故障的自动转移,如果我们停掉复制集中的一个成员,那么剩余的成员就会再自动选举一个成员,作为主库,如下所示。
我们将28010这个主库停掉,然后再看一下复制集的状态。
1) 杀掉28010端口的MongoDB
[[email protected] bin]# ps aux | grep mongod
root 16629 0.3 0.4 783320 80124 ? Sl 17:36 0:07 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend
root 16666 0.3 0.4 778000 78904 ? Sl 17:36 0:07 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend
root 16703 0.3 0.4 778004 75068 ? Sl 17:36 0:07 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend
root 16825 0.0 0.1 225072 22800 pts/1 Sl+ 17:39 0:00 /usr/local/mongodb/bin/mongo -port 28010
root 17539 0.0 0.0 110216 900 pts/0 R+ 18:12 0:00 grep --color=auto mongod
[[email protected] bin]# kill -9 16629
[[email protected] bin]# ps aux | grep mongod
root 16666 0.3 0.4 778000 79056 ? Sl 17:36 0:07 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend
root 16703 0.3 0.4 778004 75200 ? Sl 17:36 0:07 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend
root 16825 0.0 0.1 225072 22800 pts/1 Sl+ 17:39 0:00 /usr/local/mongodb/bin/mongo -port 28010
root 17569 0.0 0.0 110216 904 pts/0 R+ 18:13 0:00 grep --color=auto mongod
[[email protected] bin]#
2) 查看复制集状态
[[email protected] mongodb]# /usr/local/mongodb/bin/mongo -port 28011
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> rs.status();rs.status();//又是权限问题
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0 }",
"code" : 13
}
rs1:SECONDARY> use adminuse admin
switched to db admin
rs1:SECONDARY> db.auth(‘bbb‘,‘bbb‘)db.auth(‘bbb‘,‘bbb‘)
1
rs1:SECONDARY> rs.status(); rs.status();
{
"set" : "rs1",
"date" : ISODate("2016-07-22T10:15:14.194Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "localhost:28012",
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-07-22T10:15:14.054Z"),
"lastHeartbeatRecv" : ISODate("2016-07-22T10:13:10.712Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2313,
"optime" : {
"ts" : Timestamp(1469182400, 2),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-07-22T10:13:20Z"),
"syncingTo" : "localhost:28012",
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2097,
"optime" : {
"ts" : Timestamp(1469182400, 2),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-07-22T10:13:20Z"),
"lastHeartbeat" : ISODate("2016-07-22T10:15:13.010Z"),
"lastHeartbeatRecv" : ISODate("2016-07-22T10:15:12.593Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1469182400, 1),
"electionDate" : ISODate("2016-07-22T10:13:20Z"),
"configVersion" : 1
}
],
"ok" : 1
}
rs1:SECONDARY>
可以看到28010这个端口的MongoDB出现了异常,而系统自动选举了 28012 这个端口为主,所以这样的故障处理机制,能讲系统的稳定性大大提高。