-
Notifications
You must be signed in to change notification settings - Fork 0
Replicate Set Mongo
On this tutorial, I will explain how to set a Replicate for MongoDB.
Firstly, we need to stop mongo process:
mongodb stop
Then, create directories for stocking the database:
mkdir data/db1
mkdir data/db2
mkdir data/db3
Launch the mongodb instance for each directory:
mongod --port 27017 --dpath data/db1 --replSet repl
mongod --port 27018 --dpath data/db2 --replSet repl
mongod --port 27019 --dpath data/db3 --replSet repl
The replicate set is called 'repl' for this example.
The first part is done, now wee need to configure the replicate set.
Launch the mongo client (without parameters):
mongo
Into mongo client, we need to write configuration:
cfg = {_id:"repl", members : [{_id: 0, host: "localhost:27017"},{_id:1, host:"localhost:27018"},{_id:2, host:"localhost:27019"}] }
Free to you to change the url of mongoDB instance if instances are located on different servers.
We need to initiate it:
rs.initiate(cfg)
Then, you will see a success message like:
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
After few seconds, you will see servers instance for checking master and slaves replicate set. On the client mongo, push the 'Enter' button, you will see the prompt changing:
rep:PRIMARY>
If this appended, that is to say your replication set is Done.