MongoDB Replica Set
A replica set (Replica sets) is an additional copy of the data that is the process of synchronizing data across multiple servers, providing redundant backups and increasing the availability of data, which can be used to recover hardware failures and interrupted services.
How MongoDB Replica sets work
- A copy set of MongoDB requires at least two nodes. One is the master node (Primary), which handles the client request and the rest is the slave node (secondary), which replicates the data on the master node.
- MongoDB each node common collocation method is: A master one from, a master many from. The master node records all operations on it into Oplog, obtains these operations from the node periodically polling the master node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
- The client writes data to the primary node, reads the data from the node, and the master node interacts with the data from the node to ensure data consistency. If one of the nodes fails, the other nodes will immediately connect to the business without downtime.
Features and benefits of MongoDB replication set
MongoDB Replica set deployment
We can create multiple instances on a single server for replication sets
MongoDB installation and creation of multi-instance details in the Post CentOS 7 installation MongoDB (latest version 4.0)
Configuring Replication Sets
-
- Create 4 instances of MongoDB
# Create an instance data directory
mkdir -p / data / mongodb / mongodb {1,2,3,4}
# Create an instance's log directory
mkdir -p / data / logs
# Create an instance log file
touch /data/logs/mongodb{1,2,3,4}.log
# Grant log file permissions
chmod 777 /data/logs/mongodb*.log
#Create instance configuration file storage location
mkdir -p / data / conf
-
- Create a configuration file, turn on the copy set feature
Here Bo Master stole a little lazy, the configuration file in the previously created/data/conf/directory, created after the startup script is the relative path of the experimental machine # vim /data/conf/mongodb1.conf // simple configuration is as follows,
dbpath = / data / mongodb / mongodb1
logpath = / data / logs / mongodb1.log
port = 27017
replSet = testrc #Configuration parameter value is testrc
logappend = true
fork = true
maxConns = 5000
storageEngine = mmapv1
Required note Three other instances of the data file in the configuration file location and log files and ports to change the
-
- scripting makes it easy to control 4 instances (using relative paths, depending on the actual modification)
[[email protected] conf]# cd /usr/local/mongodb/bin
[[email protected] bin]# vim mongodb
#!/bin/bash
INSTANCE=$1
ACTION=$2
case "$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
[[email protected] bin]# chmod +x mongodb
[[email protected] bin]# ./mongodb mongodb1 restart
[[email protected] bin]# ./mongodb mongodb2 restart
[[email protected] bin]# ./mongodb mongodb3 restart
[[email protected] bin]# ./mongodb mongodb4 restart
[[email protected] bin]# netstat -ntap | grep mongod
-
- Configuring a replication set of three nodes
4.1 Viewing the status information for a replica set mongo # (default port: 27017)
rs.status ()
4.2 Define the CFG initialization parameters (add three here, the rest of the Experiment Add node function)
> cfg={"_id":"testrc","members":[{"_id":0,"host":"192.168.125.119:27017"},{"_id":1,"host":"192.168.125.119:27018"},{"_id":2,"host":"192.168.125.119:27019"}]}
4.3 Start the replication set function, ensure that the slave node has no data when initializing the configuration, keep the master node data synchronized
> rs.initiate(cfg)
-
- Adding nodes
testrc:PRIMARY> rs.add("192.168.125.119:27020")
-
- Delete a node
testrc:PRIMARY> rs.remove("192.168.125.119:27020")
Deploy a MongoDB replication set (master-slave replication, read/write separation, high availability)