- Find mongodb data folder
grep -i dbPath /etc/mongod.conf
- 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
-
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
-
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
- 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);
- In order to connect to replica set use next mongourl
mongodb://localhost:27017,localhost:27018,localhost:27019/stagingApikoApp?replicaSet=rs0