Database Reference
In-Depth Information
Creating a Replica Set
The best way to learn how to create a replica set is by looking at an example. In the example that follows, you will
create a replica set called testset . This set will have three members (two active and one passive). Table 11-1 lists the
members of this set.
Table 11-1. Configuring the Replica Set
Service
Daemon Address
Dbpath
mongod
[hostname]:27021 /db/active1/data
Active Member 1
mongod
[hostname]:27022 /db/active2/data
Active Member 2
Passive Member 1 mongod
[hostname]:27023 /db/passive1/data
Replica sets will allow you to use localhost as an identifier, but only when all machines are co-located on one
server. This occurs as each member of a replica set must be able to contact all other MongoDB instances via hostname
for replication to function.
Normally when working with replica sets we use hostnames; you can find the current hostname by using th e
hostname command, as in the following example:
$hostname
Pixl.local
In the examples that follow, replace the term [hostname] with whatever value is returned by running the
hostname command on your own system.
Getting a Replica Set Member Up and Running
The first step is to get the first active member up and running. To do so, open a terminal window and type the
following:
$ mkdir -p /db/active1/data
$ mongod --dbpath /db/active1/data --port 27021 --replSet testset
The --replSet option tells the instance the name of the replica set it is joining. This is the first member of the
replica set, so you can give it the address of any other member, even if that member has not been started up yet. Only
one member address is required, but you can also provide the names of other members by separating their addresses
with commas, as shown in the following example:
$ mongod --dbpath /db/active1/data --port 27021 --replSet testset
If you don't wish to run each of these MongoDB instances in its own shell instance, you can add the --fork
and --logpath < file > options to tell this instance to open itself in the background and direct its logging to the
specified file.
Note
 
 
Search WWH ::




Custom Search