cd \mongodbdir\
mkdir db1
mkdir db2
mkdir db3
mongod --dbpath ./db1 --port 30000 --replSet "demo"
mongod --dbpath ./db2 --port 40000 --replSet "demo"
mongod --dbpath ./db3 --port 50000 --replSet "demo"
You need to connect to one of the database sets and setup the configuration. The mongo client is a javascript repl so you can do all the usual javascript commands.
var demoConfig = {
_id: "demo",
members: [
{ _id: 0,
host: 'localhost:30000',
priority: 10
},
{ _id: 1,
host: 'localhost:40000'
},
{ _id: 2,
host: 'localhost:50000',
arbiterOnly: true
}
]
};
The rs stands for replica set.
rs.initiate(demoConfig)
Please note that this can take some time to setup the databases initially. When it is complete it will return back:
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
The prompt will change to demo:PRIMARY>
.
To test add some data to the primary:
db.foo.save({_id:1, value:'hello world'})
Check that the data is saved on the primary:
db.foo.find()
This will return:
{ "_id" : 1, "value" : "hello world" }
Then log onto the secondary. This will have a prompt of demo:SECONDARY>
.
db.foo.find()
This will return:
{ "_id" : 1, "value" : "hello world" }
You may get an initial error error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
when you are trying to run db.foo.find()
. This is because the secondary (ie the Slave) is not setup to perform reads. This can be enabled using the following command:
db.setSlaveOk()
You should then be able to access the data.
To check on the status of a replica set just use:
rs.status()