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.
This is a fork from Sanat's gist updated version of this gist from Davisford.