Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB replication set Construction & simulated switching of Master Server

Shulou Source: shulou.com Published: 2022-06-01 22:22:51 09月18日 Update

Preface

A Mongodb replication set consists of a set of Mongod instances (processes), including a Primary node and multiple Secondary nodes. All data of the Mongodb Driver (client) is written to the data synchronously written by Primary,Secondary from the Primary to keep all members of the replication set storing the same dataset and provide high availability of data.

The advantages of replication sets are as follows

Make data more secure

High data availability (724)

Disaster recovery

No-downtime maintenance (such as backup, index reconstruction, failover)

Read zoom (additional copy read)

Replica sets are transparent to applications.

1. Experimental environment

MongoDB copy structure diagram:

Second, the experimental steps

1.yum install mongodb-org

two。 Create multiple instances of mongodb

3. Configure replication set

4. Add and delete nodes

5. Master server simulation handover

III. Content of the experiment

-- yum installs monggodb-org-

Vim / etc/yum.repos.d/mongodb.repo [mongodb-org] name=MongoDB Repositorybaseurl= https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc yum install mongodb-org-y

Modify the listening address in the configuration file

Vim / etc/mongod.confnet: port: 27017 bindIp: 0.0.0.0 # listening address changed to listening on all systemctl start mongod.service

-- create multiple instances of mongodb

Create data file paths and log files

Mkdir-p / data/mongodb/mongodb {2J 3J 4} mkdir-p / data/mongodb/logstouch / data/mongodb/logs/mongodb2.logtouch / data/mongodb/logs/mongodb3.logtouch / data/mongodb/logs/mongodb4.logchmod 777 / data/mongodb/logs/*.log

Modify the configuration file of multi-instance

Vim / etc/mongod.confreplication: replSetName: yandada # replication set name is the same name cp-p / etc/mongod.conf / etc/mongod2.confcp-p / etc/mongod.conf / etc/mongod3.confcp-p / etc/mongod.conf / etc/mongod4.confvim / etc/mongod2.conf in multiple instances

Similarly: mongod3.conf & mongod4.conf is modified as shown above

Start the service

Mongod-f / etc/mongod.conf-- shutdownmongod-f / etc/mongod.confmongod-f / etc/mongod2.confmongod-f / etc/mongod3.confmongod-f / etc/mongod4.conf

-- configure replication sets-

[root@yandada3 mongodb2] # mongo > cfg= {"_ id": "yandada", "members": [{"_ id": 0, "host": "192.168.218.149cfg= 27017"}, {"_ id": 1, "host": "192.168.218.149cfg= 27018"}, {"_ id": 2 "host": "192.168.218.149 cfg 27019"}} > rs.initiate (cfg) # initialize database yandada:PRIMARY > rs.status () # View replication set status

Note:

If there is content in the database before initializing the database, it will be lost

-- add and delete nodes--

Yandada:PRIMARY > rs.add ("192.168.218.149yandada:PRIMARY 27020") # add nodes yandada:PRIMARY > rs.remove ("192.168.218.149frog 27020") # Delete nodes

-- master server analog switch--

Analog automatic switching

Mongod-f / etc/mongod.conf-shutdownmongo-port 27018rs.status ()

"_ id": 1

"name": "192.168.218.149purl 27018"

"health": 1

"state": 1

"stateStr": "PRIMARY", # 27018 elected as the master server

Simulate manual switching

Yandada:PRIMARY > rs.freeze (30) 30s do not participate in the election yandada:PRIMARY > rs.stepdown (60pm 30) 60s maintain slave server identity, 30s synchronize yandada:SECONDARY >

Tags: Data node service instance server switch file configuration database lab monitor content copy name address synchronization election same security advantage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Shulou Information Huawei NVidia