Deploy MongoDB replication set (replica set) to deploy MongoDB replication set (replica set)

Deploy MongoDB replication set (replica set)

 
surroundings
Operating system: Ubuntu 18.04
MongoDB: 4.0.3

server
3 servers deployed first, master node 1 from node 2 +
3 content servers ip are:
10.140.0.5 (master node)
10.140.0.6 (from node 01)
10.140.0.7 (from node 02)

Install MongoDB
Next, you need to install MongoDB on each server.
Complete the installation process can refer to the official documentation .
For convenience, this article provides one-click install scripts for MongoDB .

Switch to the root user
sudo su -

 

Run the installation script
wget https://gitlab.com/caizhifei2003/scripts/raw/master/install/mongodb/ubuntu-1804.sh
chmod u+x ubuntu-1804.sh
./ubuntu-1804.sh

 

At this point, you can enter commands by mongo database


Configuration Replication Set
 
Set Replication set name
 
There are two ways to set the name of the replication set. One is through mongod command, and the other is by modifying the configuration file.
As used herein, by modifying the configuration file way to set the replication set name, ensure that each node can be restarted using the same configuration to start the database.

MongoDB open the configuration file on Ubuntu
vim /etc/mongod.conf

Find replication configuration section
replication:
    replSetName: "rs0"

 

Binding IP address of MongoDB
 
Find the net configuration section
net:
    port: 27017
    bindIp: localhost,10.140.0.5
This is the address of the master, the slave node corresponding to bind
10.140.0.6
10.140.0.7

Save the file, restart the service mongod
service mongod restart

 

In this case, enter into any node. View replication set state.


Initialization replication set
The master node into the database by mongo shell
Copy command set to perform initialization
复制代码
rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "10.140.0.5:27017" },
      { _id: 1, host: "10.140.0.6:27017" },
      { _id: 2, host: "10.140.0.7:27017" }
   ]
})
复制代码

 

View replication set state
rs.status()


This, copy the set has been created successfully.

Test data synchronization
Run the following commands, inserted into a data on the primary node
use test
db.test.insertOne({"name": "kenny"})

 

Enter from any node to see whether the data is synchronized.
use test
rs.slaveOk()
db.test.find()
By default, MongoDB only the writing and reading of data on the primary node allowed. rs.slaveOk here () in order to read the data from the node needs to perform session-level commands (If you exit mongo shell, need to re-execute the command).
What is displayed as follows:


The master node re-election test
在3个节点上分别使用rs.isMaster()命令
10.140.0.5:


10.140.0.6:


10.140.0.7


强制关闭主节点上的MongoDB服务
use admin
db.shutdownServer()

 

在两个从节点上,通过rs.isMaster()命令查看状态

10.140.0.6:


10.140.0.7:


从上面的运行结果可以看出,10.140.0.6已经被推选成新的主节点。此时所有服务一切正常。
至此,部署MongoDB复制集的基本操作已经完成。

总结
  MongoDB复制集的部署还是比较简单的, 官方文档也给出了较为详细的说明。
  同任何其它数据库一样,MongoDB的副本集也是为了增强数据的安全性,避免因为服务器出现异常时,而导致数据服务不可用的情况出现。同时,数据被完整的保存在多个节点中,任何一台服务器的硬盘出现问题,都不会丢失数据。但是这里也存在风险,那就是数据同步存在时间差,如果还没有等到数据被同步到从节点,主节点就当机的话,那么这部分数据是无法找回的。
  官方建议的副本集节点数量是3个,1个主节点+2个从节点。或者是1个主节点+1个从节点+1个仲裁节点。仲裁节点的作用是在主节点不可用时,通过算法找到最适合的从节点成为新的主节点。不建议将仲裁节点和数据节点放在同一个服务器上。
环境
操作系统:Ubuntu 18.04
MongoDB: 4.0.3

服务器
首先部署3台服务器,1台主节点 + 2台从节点
3台服务器的内容ip分别是:
10.140.0.5 (主节点)
10.140.0.6 (从节点01)
10.140.0.7 (从节点02)

安装MongoDB
接下来,需要在每一台服务器上安装MongoDB。
完整安装过程可参考 官方文档
为了方便,本文提供 MongoDB的一键安装脚本

切换成root用户
sudo su -

 

运行安装脚本
wget https://gitlab.com/caizhifei2003/scripts/raw/master/install/mongodb/ubuntu-1804.sh
chmod u+x ubuntu-1804.sh
./ubuntu-1804.sh

 

此时,可以通过mongo命令进入数据库


配置复制集
 
设置复制集名称
 
有两种方式来设置复制集的名称。一种是通过mongod命令,另一种是通过修改配置文件。
本文使用通过修改配置文件的方式来设置复制集名称,确保每次节点重新启动后能够使用相同的配置启动数据库。

打开MongoDB在Ubuntu上的配置文件
vim /etc/mongod.conf

找到replication配置节
replication:
    replSetName: "rs0"

 

绑定MongoDB的IP地址
 
找到net配置节
net:
    port: 27017
    bindIp: localhost,10.140.0.5
这里是主节点的地址,相应的从节点要绑定
10.140.0.6
10.140.0.7

保存文件,重新启动mongod服务
service mongod restart

 

此时,进入到任何一个节点。查看复制集状态。


初始化复制集
通过mongo shell进入主节点的数据库
执行复制集初始化命令
复制代码
rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "10.140.0.5:27017" },
      { _id: 1, host: "10.140.0.6:27017" },
      { _id: 2, host: "10.140.0.7:27017" }
   ]
})
复制代码

 

查看复制集状态
rs.status()


到此,复制集已经创建成功。

测试数据同步
执行如下命令,在主节点上插入一条数据
use test
db.test.insertOne({"name": "kenny"})

 

进入任何一个从节点,查看数据是否被同步。
use test
rs.slaveOk()
db.test.find()
默认情况下,MongoDB只允许在主节点上写入和读取数据。这里的rs.slaveOk()是为了在从节点上也读取数据而需要执行的session(如果退出mongo shell,需要重新执行该命令)级别的命令。
显示的内容如下:


测试重新选举主节点
在3个节点上分别使用rs.isMaster()命令
10.140.0.5:


10.140.0.6:


10.140.0.7


强制关闭主节点上的MongoDB服务
use admin
db.shutdownServer()

 

在两个从节点上,通过rs.isMaster()命令查看状态

10.140.0.6:


10.140.0.7:


从上面的运行结果可以看出,10.140.0.6已经被推选成新的主节点。此时所有服务一切正常。
至此,部署MongoDB复制集的基本操作已经完成。

总结
  MongoDB复制集的部署还是比较简单的, 官方文档也给出了较为详细的说明。
  同任何其它数据库一样,MongoDB的副本集也是为了增强数据的安全性,避免因为服务器出现异常时,而导致数据服务不可用的情况出现。同时,数据被完整的保存在多个节点中,任何一台服务器的硬盘出现问题,都不会丢失数据。但是这里也存在风险,那就是数据同步存在时间差,如果还没有等到数据被同步到从节点,主节点就当机的话,那么这部分数据是无法找回的。
  官方建议的副本集节点数量是3个,1个主节点+2个从节点。或者是1个主节点+1个从节点+1个仲裁节点。仲裁节点的作用是在主节点不可用时,通过算法找到最适合的从节点成为新的主节点。不建议将仲裁节点和数据节点放在同一个服务器上。

Guess you like

Origin www.cnblogs.com/xibuhaohao/p/12044312.html