Deploying a Replica Set

Implementing a replica set is very simple in MongoDB. The following steps take you through the process of prepping and deploying a replica set:

1. Ensure that all the members in the replica set are accessible to each other using DNS or hostnames. Adding a virtual private network for the replica servers to communicate on will enhance the performance of the system because the replication process will not be affected by other traffic on the network. If the servers are not behind a DMZ so the data communications are safe, then you should also configure an auth and a kwFile for the servers to communicate on for security.

2. Configure the replSet value, which is a unique name for the replica set either in the mongodb.conf file or on your command line for each server in the replica set. For example:

mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0

3. Start the MongoDB client, using the mongo command, and execute the following command on each server in the replica set to initiate the replica set operations:

rs.initiate()

4. Use the MongoDB shell to connect to the MongoDB server that will act as the primary and execute the following command for each secondary host:

rs.add(<secondary_host_name_or_dns>)

5. Use the following command to view the configuration on each server:

rs.conf()

6. Inside your application, define the read preference for reading data from the replica set. As described in earlier chapters, you do this by setting the preference to primary, primaryPreferred, secondary, secondaryPreferred, or nearest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.21.229