- Replication >
- Replica Set Tutorials >
- Replica Set Deployment Tutorials >
- Deploy a Replica Set
Deploy a Replica Set¶
This tutorial describes how to create a three-member replica
set from three existing mongod
instances running with
access control disabled.
To deploy a replica set with enabled access control, see Deploy Replica Set With Keyfile Access Control. If you wish to deploy a replica set from a single MongoDB instance, see Convert a Standalone to a Replica Set. For more information on replica set deployments, see the Replication and Replica Set Deployment Architectures documentation.
Overview¶
Three member replica sets provide enough redundancy to survive most network partitions and other system failures. These sets also have sufficient capacity for many distributed read operations. Replica sets should always have an odd number of members. This ensures that elections will proceed smoothly. For more about designing replica sets, see the Replication overview.
The basic procedure is to start the mongod
instances that
will become members of the replica set, configure the
replica set itself, and then add the mongod
instances to it.
Requirements¶
For production deployments, you should maintain as much separation between
members as possible by hosting the mongod
instances on separate machines. When using virtual machines for
production deployments, you should place each mongod
instance on a separate host server serviced by redundant power circuits
and redundant network paths.
Before you can deploy a replica set, you must install MongoDB on each system that will be part of your replica set. If you have not already installed MongoDB, see the installation tutorials.
Before creating your replica set, you should verify that your network configuration allows communication among all members; i.e. each member must be able to connect to every other member. For instructions on how to check your connection, see Test Connections Between all Members.
Considerations When Deploying a Replica Set¶
Architecture¶
In a production, deploy each member of the replica set to its own machine
and if possible bind to the standard MongoDB port of 27017
. Use the
bind_ip
option to ensure that MongoDB listens for connections
from applications on configured addresses.
See Replica Set Deployment Architectures for more information.
Connectivity¶
Ensure that network traffic can pass between all members of the set and all clients in the network securely and efficiently. Consider the following:
- Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.
- Configure access control to prevent connections from unknown clients to the replica set.
- Configure networking and firewall rules so that incoming and outgoing packets are permitted only on the default MongoDB port and only from within your deployment.
Finally ensure that each member of a replica set is accessible by
way of resolvable DNS or hostnames. You should either configure your
DNS names appropriately or set up your systems’ /etc/hosts
file to
reflect this configuration.
Configuration¶
Specify the run time configuration on each system in a configuration
file stored in /etc/mongod.conf
or a related location. Create the directory where MongoDB stores data
files before deploying MongoDB.
For more information about the run time options used above and other configuration options, see Configuration File Options.
Procedure¶
The following procedure outlines the steps to deploy a replica set when access control is disabled.
Start each member of the replica set with the appropriate options.¶
For each member, start a mongod
instance with the
following settings:
Set
replication.replSetName
option to the replica set name,If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.
Set any other settings as appropriate for your deployment.
In this tutorial, the three mongod
instances are
associated with the following hosts:
Replica Set Member | Hostname |
---|---|
Member 0 | mongodb0.example.net |
Member 1 | mongodb1.example.net |
Member 2 | mongodb2.example.net |
The following example specifies the replica set name through the
--replSet
command-line option:
Alternatively, you can also specify the replica set name
in a configuration file:
To start mongod
with a configuration file, specify the
configuration file’s path with the --config
option:
In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.
Connect a mongo
shell to one of the mongod
instances.¶
From the same machine where one of the mongod
is running
(in this tutorial, mongodb0.example.net
), start the mongo
shell. To connect to the mongod
listening to localhost on
the default port of 27017
, simply issue:
Depending on your path, you may need to specify the path to the
mongo
binary.
Initiate the replica set.¶
From the mongo
shell, run rs.initiate()
on
replica set member 0.
Important
Run rs.initiate()
on just one and only one
mongod
instance for the replica set.
MongoDB initiates a replica set, using the default replica set configuration.
View the replica set configuration.¶
Use rs.conf()
to display the replica set configuration
object:
The replica set configuration object resembles the following:
Ensure that the replica set has a primary.¶
Use rs.status()
to identify the primary in the replica set.