Lesson 6: Deploying a Replica Set in a MongoDB Deployment / Learn

Code Summary: Deploying a Replica Set in a MongoDB Deployment

Review the following code, which demonstrates how to deploy a three-member replica set and initiate an election.

Three Ubuntu servers have been provisioned for this demonstration. We have also opened the firewall between the servers so that they can communicate with each other. The latest version of MongoDB has been installed on each of the servers.

Note that you can also use Windows, Mac, or a different Linux distribution of your choice.

Deploy a Three-Member Replica Set

This section demonstrates how to deploy a three-member replica set and is broken into the following steps:

  • Update the mongod configuration files
  • Create security files
  • Initiate an election
  • Create an admin user

Update the mongod Configuration Files

The first step in deploying a three-member replica set is to update the configuration file for each server. In the mongod config file on all three servers, update the network, security, and replication settings. The following sections show this process for each server.

Update the mongod Config File for Server One

The following code shows how to update the mongod configuration file for server one:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,<mongodb.repl.member.one.domain>


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  keyFile: /etc/mongodb/pki/mongod-keyfile
  authorization: enabled

#operationProfiling:

replication:
  replSetName: mongodb-repl-example


#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Update the mongod Config File for Server Two

The following code shows how to update the mongod configuration file for server two:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,<mongodb.repl.member.two.domain>


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  keyFile: /etc/mongodb/pki/mongod-keyfile
  authorization: enabled

#operationProfiling:

replication:
  replSetName: mongodb-repl-example


#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Update the mongod Config File for Server Three

The following code shows how to update the mongod configuration file for server three:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,<mongodb.repl.member.three.domain>


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  keyFile: /etc/mongodb/pki/mongod-keyfile
  authorization: enabled

#operationProfiling:

replication:
  replSetName: mongodb-repl-example


#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Create Security Files

The next step in deploying the three-member replica set is to create a new directory and generate a new security key, which will be used by the replica set. Then, restart mongod. These steps are shown by the following code:

# Create a new directory to hold the security key
sudo mkdir -p /etc/mongodb/pki

# Generate the security key using openssl only on server 1

openssl rand -base64 756 > /tmp/keyfile

# Open the correct permissions

chmod 0400 /tmp/keyfile

# Move the keyfile to the pki directory

sudo mv /tmp/mongod-keyfile /etc/mongodb/pki/

# Give the mongodb user ownership of the pki directory

sudo chown -R mongodb. /etc/mongodb/pki

# Restart the mongod process

sudo systemctl restart mongod

Copy the Key

Copy the newly created key by using the spc command. Here’s an example:

scp /tmp/keyfile mongod1.replset.com:/tmp

scp /tmp/keyfile mongod2.replset.com:/tmp

Add the Security Key to Server Two

Create a new directory for the security key and restart the mongod process on server two, as shown in the following code.

# Create a new directory to hold the security key

sudo mkdir -p /etc/mongodb/pki/


# Give the mongodb user ownership of the pki directory

sudo chown -R mongodb. /etc/mongodb/pki/

# Open the correct permissions

chmod 0400 /etc/mongodb/pki/

# Restart the mongod process

sudo systemctl restart mongod

Add the Security Key to Server Three

Create a new directory for the security key and restart the mongod process on server three, as shown in the following code:

# Create a new directory to hold the security key

sudo mkdir -p /etc/mongodb/pki/


# Give the mongodb user ownership of the pki directory

sudo chown -R mongodb. /etc/mongodb/pki/

# Open the correct permissions

chmod 0400 /etc/mongodb/pki/

# Restart the mongod process

sudo systemctl restart mongod

Initiate the Replica Set

Connect on server one by using the mongosh command and switch to the admin database. Use rs.initiate() with a document that contains the replica set as the _id and the hosts’ names. Here’s an example:

mongosh

use admin

rs.initiate(
  {
     _id: "mongodb-repl-example",
     version: 1,
     members: [
        { _id: 0, host : "mongod0.replset.com" },
        { _id: 1, host : "mongod1.replset.com" },
        { _id: 2, host : "mongod2.replset.com" }
     ]
  }
)

Create an Admin User

On server one, create an admin user that’s able to authenticate to the replica set. Here’s an example:

db.createUser({
   user: "dba-admin",
   pwd: "dba-pass",
   roles: [
     {role: "root", db: "admin"}
   ]
 })

Exit mongosh and then log back in to the replica set. For example:

exit

mongosh "mongodb://dba-admin:dba-pass@<server-one-ip:port>,<server-two-ip:port>,<server-three-ip:port>/?authSource=admin&replicaSet=mongodb-repl-example"

Once connected, run rs.status() in the shell to check the members array.

Initiate an Election

To initiate an election, use the rs.stepDown() command:

rs.stepDown()