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

Deploy MongoDB replication sets (master-slave replication, read-write separation, high availability)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

MongoDB replication set

Replication sets (Replica Sets) are additional copies of data, the process of synchronizing data across multiple servers. Replication sets provide redundant backups and improve data availability, and can be used to recover hardware failures and interrupted services.

How MongoDB replication sets work mongodb requires at least two nodes for the replication set. One of them is the master node (Primary), which is responsible for handling client requests, and the rest is the slave node (Secondary), which is responsible for replicating 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 the operations on it to the oplog, periodically polls the slave node to obtain these operations, and then performs these operations on its own data copy, so as to ensure that the data of the slave node is consistent with that of the master node. The client writes data in the master node and reads the data in the slave node. The master node interacts with the slave node to ensure the consistency of the data. If one of the nodes fails, the other nodes will immediately take over the business without downtime. Characteristics and advantages of MongoDB replication set

Characteristics of replication sets:

N-node cluster any node can act as the primary node automatically fail over replication sets on the primary node: make data more secure, high data availability (24x7) disaster recovery no downtime maintenance (such as backup, index rebuild, failover) read scaling (additional replica reads) deployment of MongoDB replication sets where replica sets are transparent to the application

We can create multiple instances on one server to do replication sets

MongoDB installation and creation of multiple instances details on installing MongoDB in CentOS 7 (the latest version 4.0)

Configure the replication set to create 4 MongoDB instances # create the instance data directory mkdir-p / data/mongodb/mongodb {1meme 2je 3dir 4} # create the instance log directory mkdir-p / data/logs # create the instance log file touch / data/logs/mongodb {1meme 2je 3pm 4} .log # Grant log file permissions chmod 777 / data/logs/mongodb*.log # create instance configuration file location mkdir-p / data/conf

Create a configuration file and turn on the replication set function

Here the blogger stole a laziness and stored the configuration file in the / data/conf/ directory created earlier. The startup script created later is the relative path of the experimental machine # vim / data/conf/mongodb1.conf / / easy configuration, and the configuration parameter value of dbpath=/data/mongodb/mongodb1 logpath=/data/logs/mongodb1.log port=27017 replSet=testrc # is testrc logappend=true fork=true maxConns=5000 storageEngine=mmapv1.

You need to note that the location of the data files in the configuration files of the other three instances and the log files and ports need to be changed.

It is easy to write a script to control 4 instances (relative path is used here, please modify it according to the actual) [root@CentOS7 conf] # cd / usr/local/mongodb/bin [root@CentOS7 bin] # vim mongodb #! / bin/bashINSTANCE=$1ACTION=$2case "$ACTION" in'start') / usr/local/mongodb/bin/mongod-f / data/conf/ "$INSTANCE" .conf;; 'stop') / usr/local/mongodb/bin/mongod-f / data/conf/ "$INSTANCE" .conf-- shutdown 'restart') / usr/local/mongodb/bin/mongod-f / data/conf/ "$INSTANCE" .conf-- shutdown/usr/local/mongodb/bin/mongod-f / data/conf/ "$INSTANCE" .conf;; esac [root@CentOS7 bin] # chmod + x mongodb [root@CentOS7 bin] #. / mongodb mongodb1 restart [root@CentOS7 bin] #. / mongodb mongodb2 restart [root@CentOS7 bin] #. / mongodb mongodb3 restart [root@CentOS7 bin] #. / mongodb mongodb4 restart [root@CentOS7 bin] # netstat-ntap | grep mongod

Configure a replication set with three nodes

4.1View the status information of replication set mongo # (default port: 27017) > rs.status ()

4.2 define cfg initialization parameters (three are added here, and node functions are added in the remaining experiment)

> cfg= {"_ id": "testrc", "members": [{"_ id": 0, "host": "192.168.125.119members 27017"}, {"_ id": 1, "host": "192.168.125.11919members 27018"}, {"_ id": 2, "host": "192.168.125.119members 27019"}]}

4.3 start the replication set function, initialize the configuration to ensure that the slave node has no data, and keep the master and slave node data synchronized

> rs.initiate (cfg)

Add node testrc:PRIMARY > rs.add ("192.168.125.119bank 27020")

Delete node testrc:PRIMARY > rs.remove ("192.168.125.119bank 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: 0

*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