In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to build MongoDB replica set, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Our previous cases are implemented on a single node, which is risky in a production environment. If the service is down, crashed or the hard disk is broken, it will cause losses to the company's business, so we need data backup. In MongoDB, we can achieve this requirement through replica set. MongoDB replica set (Replica Set) is a master-slave cluster with automatic failure recovery, which consists of a Primary node and one or more Secondary nodes. If the Primary crashes, it will automatically select one from the Secondary to upgrade it to a new master server.
Single server simulation
We must deploy multiple servers in the actual production environment, but in the process of learning, we can simulate this environment on one server, which can simplify our operation and let our friends get started quickly.
First we create the / data/db directory under the Linux root directory as our data storage directory, and then execute the following command to start a mongo shell:
Mongo-nodb
-nodb means that no database is connected at startup, and then create a replica set with the following command:
ReplicaSet=new ReplSetTest ({nodes:3})
In the created log, we can see the port numbers of the three instances, which are 20000, 20001 and 20002, respectively. At this time, our replica set is created, but it is not started. Then execute the following command to start the three mongodb instances:
ReplicaSet.startSet ()
Then execute the following command to configure the replication function:
ReplicaSet.initiate ()
In this way, the environment is basically ready. Do not close the current shell at this time. Let's reopen a Linux command window and execute the following command:
Mongo 192.168.248.128:20000/sang_1
Indicates the sang_1 database in the instance whose connection port is 20000. After the connection is successful, we can execute the following command to check the identity of the current instance, as follows:
Db.isMaster ()
A lot of data is returned, one of which is "ismaster": true, indicating that this is a master node. At this time, we open two more Linux windows, execute the following two commands, and enter the other two nodes:
Mongo 192.168.248.128:20001/sang_1mongo 192.168.248.128:20002/sang_1
After the connection is successful, you can still check the identity of the backup node through the db.isMaster () command. We find that "ismaster": false indicates that this is a backup node. At this time, we can do a simple test. At this time, I write a document on the primary node (port 20000). After writing, let's see if there is a copy of the document I have just written on other replica set members. The order of execution of the commands is as follows:
The primary node writes data:
Db.collect1.insert ({x: ""})
For any replica node, execute the following command to indicate that the data can be read from the backup node:
Db.setSlaveOk ()
Then execute the following command in the backup node to read the data:
Db.collect1.find ()
At this point, we found that the data had been backed up successfully.
If we try to write the document directly to the backup node at this time, we will find that the write failed. We need to note that the data in the backup node is all backed up and cannot be written directly, unless its identity is changed to the master node.
At this point, we try to shut down the primary node with the following command:
Use admindb.shutdownServer ()
Then look at the db.isMaster () of the two backup nodes and find that one backup node automatically ascends to become the primary node.
Finally, if you want to close the replica set, you can go back to the first shell command line and enter the following command:
ReplicaSet.stopSet () multiple server simulation
OK, the above operation is to build a replica set on a single server, which is convenient for us to do experiments. In a production environment, we may have multiple servers. How to build a replica set for multiple servers? Ladies and gentlemen, keep looking down.
First, prepare three servers with MongoDB installed at the following addresses:
192.168.248.128192.168.248.135192.168.248.136
Modify the configuration file mongodb.conf of each server, and add replSet=rs to indicate the name of the replica set. The modified configuration file is as follows:
Dbpath=/opt/mongodb/dblogpath=/opt/mongodb/logs/mongodb.logport=27017fork=truereplSet=rs
After the modification is completed, start the MongoDB on each of the three servers. After starting successfully, connect to any shell. After the connection is successful, define the configuration file as follows:
Config= {_ id: "rs", members: [{_ id:0,host: "192.168.248.128config= 27017"}, {_ id:1,host: "192.168.248.135VR 27017"}, {_ id:2,host: "192.168.248.136rs 27017"}]}
Id is followed by the name of the replica set, which is the name we defined in mongodb.conf, and the last three are members of the replica set. After the replica set is defined, execute the following command to initialize the replica set:
Rs.initiate (config)
After the initialization is successful, we can check the status of the replica set through rs.status (), and we can also see the role of each server. Some of the log contents are as follows:
{"members": [{"_ id": 0, "name": "192.168.248.128members 27017", "health": 1, "state": 1, "stateStr": "PRIMARY",}, {"_ id": 1, "name": "192.168.248.135name", "health": 1 "state": 2, "stateStr": "SECONDARY", "syncingTo": "192.168.248.128 id 27017"}, {"_ id": 2, "name": "192.168.248.136 syncingTo", "health": 1, "state": 2, "stateStr": "SECONDARY" "syncingTo": "192.168.248.128virtual 27017",}]}
We can see the role of each server, including primary, and secondary,secondary also indicates which server to synchronize data from. After all this work is done, we can test the replica set here in the way described above, and I won't repeat the testing work.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.