Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Boncom99/daef6bcb8a9afee598e4cc3b01a65984 to your computer and use it in GitHub Desktop.
Save Boncom99/daef6bcb8a9afee598e4cc3b01a65984 to your computer and use it in GitHub Desktop.
Setup MongoDB replica set on local host with only a single primary - For Apple Silicon

Add the replication section to the /opt/homebrew/etc/mongod.conf file:

replication:
  replSetName: replocal

If we cat the result

$ cat /opt/homebrew/etc/mongod.conf

we should get:

systemLog:
  destination: file
  path: /opt/homebrew/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /opt/homebrew/var/mongodb
net:
  bindIp: 127.0.0.1, ::1
  ipv6: true
//new 👇
replication:
  replSetName: replocal

Restart mongod using brew(change @__ according to your version):

brew services restart mongodb-community

Connect with local mongo shell and initiate the replica set:

$mongo

or, for newer versions:

$mongosh

Once we are in, we run:

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

//response
{ "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

//response
function () {
    return db._adminCommand("replSetGetStatus");
}
replocal:PRIMARY> rs.status()

You can tail the oplog at

replocal:PRIMARY> use local

//response
switched to db local
replocal:PRIMARY> db.getCollection('oplog.rs').find()

...lots of output here

and DONE.

@Boncom99
Copy link
Author

This is a fork from Sanat's gist updated version of this gist from Davisford.

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