MongoDB has become the most famous NoSQL database in the market. MongoDB is document-oriented, and Its Schema-free design makes it popular in a variety of WEB applications.
MongoDB has become the most famous NoSQL database in the market. MongoDB is document-oriented, and Its Schema-free design makes it popular in a variety of WEB applications.
MongoDB has become the most famous NoSQL database in the market. MongoDB is document-oriented, and Its Schema-free design makes it popular in a variety of WEB applications. One of my favorite features is its Replica Set, which places multiple copies of the same data on a group of mongod nodes, this achieves data redundancy and high availability.
This tutorial describes how to configure a MongoDB replica set.
The most common configuration of replica sets requires one master node and multiple slave nodes. After that, the replication behaviors started from this master node to other slave nodes. The replica set not only protects the database against unexpected hardware faults and downtime events, but also increases the data read throughput of the database client by providing more nodes.
Configure the environment
In this tutorial, We will configure a replica set that includes one master node and two slave nodes.
To achieve this goal, we use three virtual machines running on VirtualBox. I will install Ubuntu 14.04 on these VMS and install the official MongoDB package.
I will configure the required environment on a virtual machine instance, and then clone it to another virtual machine instance. Therefore, select a virtual machine named master and perform the following installation process.
First, we need to add a MongoDB key to apt:
Then, add the official MongoDB repository to source. list:
$ Sudo su
# Echo "deb" $ (lsb_release-SC) "/mongodb-org/3.0 multiverse" | sudo tee/etc/apt/sources. list. d/mongodb-org-3.0.list
Next, update the apt repository and install MongoDB.
Now make some changes to/etc/mongodb. conf
The role of the first line is to indicate that our database needs to be verified before it can be used. Keyfile: The key file used to copy data between MongoDB nodes. ReplSet sets a name for the replica set.
Next, we will create a key file for all instances.
This will create a key file containing the MD5 string, but because it contains some noise, we need to clear them before they can be officially used in MongoDB.
The grep command prints the MD5 string after filtering out spaces and other unwanted content.
Now we have some operations on the key file to make it available.
Next, disable the virtual machine. Clone the Ubuntu system to another virtual machine.
This is the cloned secondary Node 1 and secondary Node 2. Make sure that you have reinitialized their MAC addresses and cloned the entire hard disk.
Note that the three virtual machine examples must be in the same network for mutual communication. Therefore, we need them to access the Internet.
We recommend that you set a static IP address for each virtual machine instead of DHCP. In this way, they will not lose connection when DHCP assigns IP addresses to them.
Edit the/etc/networks/interfaces file for each virtual machine as follows.
On the master node:
On the secondary Node 1:
On secondary Node 2:
Because we do not have DNS service, we need to set the/etc/hosts file and manually put the host name in this file.
On the master node:
On the secondary Node 1:
On secondary Node 2:
Run the ping command to check connections between nodes.
Configure replica set
After verifying that each node can be connected normally, we can create a new administrator user for subsequent replica set operations.
On the master node, open the/etc/mongodb. conf file and comment out auth and replSet.
Comment out the auth line before configuring any user or replica set on a newly installed MongoDB instance. By default, MongoDB does not create any users. If auth is enabled before you create a user, you cannot do anything. You can enable auth again after creating a user.
After modifying/etc/mongodb. conf, restart the mongod process.
$ Sudo service producer D restart
Now connect to the MongoDB master:
After connecting to MongoDB, create an administrator user.
Restart MongoDB:
$ Sudo service producer D restart
Connect to MongoDB again and use the following command to add the secondary Node 1 and secondary Node 2 to our replica set.
Now that the replica set is ready, we can start our project. Refer to the official driver documentation to learn how to connect to the replica set. If you want to use Shell to request data, you need to connect to the master node to insert or request data. The secondary node does not work. If you want to try the replica set operation, the following error message will pop up and say hello to you.
If you want to connect to the entire replica set from the shell, you can install the following command. In the replica set, failover fails automatically.
If you use other driver languages (such as JavaScript and Ruby), the format may be different.
I hope this tutorial will help you. You can use Vagrant to automatically configure your local environment and accelerate your code.
MongoDB 3.0 official version released and downloaded
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
Via: How to set up a Replica Set on MongoDB
Author: Christopher Valerio Translator: mr-ping Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: