Last active
February 15, 2025 11:17
-
-
Save davisford/bb37079900888c44d2bbcb2c52a5d6e8 to your computer and use it in GitHub Desktop.
Setup MongoDB replica set on local host with only a single primary
This 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 characters
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 |
That's awesome! Thank you
Yes, this also works for mongo 4.4. Thanks!
Thanks a lot. Very useful
Thank you! It took me a long time figuring it out
Configuration file is at /opt/homebrew/etc/mongod.conf
on Apple M1 processors
https://docs.mongodb.com/manual/reference/configuration-options/
Works for mongodb-community 6.0! Thanks for the insturctions, saved me a huge headache
how can i setup for windows os
this works pretty well for [email protected]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI - After I upgraded to Mongo 4.0.1 something became corrupt in my
local.*1
collections.Here's the problem I was hitting when I tried to restart local mongo:
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.After I removed them,
brew services restart mongodb
worked again, but there was no primary. So again I had to issue the command:And then all was right in the world again.