Forked from davisford/Setup MongoDB on localhost as Replica Set
          
        
    
          Created
          October 22, 2020 19:42 
        
      - 
      
- 
        Save jaffarc/6c815f2d28d29581cc6cd07e9be2c0b5 to your computer and use it in GitHub Desktop. 
Revisions
- 
        davisford renamed this gist Dec 21, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        davisford revised this gist Mar 2, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ replication: Restart mongod (I use brew): ```sh $ brew services restart mongodb ``` Connect with local mongo shell and initiate the replica set: 
- 
        davisford revised this gist Jan 6, 2017 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,6 +15,12 @@ replication: replSetName: replocal ``` Restart mongod (I use brew): ```sh $ brew services mongodb restart ``` Connect with local mongo shell and initiate the replica set: ``` 
- 
        davisford renamed this gist Jan 6, 2017 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,15 +15,19 @@ replication: replSetName: replocal ``` Connect with local mongo shell and initiate the replica set: ``` $mongo MongoDB shell version: 3.2.9 connecting to: test > rs.initiate({_id: "replocal", members: [{_id: 0, host: "127.0.0.1:27017"}] }) { "ok" : 1 } ``` Now you'll be secondary, but then it will promote you to primary since you're the only one: ``` replocal:SECONDARY> rs.status function () { return db._adminCommand("replSetGetStatus"); @@ -67,4 +71,6 @@ You can tail the oplog at replocal:PRIMARY> use local switched to db local replocal:PRIMARY> db.getCollection('oplog.rs').find() ``` ...lots of output here 
- 
        davisford created this gist Jan 6, 2017 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ Add the `replication` section to the mongod.conf file: ``` $cat /usr/local/etc/mongod.conf systemLog: destination: file path: /usr/local/var/log/mongodb/mongo.log logAppend: true storage: engine: mmapv1 dbPath: /usr/local/var/repl-emagine-data net: bindIp: 127.0.0.1 replication: replSetName: replocal ``` Connect with local mongo shell: ``` $mongo MongoDB shell version: 3.2.9 connecting to: test > rs.initiate({_id: "replocal", members: [{_id: 0, host: "127.0.0.1:27017"}] }) { "ok" : 1 } replocal:SECONDARY> rs.status function () { return db._adminCommand("replSetGetStatus"); } replocal:PRIMARY> rs.status() { "set" : "replocal", "date" : ISODate("2017-01-06T16:16:27.323Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2022, "optime" : { "ts" : Timestamp(1483719372, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-01-06T16:16:12Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1483719371, 2), "electionDate" : ISODate("2017-01-06T16:16:11Z"), "configVersion" : 1, "self" : true } ], "ok" : 1 } ``` You can tail the oplog at ``` replocal:PRIMARY> use local switched to db local replocal:PRIMARY> db.getCollection('oplog.rs').find() ```