CentOS7.4 build mongodb4.2.8 replica set

Introduction to replica set nodes

1) Data node-------Master node

     The master node is responsible for data read and write operations, and records the write operations in OpLog (the oplog collection of the mongo hidden library local)

2) Data node-slave node

     Replicate the data of the master node for disaster recovery. If the master node is down, a new master node will be re-elected. The slave node cannot perform write operations, even if it has root privileges (unlike MySQL)

3) Voting node

            Not responsible for data storage and copying, only responsible for voting. (Note: In theory, a mongo replication set can have one master node, multiple slave nodes, and multiple voting nodes)

Way of working

1) The client driver points to the mongo database. When writing, it will only be done on the master node. The written information will be recorded in the oplog, and the slave node will copy the write operation to the slave node according to the oplog.

2) Asynchronous operation of the replica set, the slave node will have a certain delay under the influence of the efficiency of the flash disk or the network problem, so the read operation is also directed to the master node by default. If the real-time requirements are not high, you can configure to point to the slave node to achieve reading Write separation.

Setting up the environment

1. Download the installation package:

cd /usr/local

mkdir mongos

cd mongos

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.8.tgz

2. Unzip the installation package

tar -zxvf  mongodb-linux-x86_64-rhel70-4.2.8.tgz
 
#修改文件解压后的文件名称
mv mongodb-linux-x86_64-rhel70-4.2.8 mongodb4.2.8

3. Create a directory

cd mongodb4.2.8/
 
#在解压后的mongodb目录新建 data  log  config目录
mkdir data|mkdir log|mkdir config
 
#在data和config目录分别创建rs1  rs2 rs3目录
cd data && mkdir rs1|mkdir rs2|mkdir rs3
cd config && mkdir rs1|mkdir rs2|mkdir rs3

4. Create instance launch configuration

#由于开启认证所以要创建一个keyfile,并且拷贝到其他从节点
cd /usr/local/mongos/mongodb4.2.8/
openssl rand -base64 756 > config/autokey
chmod 400  config/autokey

cd config
cp autokey rs1
cp autokey rs2
cp autokey rs3
#分别在/usr/local/mongos/mongodb4.2.8/config/   rs1、rs2、rs3下创建配置文件 
touch mongod.cfg
 
#rs1配置mongod.cfg
dbpath=/usr/local/mongos/mongodb4.2.8/data/rs1        			 #mongo数据安装目录
logpath=/usr/local/mongos/mongodb4.2.8/log/rs1.log    			 #日志打印的目录
journal=true                                             #数据是否故障恢复
port=27017                                               #端口
replSet=rs                                               #复制集名称
logappend=true                                           #复制集日志是以追加的方式进行
fork = true                                              #是否后台启动
bind_ip=0.0.0.0                                          #绑定主机


#是否认证
auth = true
#存储引擎,有mmapv1、wiretiger、mongorocks
storageEngine=wiredTiger
keyFile=/usr/local/mongos/mongodb4.2.8/config/rs1/autokey


 
 
#rs2配置mongod.cfg
dbpath=/usr/local/mongos/mongodb4.2.8/data/rs2        			 #mongo数据安装目录
logpath=/usr/local/mongos/mongodb4.2.8/log/rs2.log    			 #日志打印的目录
journal=true                                             #数据是否故障恢复
port=27017                                               #端口
replSet=rs                                               #复制集名称
logappend=true                                           #复制集日志是以追加的方式进行
fork = true                                              #是否后台启动
bind_ip=0.0.0.0                                          #绑定主机


#是否认证
auth = true
#存储引擎,有mmapv1、wiretiger、mongorocks
storageEngine=wiredTiger
keyFile=/usr/local/mongos/mongodb4.2.8/config/rs2/autokey


 
 
#rs3配置mongod.cfg
dbpath=/usr/local/mongos/mongodb4.2.8/data/rs3        			 #mongo数据安装目录
logpath=/usr/local/mongos/mongodb4.2.8/log/rs3.log    			 #日志打印的目录
journal=true                                             #数据是否故障恢复
port=27017                                               #端口
replSet=rs                                               #复制集名称
logappend=true                                           #复制集日志是以追加的方式进行
fork = true                                              #是否后台启动
bind_ip=0.0.0.0                                          #绑定主机


#是否认证
auth = true
#存储引擎,有mmapv1、wiretiger、mongorocks
storageEngine=wiredTiger
keyFile=/usr/local/mongos/mongodb4.2.8/config/rs3/autokey

5. Start the instance

cd /usr/local/mongos/mongodb4.2.8

bin/mongod --config /usr/local/mongos/mongodb4.2.8/config/rs1/mongod.cfg 
bin/mongod --config /usr/local/mongos/mongodb4.2.8/config/rs2/mongod.cfg 
bin/mongod --config /usr/local/mongos/mongodb4.2.8/config/rs3/mongod.cfg 

#检查实例启动情况
ps -ef|grep mongo

6. Join the replica set cluster

#使用命令行,进入到27017实例
bin/mongo -port 27017
 
#进入admin
use admin

#初始化一个副本集
rs.initiate()

#创建用户
db.createUser({ user: 'root', pwd: 'Aa123456..', roles: [{ role: "root", db: "admin" }]});

#登录
db.auth("root","Aa123456..") 
 
#查看副本集状态命令
rs.conf()
 
#加入secondary节点
rs.add("localhost:27001")
#期间可能会报错 Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2" 节点的host的限制导致的修改为localhost

conf=rs.conf()
conf.members[0].host='localhost:27017'
rs.reconfig(conf)
 
#加入投票节点
rs.addArb("localhost:27002")
 
#查看副本集状态
rs.status()

#没有问题后退出
 
#重要且必须, secondary节点允许读取操作在当前节点进行,否则从节点无法复制主节点的数据
#进入secondary实例
bin/mongo -port 27001
rs.slaveOk()

7. Verification

#主节点
use test
db.createCollection('user')
db.user.insert({'name':'james'})
     
#从节点
use test
show collections
db.user.find()

 

Guess you like

Origin blog.csdn.net/qq_42407917/article/details/111595677