Guest User

Untitled

a guest
Feb 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. # Install MongoDB in MacOS, and Setup replica set on localhost with only a single primary.
  2.  
  3. Steps:
  4.  
  5. #### Install via Home Brew:
  6.  
  7. ```sh
  8. # make sure brew is up to day
  9. $ brew update
  10.  
  11. # install mongo
  12. $ brew install mongo
  13. ```
  14.  
  15. #### Create the data directory.
  16.  
  17. Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the `/data/db` directory (commands may need `sudo`).
  18.  
  19. ```sh
  20. # db directory
  21. $ mkdir -p /data/mongodb/db
  22.  
  23. # db log directory (custom, only if SystemLog is modified with it)
  24. $ mkdir -p /data/mongodb/log
  25. ```
  26.  
  27. ensure that the user account running mongod has read and write permissions for the directories.
  28.  
  29. #### Edit config file
  30. Add the `replication` section to the mongod.conf file, i.e.:
  31.  
  32. ```sh
  33. # edit the configuration file
  34. $ vim /usr/local/etc/mongod.conf
  35. ```
  36.  
  37. ```
  38. #! mongod.conf
  39.  
  40. # where to write logging data.
  41. systemLog:
  42. destination: file
  43. path: /data/mongodb/log/mongo.log
  44. logAppend: true
  45. component:
  46. replication:
  47. verbosity: true
  48.  
  49. # Where and how to store data.
  50. storage:
  51. dbPath: /data/mongodb/db
  52.  
  53. # network interfaces
  54. net:
  55. port: 27017
  56. bindIp: 127.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
  57.  
  58. #
  59. replication:
  60. oplogSizeMB: 50
  61. replSetName: rs0
  62. ```
  63.  
  64. #### Restart mongo
  65. Restart mongod using brew in this case:
  66.  
  67. ```sh
  68. $ brew services restart mongodb
  69. ```
  70.  
  71. #### Activate replica
  72. Connect with local mongo shell and initiate the replica set:
  73.  
  74. ```sh
  75. $ mongo
  76.  
  77. MongoDB shell version: 3.2.9
  78. connecting to: test
  79. > rs.initiate({_id: "rs0", members: [{_id: 0, host: "127.0.0.1:27017"}] })
  80. { "ok" : 1 }
  81. ```
  82.  
  83. Now you'll be secondary, but then it will promote you to primary since you're the only one:
  84.  
  85.  
  86. ```
  87. rs0:SECONDARY> rs.status
  88. function () {
  89. return db._adminCommand("replSetGetStatus");
  90. }
  91.  
  92. rs0:PRIMARY> rs.status()
  93. {
  94. "set" : "rs0",
  95. "date" : ISODate("2017-01-06T16:16:27.323Z"),
  96. "myState" : 1,
  97. "term" : NumberLong(1),
  98. "heartbeatIntervalMillis" : NumberLong(2000),
  99. "members" : [
  100. {
  101. "_id" : 0,
  102. "name" : "127.0.0.1:27017",
  103. "health" : 1,
  104. "state" : 1,
  105. "stateStr" : "PRIMARY",
  106. "uptime" : 2022,
  107. "optime" : {
  108. "ts" : Timestamp(1483719372, 1),
  109. "t" : NumberLong(1)
  110. },
  111. "optimeDate" : ISODate("2017-01-06T16:16:12Z"),
  112. "infoMessage" : "could not find member to sync from",
  113. "electionTime" : Timestamp(1483719371, 2),
  114. "electionDate" : ISODate("2017-01-06T16:16:11Z"),
  115. "configVersion" : 1,
  116. "self" : true
  117. }
  118. ],
  119. "ok" : 1
  120. }
  121.  
  122. ```
  123.  
  124. You can tail the oplog at
  125.  
  126. ```
  127. rs0:PRIMARY> use local
  128. switched to db local
  129. rs0:PRIMARY> db.getCollection('oplog.rs').find()
  130. ```
  131.  
  132. ...output here
Add Comment
Please, Sign In to add comment