欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongo快速搭建复制集

程序员文章站 2022-05-09 11:38:37
...
第一步:
在D:\MongoDB目录建立3个目录:conf(保存配置文件夹)、data(保存数据文件夹)、log(保存日志文件夹)
第二步:
在data文件夹下分别建rs1,rs2,rs3(分别保存primary,secondary,arbiter服务器的数据)
第三步:
在conf文件夹下建立rs1.conf,rs2.conf,rs3.conf配置文件
rs1的内容:
  1. dbpath=D:\MongoDB\data\rs1
  2. logpath=D:\MongoDB\log\rs1.log
  3. journal=true
  4. port=40000
  5. replSet=rs0
rs2的内容:
  1. dbpath=D:\MongoDB\data\rs2
  2. logpath=D:\MongoDB\log\rs2.log
  3. journal=true
  4. port=40001
  5. replSet=rs0
rs3的内容:
  1. dbpath=D:\MongoDB\data\rs3
  2. logpath=D:\MongoDB\log\rs3.log
  3. journal=true
  4. port=40002
  5. replSet=rs0
第四步:
用conf的方式启动mongod服务
下面的3条命令分别在3个命令行中执行
  1. mongod --config D:\MongoDB\conf\rs1.conf
  2. mongod --config D:\MongoDB\conf\rs2.conf
  3. mongod --config D:\MongoDB\conf\rs3.conf
第五步:
登录3个服务器
下面3条命令分别在3个命令行中执行
  1. mongo --port 40000
  2. mongo --port 40001
  3. mongo --port 40002
第六步:
在40000端口对应的客户端上依次执行如下命令
  1. rs.initiate()
  2. rs.conf()
  3. rs.add("DESKTOP-MNG19L3:40001")
  4. rs.addArb("DESKTOP-MNG19L3:40002")
  5. rs.status()
运行的结果如下:
  1. > rs.initiate()
  2. {
  3. "info2":"no configuration specified. Using a default configuration for the set",
  4. "me":"DESKTOP-MNG19L3:40000",
  5. "ok":1
  6. }
  7. rs0:SECONDARY> rs.conf()
  8. {
  9. "_id":"rs0",
  10. "version":1,
  11. "protocolVersion":NumberLong(1),
  12. "members":[
  13. {
  14. "_id":0,
  15. "host":"DESKTOP-MNG19L3:40000",
  16. "arbiterOnly":false,
  17. "buildIndexes":true,
  18. "hidden":false,
  19. "priority":1,
  20. "tags":{
  21. },
  22. "slaveDelay":NumberLong(0),
  23. "votes":1
  24. }
  25. ],
  26. "settings":{
  27. "chainingAllowed":true,
  28. "heartbeatIntervalMillis":2000,
  29. "heartbeatTimeoutSecs":10,
  30. "electionTimeoutMillis":10000,
  31. "getLastErrorModes":{
  32. },
  33. "getLastErrorDefaults":{
  34. "w":1,
  35. "wtimeout":0
  36. },
  37. "replicaSetId":ObjectId("593ff0ff57e4befb6527fdac")
  38. }
  39. }
  40. rs0:PRIMARY> rs.add("DESKTOP-MNG19L3:40001")
  41. {"ok":1}
  42. rs0:PRIMARY> rs.addArb("DESKTOP-MNG19L3:40002")
  43. {"ok":1}
  44. rs0:PRIMARY> rs.status()
  45. {
  46. "set":"rs0",
  47. "date":ISODate("2017-06-13T14:05:34.257Z"),
  48. "myState":1,
  49. "term":NumberLong(1),
  50. "heartbeatIntervalMillis":NumberLong(2000),
  51. "members":[
  52. {
  53. "_id":0,
  54. "name":"DESKTOP-MNG19L3:40000",
  55. "health":1,
  56. "state":1,
  57. "stateStr":"PRIMARY",
  58. "uptime":484,
  59. "optime":{
  60. "ts":Timestamp(1497362727,1),
  61. "t":NumberLong(1)
  62. },
  63. "optimeDate":ISODate("2017-06-13T14:05:27Z"),
  64. "infoMessage":"could not find member to sync from",
  65. "electionTime":Timestamp(1497362687,2),
  66. "electionDate":ISODate("2017-06-13T14:04:47Z"),
  67. "configVersion":3,
  68. "self":true
  69. },
  70. {
  71. "_id":1,
  72. "name":"DESKTOP-MNG19L3:40001",
  73. "health":1,
  74. "state":2,
  75. "stateStr":"SECONDARY",
  76. "uptime":15,
  77. "optime":{
  78. "ts":Timestamp(1497362727,1),
  79. "t":NumberLong(1)
  80. },
  81. "optimeDate":ISODate("2017-06-13T14:05:27Z"),
  82. "lastHeartbeat":ISODate("2017-06-13T14:05:33.513Z"),
  83. "lastHeartbeatRecv":ISODate("2017-06-13T14:05:32.541Z"),
  84. "pingMs":NumberLong(1),
  85. "syncingTo":"DESKTOP-MNG19L3:40000",
  86. "configVersion":3
  87. },
  88. {
  89. "_id":2,
  90. "name":"DESKTOP-MNG19L3:40002",
  91. "health":1,
  92. "state":0,
  93. "stateStr":"STARTUP",
  94. "uptime":3,
  95. "lastHeartbeat":ISODate("2017-06-13T14:05:32.526Z"),
  96. "lastHeartbeatRecv":ISODate("2017-06-13T14:05:33.649Z"),
  97. "pingMs":NumberLong(1),
  98. "configVersion":-2
  99. }
  100. ],
  101. "ok":1
  102. }
  103. rs0:PRIMARY>