Skip to content

Instantly share code, notes, and snippets.

@davisford
Last active March 6, 2024 21:40
Show Gist options
  • Star 86 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save davisford/bb37079900888c44d2bbcb2c52a5d6e8 to your computer and use it in GitHub Desktop.
Save davisford/bb37079900888c44d2bbcb2c52a5d6e8 to your computer and use it in GitHub Desktop.
Setup MongoDB replica set on local host with only a single primary
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
```
Restart mongod (I use brew):
```sh
$ brew services restart mongodb
```
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");
}
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()
```
...lots of output here
@davisford
Copy link
Author

Thanks @smilledge -- updated...

@davisford
Copy link
Author

FYI - After I upgraded to Mongo 4.0.1 something became corrupt in my local.*1 collections.

ls -alh /usr/local/var/repl-emagine-data/local*
-rw-------  1 davisford  admin    64M Sep 27 09:40 /usr/local/var/repl-emagine-data/local.0
-rw-------  1 davisford  admin   256M Sep 27 09:40 /usr/local/var/repl-emagine-data/local.1
-rw-------  1 davisford  admin    16M Sep 27 09:40 /usr/local/var/repl-emagine-data/local.ns

Here's the problem I was hitting when I tried to restart local mongo:

2018-09-27T09:28:45.487-0500 I REPL     [rsSync-0] conducting a dry run election to see if we could be elected. current term: 52
2018-09-27T09:28:45.487-0500 I REPL     [replexec-0] dry election run succeeded, running for election in term 53
2018-09-27T09:28:45.590-0500 I REPL     [replexec-0] election succeeded, assuming primary role in term 53
2018-09-27T09:28:45.590-0500 I REPL     [replexec-0] transition to PRIMARY from SECONDARY
2018-09-27T09:28:45.590-0500 I REPL     [replexec-0] Entering primary catch-up mode.
2018-09-27T09:28:45.590-0500 I REPL     [replexec-0] Exited primary catch-up mode.
2018-09-27T09:28:45.590-0500 I REPL     [replexec-0] Stopping replication producer
2018-09-27T09:28:47.531-0500 E -        [rsSync-0] Assertion: Location10320: BSONElement: bad type 111 src/mongo/bson/bsonelement.cpp 595
2018-09-27T09:28:47.531-0500 E -        [rsSync-0] Assertion: BSONObjectTooLarge: BSONObj size: 1847616361 (0x6E206369) is invalid. Size must be between 0 and 16793600(16MB) src/mongo/bson/bsonobj.cpp 101
2018-09-27T09:28:47.537-0500 F -        [rsSync-0] terminate() called. An exception is active; attempting to gather more information
2018-09-27T09:28:47.547-0500 F -        [rsSync-0] DBException::toString(): BSONObjectTooLarge: BSONObj size: 1847616361 (0x6E206369) is invalid. Size must be between 0 and 16793600(16MB)
Actual exception type: mongo::error_details::ExceptionForImpl<(mongo::ErrorCodes::Error)10334, mongo::AssertionException>
 0x103d691f9 0x103d68547 0x7fff783367c9 0x7fff78336843 0x10282c83b 0x10282848f 0x1036a88b3 0x1036af503 0x10361954a 0x103618b95 0x1036187e1 0x10361ac54 0x7fff7a68c661 0x7fff7a68c50d 0x7fff7a68bbf9
----- BEGIN BACKTRACE -----

It seems there was a corrupt bson object whose size was too large. Executing mongod --repair was not able to resolve the issue.

The only way I could work around this was to brute force remove the local* files, and then re-initiate the replica set again.

mv /usr/local/var/repl-emagine-data/local* ~/Desktop/temp/

After I removed them, brew services restart mongodb worked again, but there was no primary. So again I had to issue the command:

> rs.initiate({_id: "replocal", members: [{_id: 0, host: "127.0.0.1:27017"}] })
{ "ok" : 1 }

And then all was right in the world again.

@ntloi95
Copy link

ntloi95 commented Aug 27, 2020

That's awesome! Thank you

@carlosmondra
Copy link

Yes, this also works for mongo 4.4. Thanks!

@s4l4r
Copy link

s4l4r commented Aug 13, 2021

Thanks a lot. Very useful

@Mosallamy
Copy link

Thank you! It took me a long time figuring it out

@thefungiz
Copy link

Configuration file is at /opt/homebrew/etc/mongod.conf on Apple M1 processors
https://docs.mongodb.com/manual/reference/configuration-options/

@BioFrancescaH
Copy link

Works for mongodb-community 6.0! Thanks for the insturctions, saved me a huge headache

@ziaulhoque24
Copy link

how can i setup for windows os

@sadiqumar18
Copy link

this works pretty well for mogodb-community@7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment