Mongodb副本集

创建副本集需要三台机器,已经提前安装好mongodb了,本次使用的版本是mongodb3.2.10

  • 环境

节点1

  1. [root@mongodb-node1 ~]# cat /etc/redhat-release
  2. CentOS release 6.3 (Final)
  3. [root@mongodb-node1 ~]# uname -r
  4. 2.6.32-279.el6.x86_64
  5. [root@mongodb-node1 ~]# hostname -I
  6. 192.168.56.4

节点2

  1. [root@mongodb-node2 ~]# cat /etc/redhat-release
  2. CentOS release 6.3 (Final)
  3. [root@mongodb-node2 ~]# uname -r
  4. 2.6.32-279.el6.x86_64
  5. [root@mongodb-node2 ~]# hostname -I
  6. 192.168.56.5

节点3

  1. [root@mongodb-node3 ~]# cat /etc/redhat-release
  2. CentOS release 6.3 (Final)
  3. [root@mongodb-node3 ~]# uname -r
  4. 2.6.32-279.el6.x86_64
  5. [root@mongodb-node3 ~]# hostname -I
  6. 192.168.56.6
  • 创建key
    处于安全考虑,开启认证(auth)功能,所以需要在副本集同步的时候需要指定key,权限是600。
  1. [root@mongodb-node1 ~]# openssl rand -base64 753 >/data/mongo/conf/key
  2. [root@mongodb-node1 ~]# chmod 600 /data/mongo/conf/key

分配key文件,在分配过程中权限不会变的。

  1. [root@mongodb-node1 ~]# scp -P 22 key root@192.168.56.5:/data/mongo/conf/
  2. [root@mongodb-node1 ~]# scp -P 22 key root@192.168.56.6:/data/mongo/conf/
  • 配置文件
    在副本集创建完成,前不要把key认证开启,auth认证也不要开启。

节点1配置文件

  1. [root@mongodb-node1 ~]# cat /data/mongo/conf/mongodb.conf
  2. dbpath=/data/mongo/data
  3. port=20000
  4. bind_ip=192.168.56.4
  5. logpath=/data/mongo/logs/mongodb.log
  6. logappend=true
  7. fork=true
  8. maxConns=5000
  9. noauth=true
  10. httpinterface=true
  11. rest=false
  12. oplogSize=10
  13. replSet = repl
  14. #keyFile = /data/mongo/conf/key

剩下的基本上都是一样的就是端口和IP不一样。

  • 启动登录mongod
  1. [root@mongodb-node1 ~]# /usr/local/mongodb-3.2.10/bin/mongod -f /data/mongo/conf/mongodb.conf
  2. about to fork child process, waiting until server is ready for connections.
  3. forked process: 13539
  4. child process started successfully, parent exiting
  • 配置mongodb副本集
  1. [root@mongodb-node1 ~]# /usr/local/mongodb-3.2.10/bin/mongo 192.168.56.4:20000
  2. MongoDB shell version: 3.2.10
  3. connecting to: 192.168.56.4:20000/test
  4. Server has startup warnings:
  5. 2016-10-25T15:31:18.193+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
  6. 2016-10-25T15:31:18.193+0800 I CONTROL [initandlisten]
  7. > repl_cfg = { _id:"repl", members:[
  8. ... {_id:0,host:"192.168.56.4:20000",priority:2},
  9. ... {_id:1,host:"192.168.56.5:20001",priority:1},
  10. ... {_id:2,host:"192.168.56.6:20002",priority:0}]
  11. ... }
  12. {
  13. "_id" : "repl",
  14. "members" : [
  15. {
  16. "_id" : 0,
  17. "host" : "192.168.56.4:20000",
  18. "priority" : 2
  19. },
  20. {
  21. "_id" : 1,
  22. "host" : "192.168.56.5:20001",
  23. "priority" : 1
  24. },
  25. {
  26. "_id" : 2,
  27. "host" : "192.168.56.6:20002",
  28. "priority" : 0
  29. }
  30. ]
  31. }
  32. > rs.initiate(repl_cfg)
  33. { "ok" : 1 }
  34. repl:OTHER>
  35. repl:PRIMARY> rs.status()
  36. {
  37. "set" : "repl",
  38. "date" : ISODate("2016-10-25T07:33:24.932Z"),
  39. "myState" : 1,
  40. "term" : NumberLong(1),
  41. "heartbeatIntervalMillis" : NumberLong(2000),
  42. "members" : [
  43. {
  44. "_id" : 0,
  45. "name" : "192.168.56.4:20000",
  46. "health" : 1,
  47. "state" : 1,
  48. "stateStr" : "PRIMARY",
  49. "uptime" : 127,
  50. "optime" : {
  51. "ts" : Timestamp(1477380795, 1),
  52. "t" : NumberLong(1)
  53. },
  54. "optimeDate" : ISODate("2016-10-25T07:33:15Z"),
  55. "infoMessage" : "could not find member to sync from",
  56. "electionTime" : Timestamp(1477380794, 1),
  57. "electionDate" : ISODate("2016-10-25T07:33:14Z"),
  58. "configVersion" : 1,
  59. "self" : true
  60. },
  61. {
  62. "_id" : 1,
  63. "name" : "192.168.56.5:20001",
  64. "health" : 1,
  65. "state" : 2,
  66. "stateStr" : "SECONDARY",
  67. "uptime" : 21,
  68. "optime" : {
  69. "ts" : Timestamp(1477380795, 1),
  70. "t" : NumberLong(1)
  71. },
  72. "optimeDate" : ISODate("2016-10-25T07:33:15Z"),
  73. "lastHeartbeat" : ISODate("2016-10-25T07:33:24.700Z"),
  74. "lastHeartbeatRecv" : ISODate("2016-10-25T07:33:23.275Z"),
  75. "pingMs" : NumberLong(0),
  76. "syncingTo" : "192.168.56.4:20000",
  77. "configVersion" : 1
  78. },
  79. {
  80. "_id" : 2,
  81. "name" : "192.168.56.6:20002",
  82. "health" : 1,
  83. "state" : 2,
  84. "stateStr" : "SECONDARY",
  85. "uptime" : 21,
  86. "optime" : {
  87. "ts" : Timestamp(1477380795, 1),
  88. "t" : NumberLong(1)
  89. },
  90. "optimeDate" : ISODate("2016-10-25T07:33:15Z"),
  91. "lastHeartbeat" : ISODate("2016-10-25T07:33:24.700Z"),
  92. "lastHeartbeatRecv" : ISODate("2016-10-25T07:33:23.269Z"),
  93. "pingMs" : NumberLong(0),
  94. "syncingTo" : "192.168.56.4:20000",
  95. "configVersion" : 1
  96. }
  97. ],
  98. "ok" : 1
  99. }

配置完成,另外两个副本集会自动加入。priority意思是优先值的问题,数值越大,优先值越高。
优先值高的写入数据

  1. repl:PRIMARY> show dbs;
  2. local 0.000GB
  3. repl:PRIMARY> use test
  4. switched to db test
  5. repl:PRIMARY> db.test.insert({'a':'1'})
  6. WriteResult({ "nInserted" : 1 })
  7. repl:PRIMARY> db.test.find()
  8. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }

去另一个副本查看。

  1. [root@mongodb-node2 ~]# /usr/local/mongodb-3.2.10/bin/mongo 192.168.56.5:20001
  2. MongoDB shell version: 3.2.10
  3. connecting to: 192.168.56.5:20001/test
  4. Server has startup warnings:
  5. 2016-10-23T21:08:16.565+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
  6. 2016-10-23T21:08:16.565+0800 I CONTROL [initandlisten]
  7. >
  8. repl:SECONDARY> use test
  9. switched to db test
  10. repl:SECONDARY> db.test.find()
  11. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }

如果报错not master and slaveOk=false,如下是解决方法

  1. repl:SECONDARY> db.test.find()
  2. Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
  3. repl:SECONDARY> rs.slaveOk()

只有优先值最高的副本才能写入数据。

  1. repl:SECONDARY> db.test.insert({'b':'2'})
  2. WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })
  • 开启认证
    在开启认证前记得把用户给创建好。
  1. > use admin
  2. switched to db admin
  3. >db.createUser({
  4. user: "admin",
  5. pwd: "admin",
  6. roles: [ { role: "userAdminAnyDatabase",db: "admin" },{ role: "readAnyDatabase",db: "admin"},
  7. { role: "readWriteAnyDatabase",db: "admin"},{ role: "dbAdminAnyDatabase",db: "admin"}]
  8. })

先关闭mongodb,修改配置文件

  1. [root@mongodb-node1 ~]# /usr/local/mongodb-3.2.10/bin/mongod --shutdown --dbpath /data/mongo/data/
  2. killing process with pid: 13717
  3. [root@mongodb-node1 ~]# vim /data/mongo/conf/mongodb.conf
  4. [root@mongodb-node1 ~]# cat /data/mongo/conf/mongodb.conf
  5. dbpath=/data/mongo/data
  6. port=20000
  7. bind_ip=192.168.56.4
  8. logpath=/data/mongo/logs/mongodb.log
  9. logappend=true
  10. fork=true
  11. maxConns=5000
  12. auth=true
  13. httpinterface=true
  14. rest=false
  15. oplogSize=10
  16. replSet = repl
  17. keyFile = /data/mongo/conf/key
  18. [root@mongodb-node1 ~]# /usr/local/mongodb-3.2.10/bin/mongod -f /data/mongo/conf/mongodb.conf
  19. about to fork child process, waiting until server is ready for connections.
  20. forked process: 13819
  21. child process started successfully, parent exiting

三个节点都需要这么做。

登录mongodb进行查看,写如数据进行测试。

  1. [root@mongodb-node1 ~]# /usr/local/mongodb-3.2.10/bin/mongo 192.168.56.4:20000
  2. MongoDB shell version: 3.2.10
  3. connecting to: 192.168.56.4:20000/test
  4. repl:PRIMARY> show dbs;
  5. 2016-10-25T15:49:45.697+0800 E QUERY [thread1] Error: listDatabases failed:{
  6. "ok" : 0,
  7. "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
  8. "code" : 13
  9. } :
  10. _getErrorWithCode@src/mongo/shell/utils.js:25:13
  11. Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
  12. shellHelper.show@src/mongo/shell/utils.js:761:19
  13. shellHelper@src/mongo/shell/utils.js:651:15
  14. @(shellhelp2):1:1
  15. repl:PRIMARY> use admin
  16. switched to db admin
  17. repl:PRIMARY> db.auth('admin','admin')
  18. 1
  19. repl:PRIMARY> show dbs;
  20. admin 0.000GB
  21. local 0.000GB
  22. test 0.000GB
  23. repl:PRIMARY> use test
  24. switched to db test
  25. repl:PRIMARY> db.test.insert({'c':'3'})
  26. WriteResult({ "nInserted" : 1 })
  27. repl:PRIMARY> db.test.find()
  28. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }
  29. { "_id" : ObjectId("580f0d59fdcbdd0ab706f022"), "b" : "2" }
  30. { "_id" : ObjectId("580f0ebe5447a2d906013ca9"), "c" : "3" }

进入另一个副本进行查看

  1. [root@mongodb-node2 ~]# /usr/local/mongodb-3.2.10/bin/mongo 192.168.56.5:20001
  2. MongoDB shell version: 3.2.10
  3. connecting to: 192.168.56.5:20001/test
  4. repl:SECONDARY> show dbs;
  5. 2016-10-23T21:28:38.644+0800 E QUERY [thread1] Error: listDatabases failed:{
  6. "ok" : 0,
  7. "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
  8. "code" : 13
  9. } :
  10. _getErrorWithCode@src/mongo/shell/utils.js:25:13
  11. Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
  12. shellHelper.show@src/mongo/shell/utils.js:761:19
  13. shellHelper@src/mongo/shell/utils.js:651:15
  14. @(shellhelp2):1:1
  15. repl:SECONDARY> use admin
  16. switched to db admin
  17. repl:SECONDARY> db.auth('admin','admin')
  18. 1
  19. repl:SECONDARY> show dbs;
  20. 2016-10-23T21:28:56.429+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :
  21. _getErrorWithCode@src/mongo/shell/utils.js:25:13
  22. Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
  23. shellHelper.show@src/mongo/shell/utils.js:761:19
  24. shellHelper@src/mongo/shell/utils.js:651:15
  25. @(shellhelp2):1:1
  26. repl:SECONDARY> rs.slaveOk()
  27. repl:SECONDARY> show dbs;
  28. admin 0.000GB
  29. local 0.000GB
  30. test 0.000GB
  31. repl:SECONDARY> use test
  32. switched to db test
  33. repl:SECONDARY> db.test.find()
  34. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }
  35. { "_id" : ObjectId("580f0d59fdcbdd0ab706f022"), "b" : "2" }
  36. { "_id" : ObjectId("580f0ebe5447a2d906013ca9"), "c" : "3" }

要进行认证才可以查看,要执行rs.slaveOk()才会恢复正的副本复制。

  • 创建用户
    如果不认证这个用户,会查询不了副本集的状态
  1. repl:PRIMARY> rs.status()
  2. {
  3. "ok" : 0,
  4. "errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0 }",
  5. "code" : 13
  6. }
  1. repl:PRIMARY> use admin
  2. switched to db admin
  3. repl:PRIMARY> db.createUser(
  4. ... {
  5. ... user: "root",
  6. ... pwd: "root",
  7. ... roles: [ { role: "root", db: "admin" } ]
  8. ... }
  9. ... );
  10. Successfully added user: {
  11. "user" : "root",
  12. "roles" : [
  13. {
  14. "role" : "root",
  15. "db" : "admin"
  16. }
  17. ]
  18. }
  19. repl:PRIMARY> db.auth('root','root')
  20. 1
  21. repl:PRIMARY> rs.status()
  22. {
  23. "set" : "repl",
  24. "date" : ISODate("2016-10-25T08:15:38.217Z"),
  25. "myState" : 1,
  26. "term" : NumberLong(4),
  27. "heartbeatIntervalMillis" : NumberLong(2000),
  28. "members" : [
  29. {
  30. "_id" : 0,
  31. "name" : "192.168.56.4:20000",
  32. "health" : 1,
  33. "state" : 1,
  34. "stateStr" : "PRIMARY",
  35. "uptime" : 1651,
  36. "optime" : {
  37. "ts" : Timestamp(1477383316, 1),
  38. "t" : NumberLong(4)
  39. },
  40. "optimeDate" : ISODate("2016-10-25T08:15:16Z"),
  41. "electionTime" : Timestamp(1477381758, 1),
  42. "electionDate" : ISODate("2016-10-25T07:49:18Z"),
  43. "configVersion" : 1,
  44. "self" : true
  45. },
  46. {
  47. "_id" : 1,
  48. "name" : "192.168.56.5:20001",
  49. "health" : 1,
  50. "state" : 2,
  51. "stateStr" : "SECONDARY",
  52. "uptime" : 1600,
  53. "optime" : {
  54. "ts" : Timestamp(1477383316, 1),
  55. "t" : NumberLong(4)
  56. },
  57. "optimeDate" : ISODate("2016-10-25T08:15:16Z"),
  58. "lastHeartbeat" : ISODate("2016-10-25T08:15:36.637Z"),
  59. "lastHeartbeatRecv" : ISODate("2016-10-25T08:15:37.451Z"),
  60. "pingMs" : NumberLong(0),
  61. "syncingTo" : "192.168.56.6:20002",
  62. "configVersion" : 1
  63. },
  64. {
  65. "_id" : 2,
  66. "name" : "192.168.56.6:20002",
  67. "health" : 1,
  68. "state" : 2,
  69. "stateStr" : "SECONDARY",
  70. "uptime" : 1589,
  71. "optime" : {
  72. "ts" : Timestamp(1477383316, 1),
  73. "t" : NumberLong(4)
  74. },
  75. "optimeDate" : ISODate("2016-10-25T08:15:16Z"),
  76. "lastHeartbeat" : ISODate("2016-10-25T08:15:36.623Z"),
  77. "lastHeartbeatRecv" : ISODate("2016-10-25T08:15:37.586Z"),
  78. "pingMs" : NumberLong(0),
  79. "syncingTo" : "192.168.56.4:20000",
  80. "configVersion" : 1
  81. }
  82. ],
  83. "ok" : 1
  84. }
  • 移除和增加节点

删除节点后写入数据

  1. repl:PRIMARY> rs.remove('192.168.56.5:20001')
  2. { "ok" : 1 }
  3. repl:PRIMARY> rs.status()
  4. {
  5. "set" : "repl",
  6. "date" : ISODate("2016-10-25T08:19:25.481Z"),
  7. "myState" : 1,
  8. "term" : NumberLong(4),
  9. "heartbeatIntervalMillis" : NumberLong(2000),
  10. "members" : [
  11. {
  12. "_id" : 0,
  13. "name" : "192.168.56.4:20000",
  14. "health" : 1,
  15. "state" : 1,
  16. "stateStr" : "PRIMARY",
  17. "uptime" : 1878,
  18. "optime" : {
  19. "ts" : Timestamp(1477383563, 1),
  20. "t" : NumberLong(4)
  21. },
  22. "optimeDate" : ISODate("2016-10-25T08:19:23Z"),
  23. "electionTime" : Timestamp(1477381758, 1),
  24. "electionDate" : ISODate("2016-10-25T07:49:18Z"),
  25. "configVersion" : 2,
  26. "self" : true
  27. },
  28. {
  29. "_id" : 2,
  30. "name" : "192.168.56.6:20002",
  31. "health" : 1,
  32. "state" : 2,
  33. "stateStr" : "SECONDARY",
  34. "uptime" : 1816,
  35. "optime" : {
  36. "ts" : Timestamp(1477383316, 1),
  37. "t" : NumberLong(4)
  38. },
  39. "optimeDate" : ISODate("2016-10-25T08:15:16Z"),
  40. "lastHeartbeat" : ISODate("2016-10-25T08:19:23.596Z"),
  41. "lastHeartbeatRecv" : ISODate("2016-10-25T08:19:23.622Z"),
  42. "pingMs" : NumberLong(0),
  43. "syncingTo" : "192.168.56.4:20000",
  44. "configVersion" : 1
  45. }
  46. ],
  47. "ok" : 1
  48. }
  49. repl:PRIMARY> use test
  50. switched to db test
  51. repl:PRIMARY> db.test.insert({'d':'4'})
  52. WriteResult({ "nInserted" : 1 })
  53. repl:PRIMARY> db.test.find()
  54. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }
  55. { "_id" : ObjectId("580f0d59fdcbdd0ab706f022"), "b" : "2" }
  56. { "_id" : ObjectId("580f0ebe5447a2d906013ca9"), "c" : "3" }
  57. { "_id" : ObjectId("580f12185447a2d906013caa"), "c" : "3" }
  58. { "_id" : ObjectId("580f16085447a2d906013cab"), "d" : "4" }

查看那个节点状态

  1. repl:SECONDARY>
  2. > show dbs;
  3. 2016-10-23T21:57:10.004+0800 I NETWORK [thread1] trying reconnect to 192.168.56.5:20001 (192.168.56.5) failed
  4. 2016-10-23T21:57:10.023+0800 I NETWORK [thread1] reconnect 192.168.56.5:20001 (192.168.56.5) ok
  5. admin 0.000GB
  6. local 0.000GB
  7. test 0.000GB
  8. > use test
  9. switched to db test
  10. > db.test.find()
  11. Error: error: { "ok" : 0, "errmsg" : "node is recovering", "code" : 13436 }

增加节点

  1. repl:PRIMARY> rs.add('192.168.56.5:20001')
  2. { "ok" : 1 }
  3. repl:PRIMARY> rs.status()
  4. {
  5. "set" : "repl",
  6. "date" : ISODate("2016-10-25T08:23:31.316Z"),
  7. "myState" : 1,
  8. "term" : NumberLong(4),
  9. "heartbeatIntervalMillis" : NumberLong(2000),
  10. "members" : [
  11. {
  12. "_id" : 0,
  13. "name" : "192.168.56.4:20000",
  14. "health" : 1,
  15. "state" : 1,
  16. "stateStr" : "PRIMARY",
  17. "uptime" : 2124,
  18. "optime" : {
  19. "ts" : Timestamp(1477383807, 1),
  20. "t" : NumberLong(4)
  21. },
  22. "optimeDate" : ISODate("2016-10-25T08:23:27Z"),
  23. "electionTime" : Timestamp(1477381758, 1),
  24. "electionDate" : ISODate("2016-10-25T07:49:18Z"),
  25. "configVersion" : 3,
  26. "self" : true
  27. },
  28. {
  29. "_id" : 2,
  30. "name" : "192.168.56.6:20002",
  31. "health" : 1,
  32. "state" : 2,
  33. "stateStr" : "SECONDARY",
  34. "uptime" : 2062,
  35. "optime" : {
  36. "ts" : Timestamp(1477383807, 1),
  37. "t" : NumberLong(4)
  38. },
  39. "optimeDate" : ISODate("2016-10-25T08:23:27Z"),
  40. "lastHeartbeat" : ISODate("2016-10-25T08:23:31.254Z"),
  41. "lastHeartbeatRecv" : ISODate("2016-10-25T08:23:31.313Z"),
  42. "pingMs" : NumberLong(0),
  43. "syncingTo" : "192.168.56.4:20000",
  44. "configVersion" : 3
  45. },
  46. {
  47. "_id" : 3,
  48. "name" : "192.168.56.5:20001",
  49. "health" : 1,
  50. "state" : 2,
  51. "stateStr" : "SECONDARY",
  52. "uptime" : 2,
  53. "optime" : {
  54. "ts" : Timestamp(1477383807, 1),
  55. "t" : NumberLong(4)
  56. },
  57. "optimeDate" : ISODate("2016-10-25T08:23:27Z"),
  58. "lastHeartbeat" : ISODate("2016-10-25T08:23:31.304Z"),
  59. "lastHeartbeatRecv" : ISODate("2016-10-25T08:23:29.256Z"),
  60. "pingMs" : NumberLong(0),
  61. "configVersion" : 3
  62. }
  63. ],
  64. "ok" : 1
  65. }

查看新增加的节点数据

  1. repl:SECONDARY> use test
  2. switched to db test
  3. repl:SECONDARY> db.test.find()
  4. { "_id" : ObjectId("580f0b3afdcbdd0ab706f021"), "a" : "1" }
  5. { "_id" : ObjectId("580f0d59fdcbdd0ab706f022"), "b" : "2" }
  6. { "_id" : ObjectId("580f0ebe5447a2d906013ca9"), "c" : "3" }
  7. { "_id" : ObjectId("580f12185447a2d906013caa"), "c" : "3" }
  8. { "_id" : ObjectId("580f16085447a2d906013cab"), "d" : "4" }

在增加节点的时候不要执行priority,如果执行了,节点会变成仲裁节点。

  1. repl:PRIMARY> rs.add('192.168.56.5:20001','priority:1')
  2. { "ok" : 1 }
  3. repl:PRIMARY> rs.status()
  4. {
  5. "set" : "repl",
  6. "date" : ISODate("2016-10-25T08:39:58.509Z"),
  7. "myState" : 1,
  8. "term" : NumberLong(4),
  9. "heartbeatIntervalMillis" : NumberLong(2000),
  10. "members" : [
  11. {
  12. "_id" : 0,
  13. "name" : "192.168.56.4:20000",
  14. "health" : 1,
  15. "state" : 1,
  16. "stateStr" : "PRIMARY",
  17. "uptime" : 3111,
  18. "optime" : {
  19. "ts" : Timestamp(1477384788, 1),
  20. "t" : NumberLong(4)
  21. },
  22. "optimeDate" : ISODate("2016-10-25T08:39:48Z"),
  23. "electionTime" : Timestamp(1477381758, 1),
  24. "electionDate" : ISODate("2016-10-25T07:49:18Z"),
  25. "configVersion" : 15,
  26. "self" : true
  27. },
  28. {
  29. "_id" : 4,
  30. "name" : "192.168.56.6:20002",
  31. "health" : 1,
  32. "state" : 2,
  33. "stateStr" : "SECONDARY",
  34. "uptime" : 309,
  35. "optime" : {
  36. "ts" : Timestamp(1477384788, 1),
  37. "t" : NumberLong(4)
  38. },
  39. "optimeDate" : ISODate("2016-10-25T08:39:48Z"),
  40. "lastHeartbeat" : ISODate("2016-10-25T08:39:58.231Z"),
  41. "lastHeartbeatRecv" : ISODate("2016-10-25T08:39:58.292Z"),
  42. "pingMs" : NumberLong(1),
  43. "syncingTo" : "192.168.56.4:20000",
  44. "configVersion" : 15
  45. },
  46. {
  47. "_id" : 5,
  48. "name" : "192.168.56.5:20001",
  49. "health" : 1,
  50. "state" : 7,
  51. "stateStr" : "ARBITER",
  52. "uptime" : 8,
  53. "lastHeartbeat" : ISODate("2016-10-25T08:39:58.290Z"),
  54. "lastHeartbeatRecv" : ISODate("2016-10-25T08:39:58.326Z"),
  55. "pingMs" : NumberLong(0),
  56. "syncingTo" : "192.168.56.6:20002",
  57. "configVersion" : 15
  58. }
  59. ],
  60. "ok" : 1
  61. }
  1. repl:SECONDARY>
  2. > use test;
  3. switched to db test
  4. > db.test.find()
  5. 2016-10-23T22:17:37.106+0800 I NETWORK [thread1] trying reconnect to 192.168.56.5:20001 (192.168.56.5) failed
  6. 2016-10-23T22:17:37.106+0800 I NETWORK [thread1] reconnect 192.168.56.5:20001 (192.168.56.5) ok
  7. Error: error: { "ok" : 0, "errmsg" : "node is recovering", "code" : 13436 }
  8. > db.test.find()
  9. Error: error: { "ok" : 0, "errmsg" : "node is recovering", "code" : 13436 }

尽量不要使用仲裁节点,仲裁节点的作用是,如果当两个优先值一样的时候,仲裁节点进行选举,快速选择出主节点。