Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Install MongoDB in MacOS, and Setup replica set on localhost with only a single primary.
- Steps:
- #### Install via Home Brew:
- ```sh
- # make sure brew is up to day
- $ brew update
- # install mongo
- $ brew install mongo
- ```
- #### Create the data directory.
- Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the `/data/db` directory (commands may need `sudo`).
- ```sh
- # db directory
- $ mkdir -p /data/mongodb/db
- # db log directory (custom, only if SystemLog is modified with it)
- $ mkdir -p /data/mongodb/log
- ```
- ensure that the user account running mongod has read and write permissions for the directories.
- #### Edit config file
- Add the `replication` section to the mongod.conf file, i.e.:
- ```sh
- # edit the configuration file
- $ vim /usr/local/etc/mongod.conf
- ```
- ```
- #! mongod.conf
- # where to write logging data.
- systemLog:
- destination: file
- path: /data/mongodb/log/mongo.log
- logAppend: true
- component:
- replication:
- verbosity: true
- # Where and how to store data.
- storage:
- dbPath: /data/mongodb/db
- # network interfaces
- net:
- port: 27017
- bindIp: 127.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
- #
- replication:
- oplogSizeMB: 50
- replSetName: rs0
- ```
- #### Restart mongo
- Restart mongod using brew in this case:
- ```sh
- $ brew services restart mongodb
- ```
- #### Activate replica
- Connect with local mongo shell and initiate the replica set:
- ```sh
- $ mongo
- MongoDB shell version: 3.2.9
- connecting to: test
- > rs.initiate({_id: "rs0", 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:
- ```
- rs0:SECONDARY> rs.status
- function () {
- return db._adminCommand("replSetGetStatus");
- }
- rs0:PRIMARY> rs.status()
- {
- "set" : "rs0",
- "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
- ```
- rs0:PRIMARY> use local
- switched to db local
- rs0:PRIMARY> db.getCollection('oplog.rs').find()
- ```
- ...output here
Add Comment
Please, Sign In to add comment