Create replica set for MongoDB in Linux

2k Views Asked by At

When I run the following command: rs.initiate(),

I get the following error:

"info2" : "no configuration explicitly specified -- making one",
        "me" : "ip-10-0-2-113:27017",
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set s-1-rs maps to this node",
        "code" : 93

I'm just running on the local host. I'm following this guide: http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/

I just skipped the part where they named the replica set.

In any case, how can I create a replica set and what am I doing wrong?

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER
-- Replace your data and installation location accordingly.
rm -rf ~/tmp/mongo_data
mkdir -p ~/tmp/mongo_data/rs0_1 ~/tmp/mongo_data/rs0_2 ~/tmp/mongo_data/rs0_3  

nohup ~/Softwares/mongodb/bin/mongod --port 27017 --dbpath ~/tmp/mongo_data/rs0_1 --replSet rs0 &
nohup ~/Softwares/mongodb/bin/mongod --port 27018 --dbpath ~/tmp/mongo_data/rs0_2 --replSet rs0 &
nohup ~/Softwares/mongodb/bin/mongod --port 27019 --dbpath ~/tmp/mongo_data/rs0_3 --replSet rs0 &

~/Softwares/mongodb/bin/mongo --port 27017

rsconf = {
       _id: "rs0",
       members: [
                  {
                   _id: 0,
                   host: "localhost:27017"
                  }
                ]
     }

rs.initiate(rsconf)
rs.conf()


rs.add("localhost:27018")
rs.add("localhost:27019")

rs.status()
0
On

Giving whole procedure for starting mongo replication locally. Might help some one in future.

Create three directories for the three mongod processes. On unix, this could be done as follows:

mkdir -p /data/rs1 /data/rs2 /data/rs3

Now start three mongo instances as follows. Note that are three commands.

mongod --replSet s1 --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --oplogSize 64 --fork

mongod --replSet s1 --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --oplogSize 64 --fork

mongod --replSet s1 --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --oplogSize 64 --fork

Creating replica set of s1.

Now connect to a mongo shell.

mongo --port 27017

Now you will create the replica set. Type the following commands into the mongo shell:

config = { _id: "s1", members:[
          { _id : 0, host : "localhost:27017"},
          { _id : 1, host : "localhost:27018"},
          { _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);

By this your replica set is started. You can check state of replication by:

rs.status()