mongoDB officials no longer recommend using master-slave mode. Instead, the replica set mode is adopted. click For example,
So what is a replica set? World of Warcraft always talks about duplication, but in fact these two concepts have almost the same meaning. Duplicate in the game refers to players concentrating on a scene during rush hours to fight strange, there will be more players and fewer monsters. In order to ensure the player's experience, game developers will open a single batch of players with the same number of monsters in the same space. This duplicate scene is a copy, no matter how many players play in their own duplicates or not. It will affect each other. This is also true of the mongoDB replica. The master-slave mode is actually a single-copy application, which does not have good scalability and fault tolerance. The replica set has multiple replicas to ensure fault-tolerance. Even if a replica is suspended, there are many replicas, and the first problem above is solved: "The primary node is suspended, and the whole cluster will switch automatically". No wonder mongoDB officially recommends this model.
Let's look at the architecture of the mongoDB replica set:
You can see from the graph that the client connects to the entire replica set, regardless of which machine is dead or not. The master server is responsible for reading and writing the entire replica set. The replica set periodically synchronizes data backup. Once the master node is suspended, the replica node will elect a new master server, which does not need to be concerned about the application server. Let's take a look at the architecture after the primary server hangs up:
After the duplicate node in the replica set is detected by heartbeat mechanism after the primary node is suspended, the election mechanism of the primary node will be initiated in the cluster, and a new primary server will be automatically elected. It looks like a cow X. Let's deploy it quickly.
The number of machines in the official recommended replica set is at least three, so we also configure the test according to this number.
Mongodb replica set environment deployment record
System environment
Centos 7.5, MongoDB 4.0.6, shutting down firewalls, cluster using different communication ports
1) Machine environment
10.153.1.183 master-node (master node)
10.153.1.184 Slave-node 1 (slave node)
10.153.1.185 Slave-node 2 (slave node)
2) Install master-node
#!/bin/bash ####################### #Introduction to mongodb #mongodb is a non-relational database, but its operation is most similar to relational data. mysql is a relational database #mongodb is a non-relational database for document storage, and data is stored in json format #mongodb can be used for permanent storage or for caching data #mongodb provides replica set and fragmented cluster functions, which are easy to operate. ############################# if [ `whoami` != root ] then echo "Please login as root to continue :)" exit 1 fi if [ ! -d /home/tools/ ];then mkdir -p /home/tools else rm -rf /home/tools && mkdir -p /home/tools fi #Prohibit memory giant pages echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag #Add commands to /etc/rc.local chmod +x /etc/rc.d/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.local #Disable firewall and selinux sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config systemctl disable firewalld.service #Setting Handles Number and Process cat >> /etc/security/limits.conf << EOF * soft nofile 204800 * hard nofile 204800 * soft nproc 204800 * hard nproc 204800 EOF sed -i 's/4096/204800/g' /etc/security/limits.d/20-nproc.conf #download mongodb on centos 7 cd /home/tools && wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz #install mongodb tar zxvf mongodb-linux-x86_64-rhel70-4.0.6.tgz mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 ln -s /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb #Create data directory mkdir -p /data/mongodb/27017/ cat > /data/mongodb/27017/mongodb.conf <<EOF systemLog: destination: file logAppend: true path: /data/mongodb/27017/mongodb.log storage: dbPath: /data/mongodb/27017/ journal: enabled: true processManagement: fork: true net: port: 27017 bindIp: 0.0.0.0 maxIncomingConnections: 40000 replication: replSetName: oriente oplogSizeMB: 1024 security: authorization: enabled keyFile: /home/mongodb/keyfile EOF #Add mongodb users and setting permission groupadd -g 800 mongodb && useradd -u 800 -g mongodb mongodb chown -R mongodb.mongodb /data/mongodb/ /usr/local/mongodb/ #Create keyfile cat >/home/mongodb/keyfile <<EOF raQvX0ESjiZD/LaB4QmGpm/EJUfhea/r9CcGMHA/c46fNezLrIHLpSFlVb3BD7mt sZY4w4qNuV7mL/6qxVEktSyRu1yvdZG49ImJBH8ssUeCLBBHtfAaayH5 EOF chmod 600 /home/mongodb/keyfile && chown -R mongodb.mongodb /home/mongodb/keyfile #Add autoStart script cat >/etc/init.d/mongodb <<EOF #!/bin/bash # Description:mongodb ORS SERVER # chkconfig: - 85 15 # Written by jerry MONGODB_EXEC="/usr/local/mongodb/bin/mongod" MONGODB_DATA="/data/mongodb/27017/" MONGODB_CONF="/data/mongodb/27017/mongodb.conf" PORT=\$(netstat -tunlp|grep 27017|awk '{print \$4}'|cut -d ':' -f2) MONGODB_USER=mongodb case \$1 in start) echo -n "Starting mongodb..." if [[ \$PORT = 27017 ]];then echo "mongodb is alreday running!" else /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC -f \$MONGODB_CONF" fi echo " done" ;; stop) echo -n "Stoping mongodb..." /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC --shutdown --dbpath \$MONGODB_DATA" echo " done" ;; restart) \$0 stop \$0 start ;; status) if [[ \$PORT != 27017 ]];then echo "mongodb is not running!" else echo "mongodb is running!" fi ;; *) echo "Usage: \$0" exit 1 esac EOF #Setting environment variables cat >/etc/profile.d/mongodb.sh<<EOF export MONGODB_HOME=/usr/local/mongodb export PATH=\$PATH:\$MONGODB_HOME/bin EOF source /etc/profile.d/mongodb.sh #Add permission to /etc/init.d/mongodb chmod +x /etc/init.d/mongodb #Add to chkconfig service chkconfig --add mongodb #Setting up MongoDB auto-start chkconfig mongodb on #Start MongoDB service mongodb start
3) Install Slave-node 1
#!/bin/bash ############################# if [ `whoami` != root ] then echo "Please login as root to continue :)" exit 1 fi if [ ! -d /home/tools/ ];then mkdir -p /home/tools else rm -rf /home/tools && mkdir -p /home/tools fi #Prohibit memory giant pages echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag #Add commands to /etc/rc.local chmod +x /etc/rc.d/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.local #Disable firewall and selinux sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config systemctl disable firewalld.service #Setting Handles Number and Process cat >> /etc/security/limits.conf << EOF * soft nofile 204800 * hard nofile 204800 * soft nproc 204800 * hard nproc 204800 EOF sed -i 's/4096/204800/g' /etc/security/limits.d/20-nproc.conf #download mongodb on centos 7 cd /home/tools && wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz #install mongodb tar zxvf mongodb-linux-x86_64-rhel70-4.0.6.tgz mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 ln -s /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb #Create data directory mkdir -p /data/mongodb/27018/ cat > /data/mongodb/27018/mongodb.conf <<EOF systemLog: destination: file logAppend: true path: /data/mongodb/27018/mongodb.log storage: dbPath: /data/mongodb/27018/ journal: enabled: true processManagement: fork: true net: port: 27018 bindIp: 0.0.0.0 maxIncomingConnections: 40000 replication: replSetName: oriente oplogSizeMB: 1024 security: authorization: enabled keyFile: /home/mongodb/keyfile EOF #Add mongodb users and setting permission groupadd -g 800 mongodb && useradd -u 800 -g mongodb mongodb chown -R mongodb.mongodb /data/mongodb/ /usr/local/mongodb/ #Create keyfile cat >/home/mongodb/keyfile <<EOF raQvX0ESjiZD/LaB4QmGpm/EJUfhea/r9CcGMHA/c46fNezLrIHLpSFlVb3BD7mt sZY4w4qNuV7mL/6qxVEktSyRu1yvdZG49ImJBH8ssUeCLBBHtfAaayH5 EOF chmod 600 /home/mongodb/keyfile && chown -R mongodb.mongodb /home/mongodb/keyfile #Add autoStart script cat >/etc/init.d/mongodb <<EOF #!/bin/bash # Description:mongodb ORS SERVER # chkconfig: - 85 15 # Written by jerry MONGODB_EXEC="/usr/local/mongodb/bin/mongod" MONGODB_DATA="/data/mongodb/27018/" MONGODB_CONF="/data/mongodb/27018/mongodb.conf" PORT=\$(netstat -tunlp|grep 27018|awk '{print \$4}'|cut -d ':' -f2) MONGODB_USER=mongodb case \$1 in start) echo -n "Starting mongodb..." if [[ \$PORT = 27018 ]];then echo "mongodb is alreday running!" else /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC -f \$MONGODB_CONF" fi echo " done" ;; stop) echo -n "Stoping mongodb..." /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC --shutdown --dbpath \$MONGODB_DATA" echo " done" ;; restart) \$0 stop \$0 start ;; status) if [[ \$PORT != 27018 ]];then echo "mongodb is not running!" else echo "mongodb is running!" fi ;; *) echo "Usage: \$0" exit 1 esac EOF #Setting environment variables cat >/etc/profile.d/mongodb.sh<<EOF export MONGODB_HOME=/usr/local/mongodb export PATH=\$PATH:\$MONGODB_HOME/bin EOF source /etc/profile.d/mongodb.sh #Add permission to /etc/init.d/mongodb chmod +x /etc/init.d/mongodb #Add to chkconfig service chkconfig --add mongodb #Setting up MongoDB auto-start chkconfig mongodb on #Start MongoDB service mongodb start
4) Install Slave-node 2
#!/bin/bash ############################# if [ `whoami` != root ] then echo "Please login as root to continue :)" exit 1 fi if [ ! -d /home/tools/ ];then mkdir -p /home/tools else rm -rf /home/tools && mkdir -p /home/tools fi #Prohibit memory giant pages echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag #Add commands to /etc/rc.local chmod +x /etc/rc.d/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local echo "echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.local #Disable firewall and selinux sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config systemctl disable firewalld.service #Setting Handles Number and Process cat >> /etc/security/limits.conf << EOF * soft nofile 204800 * hard nofile 204800 * soft nproc 204800 * hard nproc 204800 EOF sed -i 's/4096/204800/g' /etc/security/limits.d/20-nproc.conf #download mongodb on centos 7 cd /home/tools && wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz #install mongodb tar zxvf mongodb-linux-x86_64-rhel70-4.0.6.tgz mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 ln -s /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb #Create data directory mkdir -p /data/mongodb/27019/ cat > /data/mongodb/27019/mongodb.conf <<EOF systemLog: destination: file logAppend: true path: /data/mongodb/27019/mongodb.log storage: dbPath: /data/mongodb/27019/ journal: enabled: true processManagement: fork: true net: port: 27019 bindIp: 0.0.0.0 maxIncomingConnections: 40000 replication: replSetName: oriente oplogSizeMB: 1024 security: authorization: enabled keyFile: /home/mongodb/keyfile EOF #Add mongodb users and setting permission groupadd -g 800 mongodb && useradd -u 800 -g mongodb mongodb chown -R mongodb.mongodb /data/mongodb/ /usr/local/mongodb/ #Create keyfile cat >/home/mongodb/keyfile <<EOF raQvX0ESjiZD/LaB4QmGpm/EJUfhea/r9CcGMHA/c46fNezLrIHLpSFlVb3BD7mt sZY4w4qNuV7mL/6qxVEktSyRu1yvdZG49ImJBH8ssUeCLBBHtfAaayH5 EOF chmod 600 /home/mongodb/keyfile && chown -R mongodb.mongodb /home/mongodb/keyfile #Add autoStart script cat >/etc/init.d/mongodb <<EOF #!/bin/bash # Description:mongodb ORS SERVER # chkconfig: - 85 15 # Written by jerry MONGODB_EXEC="/usr/local/mongodb/bin/mongod" MONGODB_DATA="/data/mongodb/27019/" MONGODB_CONF="/data/mongodb/27019/mongodb.conf" PORT=\$(netstat -tunlp|grep 27019|awk '{print \$4}'|cut -d ':' -f2) MONGODB_USER=mongodb case \$1 in start) echo -n "Starting mongodb..." if [[ \$PORT = 27019 ]];then echo "mongodb is alreday running!" else /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC -f \$MONGODB_CONF" fi echo " done" ;; stop) echo -n "Stoping mongodb..." /bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC --shutdown --dbpath \$MONGODB_DATA" echo " done" ;; restart) \$0 stop \$0 start ;; status) if [[ \$PORT != 27019 ]];then echo "mongodb is not running!" else echo "mongodb is running!" fi ;; *) echo "Usage: \$0" exit 1 esac EOF #Setting environment variables cat >/etc/profile.d/mongodb.sh<<EOF export MONGODB_HOME=/usr/local/mongodb export PATH=\$PATH:\$MONGODB_HOME/bin EOF source /etc/profile.d/mongodb.sh #Add permission to /etc/init.d/mongodb chmod +x /etc/init.d/mongodb #Add to chkconfig service chkconfig --add mongodb #Setting up MongoDB auto-start chkconfig mongodb on #Start MongoDB service mongodb start
5) Log on to master-node
mongo
6) Initialization of mongodb replica set and its status viewing
config = { _id:"oriente", members:[ {_id:0,host:"10.153.1.183:27017"}, {_id:1,host:"10.153.1.184:27018"}, {_id:2,host:"10.153.1.185:27019"}] }
The screenshots are as follows
use admin
Initialization of replica sets takes some time
rs.initiate( config )
Copy Set Status, One primary, Other SECONDARY
rs.status()
Create admin users and set passwords
db.createUser({user:"admin",pwd:"oriente1234.com",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Setting Related Permissions
use admin db.auth("admin","oriente1234.com") db.grantRolesToUser( "admin" , [ { role: "dbOwner", db: "admin" },{ "role": "clusterAdmin", "db": "admin" }, { "role": "userAdminAnyDatabase", "db": "admin" }, { "role": "dbAdminAnyDatabase", "db": "admin" }])
7) Any query from the database, here is node-slave1(10.153.1.184)
mongo 10.153.1.184:27018
use admin db.auth(admin,'oriente1234.com') rs.status()