Tuesday, September 6, 2011

Setup mongodb replica sets

Here is a sample how to setup and run mongodb replica sets with 3 members (2 for data and 1 arbiter). We'll run on localhost on ports 27017, 27018 and arbiter on 27019.

# download mongodb here
# create data and log directories for all replica members

touch /var/log/mongodb/mongo1.log
touch /var/log/mongodb/mongo2.log
touch /var/log/mongodb/mongo3.log


mkdir /var/lib/mongodb/data1/db
mkdir /var/lib/mongodb/data2/db
mkdir /var/lib/mongodb/data3/db

# create configuration files for members

/etc/mongo1.conf:

dbpath = /var/lib/mongodb/data1/db
bind_ip = 127.0.0.1
noauth = true
verbose = true
port=27017
logpath=/var/log/mongodb/mongo1.log
logappend=true
replSet=mySetName

/etc/mongo2.conf:

dbpath = /var/lib/mongodb/data2/db
bind_ip = 127.0.0.1
noauth = true
verbose = true
port=27018
logpath=/var/log/mongodb/mongo2.log
logappend=true
replSet=mySetName

/etc/mongo3.conf:

dbpath = /var/lib/mongodb/data3/db
bind_ip = 127.0.0.1
noauth = true
verbose = true
port=27019
logpath=/var/log/mongodb/mongo3.log
logappend=true
replSet=mySetName

# run all members (use --rest if you want to use web admin UI, http://localhost:28017)

mongod --rest --config /etc/mongo1.conf &
mongod --rest --config /etc/mongo2.conf &
mongod --rest --config /etc/mongo3.conf &

# init replica sets in mongo shell

$ mongo
> cfg = {_id:"mySetName", members:[
{_id:0,host:"127.0.0.1:27017"},
{_id:1,host:"127.0.0.1:27018"},
{_id:2,host:"127.0.0.1:27019",arbiterOnly:true}
]}
> rs.initiate(cfg);
> rs.status()

No comments:

Post a Comment