Skip to content

Instantly share code, notes, and snippets.

@olecksamdr
Last active February 15, 2021 13:22
Show Gist options
  • Save olecksamdr/e1bd1d0b5955fe1e56d6ccc772a67891 to your computer and use it in GitHub Desktop.
Save olecksamdr/e1bd1d0b5955fe1e56d6ccc772a67891 to your computer and use it in GitHub Desktop.
Run mongoDb Replica Set on localhost
  1. Find mongodb data folder
grep -i dbPath /etc/mongod.conf
  1. Let's say that data folder is /data/db. Create the necessary data directories for each member by issuing a command similar to the following:
mkdir -p /data/db/rs0-0 /data/db/rs0-1 /data/db/rs0-2
  1. Start your mongod instances in their own shell windows by issuing the following commands:

    3.1. First member

    mongod --replSet rs0 --port 27017 --bind_ip localhost --dbpath ~/data/rs0-0  --oplogSize 128

    3.2. Second member

    mongod --replSet rs0 --port 27018 --bind_ip localhost --dbpath ~/data/rs0-1  --oplogSize 128

    3.3. Third member

    mongod --replSet rs0 --port 27019 --bind_ip localhost --dbpath ~/data/rs0-2  --oplogSize 128
  2. Connect to one of your mongod instances through the mongo shell. You will need to indicate which instance by specifying its port number. For the sake of simplicity and clarity, you may want to choose the first one, as in the following command;

mongo --port 27017
  1. In the mongo shell, use rs.initiate() to initiate the replica set. You can create a replica set configuration object in the mongo shell environment, as in the following example:
rsconf = {
 _id: "rs0",
 members: [
   {
    _id: 0,
    host: "localhost:27017"
   },
   {
    _id: 1,
    host: "localhost:27018"
   },
   {
    _id: 2,
    host: "localhost:27019"
   }
  ]
};

rs.initiate(rsconf);
  1. In order to connect to replica set use next mongourl mongodb://localhost:27017,localhost:27018,localhost:27019/stagingApikoApp?replicaSet=rs0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment