Mongodb 简明教程
MongoDB - Replication
复制是指在多个服务器之间同步数据的过程。复制提供冗余并且使用多个数据库服务器中的多份数据副本提高数据可用性。复制可以防止因单个服务器丢失而导致数据库丢失数据。复制还允许从硬件故障和服务中断中恢复。通过额外的数据副本,您可以分配一份副本到灾难恢复、报告或备份中。
Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
Why Replication?
-
To keep your data safe
-
High (24*7) availability of data
-
Disaster recovery
-
No downtime for maintenance (like backups, index rebuilds, compaction)
-
Read scaling (extra copies to read from)
-
Replica set is transparent to the application
How Replication Works in MongoDB
MongoDB 通过使用副本集来实现复制。副本集是一组承载同一数据集的 mongod 实例。在副本中,一个节点是接收所有写入操作的主节点。所有其他实例(如辅助节点)都会从主节点应用操作,以便具有相同的数据集。副本集中只能有一个主节点。
MongoDB achieves replication by the use of replica set. A replica set is a group of mongod instances that host the same data set. In a replica, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node.
-
Replica set is a group of two or more nodes (generally minimum 3 nodes are required).
-
In a replica set, one node is primary node and remaining nodes are secondary.
-
All data replicates from primary to secondary node.
-
At the time of automatic failover or maintenance, election establishes for primary and a new primary node is elected.
-
After the recovery of failed node, it again join the replica set and works as a secondary node.
MongoDB 复制的典型示意图中,客户端应用程序始终与主节点交互,然后主节点将数据复制到辅助节点。
A typical diagram of MongoDB replication is shown in which client application always interact with the primary node and the primary node then replicates the data to the secondary nodes.
Replica Set Features
-
A cluster of N nodes
-
Any one node can be primary
-
All write operations go to primary
-
Automatic failover
-
Automatic recovery
-
Consensus election of primary
Set Up a Replica Set
在本教程中,我们将把独立的 MongoDB 实例转换为副本集。若要转换为副本集,请执行以下步骤:
In this tutorial, we will convert standalone MongoDB instance to a replica set. To convert to replica set, following are the steps −
-
Shutdown already running MongoDB server.
.
-
Start the MongoDB server by specifying — replSet option. Following is the basic syntax of --replSet −
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
Example
mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
-
It will start a mongod instance with the name rs0, on port 27017.
-
Now start the command prompt and connect to this mongod instance.
-
In Mongo client, issue the command rs.initiate() to initiate a new replica set.
-
To check the replica set configuration, issue the command rs.conf(). To check the status of replica set issue the command rs.status().
Add Members to Replica Set
若要向副本集添加成员,请在多台机器上启动 mongod 实例。现在启动一个 mongo 客户端并发出命令 rs.add() 。
To add members to replica set, start mongod instances on multiple machines. Now start a mongo client and issue a command rs.add().
Syntax
rs.add() 命令的基本语法如下:
The basic syntax of rs.add() command is as follows −
>rs.add(HOST_NAME:PORT)
Example
假设你的 mongod 实例名称是 mongod1.net 且它在端口 27017 上运行。若要将此实例添加到副本集中,请在 Mongo 客户端中发出命令 rs.add() 。
Suppose your mongod instance name is mongod1.net and it is running on port 27017. To add this instance to replica set, issue the command rs.add() in Mongo client.
>rs.add("mongod1.net:27017")
>
你只能在连接到主节点时将 mongod 实例添加到副本集中。若要检查你是否已连接到主节点,请在 mongo 客户端中发出命令 db.isMaster() 。
You can add mongod instance to replica set only when you are connected to primary node. To check whether you are connected to primary or not, issue the command db.isMaster() in mongo client.