Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB master-slave replication set building

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. introduction to Mongodb replication sets

What is a replication set?

Replication sets (Replica Sets) are additional copies of data, the process of synchronizing data across multiple servers, providing redundancy and increasing data availability, and allowing recovery from hardware failures and disrupted services.

MongoDB replication set principle:

The replication set of mongodb requires at least two nodes. One of them is the master node, which is responsible for handling client requests, and the rest are slave nodes, which are responsible for replicating the data on the master node.

The common collocation of each node of mongodb is: one master, one slave, one master and multiple slaves.

The master node records all operations on it oplog, and the slave node periodically polls the master node for these operations, and then performs these operations on its own data copy, thus ensuring that the data of the slave node is consistent with that of the master node.

The MongoDB replication structure diagram is as follows:

In the above structure diagram, the client reads data from the master node, and when the client writes data to the master node, the master node interacts with the slave node to ensure the consistency of the data.

Advantages of replication sets:

Make data more secure

Engage in data availability

Disaster recovery

No downtime maintenance (such as backup, re-indexing, failover)

Read zoom (additional copy read)

The replica set is transparent to the application

Characteristics of replication sets:

N clusters of what time

Any node can be used as the primary node

All writes are on the primary node

Automatic failover

Automatic recovery

Master-slave switch:

1. Manual switch: actively abandon the primary, and the rest of the slave server will elect the master server

two。 Automatic switching: turn off the MongoDB service, and the rest of the slave server will elect the master server.

2. Experimental environment:

1. Install MongoDB link: Centos 7 ultra-simple yum source installation MongoDB

two。 Because it is an experiment, configure the replication set, IP:192.168.233.6, on the MongoDB server of a Centos 7 virtual machine

3. Four instances are enabled to add configuration, and the port numbers are:

1) 27017

2) 27018

3) 27019

4) 27020

4. Experimental operation: 4 instances open 3 1 master 2 add additional instance 27020 node cancel instance 27020 node from additional replication set

III. Operation steps

1. After installing MongoDB, the default port number is 27017

Add 3 additional instances

First, you need to create three data storage directories and three log files

Mkdir-p / data/mongodb/mongodb {2J 3J 4} # create 3 data file directories

Mkdir-p / data/mongodb/logs # create log file directory

Touch / data/mongodb/logs/mongodb {2Jet 3Jet 4} .log # create 3 log files

Chmod 777 / data/mongodb/logs/*.log # modify log file permissions, can write and read, maximum permissions

two。 Modify the configuration file of the default MongoDB for port 27017 to enable replication set

Vim / etc/mongod.conf

3. MongoDB needs to be closed and reopened because the configuration file has been modified

Mongod-f / etc/mongod.conf-- shutdown # closes MongoDB

Mongod-f / etc/mongod.conf # enable MongoDB

4. Copy and generate 3 node configuration files for modification

[root@localhost logs] # cp-p / etc/mongod.conf / etc/mongod2.conf

[root@localhost logs] # cp-p / etc/mongod.conf / etc/mongod3.conf

[root@localhost logs] # cp-p / etc/mongod.conf / etc/mongod4.conf

5. The configuration file of each node needs to be modified

[root@localhost logs] # vim / etc/mongod2.conf

# vim / etc/mongod2.conf

.

Path: / data/mongodb/logs/mongodb2.log / / each instance specifies its own log directory

.

DbPath: / data/mongodb/mongodb2 / / each instance specifies its own data directory

Port: 27018 / / designated ports are 27018, 27019 and 27020 respectively

.

Replication: / / enable replication parameters for all instances: define a replSetName: yang

ReplSetName: yang

[root@localhost logs] # vim / etc/mongod3.conf

# vim / etc/mongod3.conf

.

Path: / data/mongodb/logs/mongodb3.log / / each instance specifies its own log directory

.

DbPath: / data/mongodb/mongodb3 / / each instance specifies its own data directory

Port: 27019 / / designated ports are 27018, 27019 and 27020 respectively

.

Replication: / / enable replication parameters for all instances: define a replSetName: yang

ReplSetName: yang

[root@localhost logs] # vim / etc/mongod4.conf

# vim / etc/mongod4.conf

.

Path: / data/mongodb/logs/mongodb4.log / / each instance specifies its own log directory

.

DbPath: / data/mongodb/mongodb4 / / each instance specifies its own data directory

Port: 27020 / / designated ports are 27018, 27019 and 27020 respectively

.

Replication: / / enable replication parameters for all instances: define a replSetName: yang

ReplSetName: yang

6. After the mongo2 mongo3 mongo4 is modified in the above steps, enable the service.

[root@localhost logs] # mongod-f / etc/mongod2.conf

[root@localhost logs] # mongod-f / etc/mongod3.conf

[root@localhost logs] # mongod-f / etc/mongod4.conf

# check the MongoDB enabled of the next four ports

[root@localhost logs] # netstat-natp | grep mongo

7. Configure a replication set with three nodes

# enter the default MongoDB database first

Mongo

# create three node replication sets

Cfg= {"_ id": "yang", "members": [{"_ id": 0, "host": "192.168.233.6 yang 27017"}, {"_ id": 1, "host": "192.168.233.6 purge 27018"}, {"_ id": 2, "host": "192.168.233.6 yang 27019"}]}

# ensure that there is no data in the slave node when initializing the configuration

Rs.initiate (cfg)

Rs.status () # View the status of the replication set

Master node: Port 27017

Slave node: Port 27018

Slave node: Port 27019

# since only a replication set of three nodes was created before, now show me how to add a node with port 27020

# first, you need to enter the master node PRIMARY 27017

# add a node to operate primary on the primary node

Rs.add ("192.168.233.6 purl 27020")

Rs.status () # View the status of the replication set

# now that you can add nodes, you can delete nodes:

# Delete a node to operate primary on the primary node

Rs.remove ("192.168.233.6 purl 27020")

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 250

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report