When creating a replica set locally, you must use
localhost:port_number in the "host" field of the configuration file.
From your output, "myhost" is unrecognized, which is most likely where
the issue lies.
Here are the steps that you can follow to get your replica set up and
running localy:
Create your three data directories: /data/r1, /data/r2, and /data/r3.
If they already exist, make sure they are empty; we will create the
replica set from scratch. (I imagine that because you are just
testing, you do not have any valuable data in these folders, but if
you do, please relocate the files, instead of deleting them!)
In a terminal, start up the first mongod:
$ ./mongod --replSet mySet --dbpath /data/r1 --port 27017 --oplogSize
700
Adding --oplogSize 700 will limit the size of the oplog for each
mongod to 700MB, instead of taking up the default 5% of the free disk
space on the volume.
More data on the oplog may be found here:
http://www.mongodb.org/display/DOCS/Replica+Sets+-+Oplog
When you start each Replica set member, you should get log messages
like this:
Tue Mar 13 13:33:06 [rsStart] replSet can't get local.system.replset
config from self or any seed (EMPTYCONFIG)
Tue Mar 13 13:33:06 [rsStart] replSet info you may need to run
replSetInitiate -- rs.initiate() in the shell -- if that is not
already done
This is expected.
Now open up two new terminal windows and start up the other mongo
instances, one process per window. Be sure to change the dbpath and
port variables appropriately
$ ./mongod --replSet mySet --dbpath /data/r2 --port 27018 --oplogSize
700
$ ./mongod --replSet mySet --dbpath /data/r3 --port 27019 --oplogSize
700
You now have three mongod processes up and running, but the Repica set
is not yet configured.
In a fourth terminal window, start mongo, and connect to the first
process that was started. In mongo, port 27017 is the default port,
but I have included it just to be explicit, in case any other users
may be following this example and using different port numbers.
$ ./mongo --port 27017
MongoDB shell version: 2.0.2
connecting to:
127.0.0.1:27017/test
>
Now we create our config object as per the documentation: Be sure to
keep the _id the same as the value for --replSet that you started all
of the mongod processes with:
> cfg = {
_id : "mySet",
members : [
{ _id : 0, host : "localhost:27017" },
{ _id : 1, host : "localhost:27018" },
{ _id : 2, host : "localhost:27019" } ] }
Now, initiate the replica set with this configuration document:
> rs.initiate(cfg)
{
"info" : "Config now saved locally. Should come online in about a
minute.",
"ok" : 1
}
> # At this point, a simple ">" is returned. Hitting "return" a few times should cause ">" to change to "PRIMARY>", indicating that mongo is connected to the Primary member of a replica set. We can then run rs.status() to see the configuration of our replica set.
PRIMARY> rs.status()
{
"set" : "mySet",
"date" : ISODate("2012-03-13T18:08:08Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1331662006000,
"i" : 1
},
"optimeDate" : ISODate("2012-03-13T18:06:46Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79,
"optime" : {
"t" : 1331662006000,
"i" : 1
},
"optimeDate" : ISODate("2012-03-13T18:06:46Z"),
"lastHeartbeat" : ISODate("2012-03-13T18:08:07Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "localhost:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79,
"optime" : {
"t" : 1331662006000,
"i" : 1
},
"optimeDate" : ISODate("2012-03-13T18:06:46Z"),
"lastHeartbeat" : ISODate("2012-03-13T18:08:07Z"),
"pingMs" : 267583
}
],
"ok" : 1
}
PRIMARY>
Hopefully, this will clear up any ambiguities in the tutorial, and get
you up and running with a locally running replica set that you can
experiment with.
For more information on replica sets, please see the "Replica Set
Configuration" documentation:
http://www.mongodb.org/display/DOCS/Replica+Set+Configuration
Please also see the documentation titled "Replica Set Commands":
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
This contains information on the rs.add() and rs.remove() methods,
which allow you to add and remove members as per your original
question.
Please let us know if you run into any difficulties or have any
additional questions regarding replica sets. Good luck!