In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB is used in production for data security and access stability. Copy sets are often used.
Since the last book, the MongoDB copy set has been installed many times. But every time to go to Baidu specific order details.
I had to organize my own notes and write them down for inquiry.
For download and installation of MongoDB, please refer to:
Https://blog.51cto.com/hsbxxl/2149500
1. What is a Replica Set- replica set?
A replica set is a cluster of mongoDB replicas.
The principle is that the write operation occurs in the master library, and the OpLog log of the master library is synchronized from the slave library.
There is no specific master database in the cluster. The master database is elected. If the master database down is gone, another master database will be elected.
two。 Early versions of MongoDB use master-slave, and one master and one slave is similar to MySQL, but slave is read-only in this architecture. When the master library goes down, the slave library cannot automatically switch to the master library. At present, the master-slave mode has been eliminated and changed to a replica set, in which there is one master (primary) and multiple slaves (secondary), read-only. Support to set the weight to them, when the master is down, the slave with the highest weight will switch to the main. An arbiter role can also be established in this architecture, which is only responsible for adjudication and does not store data. In this architecture, the read and write data are all on the master. To achieve the purpose of load balancing, you need to manually specify the target server of the read library.
In short, the MongoDB replica set is a master-slave cluster with automatic failure recovery, which consists of one Primary node and one or more Secondary nodes. Similar to MySQL's MMM architecture. For more information on the copy collection, please see the official documentation:
Official document address:
Https://docs.mongodb.com/manual/replication/
3. The replica set has the following characteristics:
. The minimum composition is primary,secondary,arbiter, and the general deployment is primary,2 secondary.
. The number of members should be odd, and if it is even, adding arbiter,arbiter does not save the data, only vote.
. A maximum of 50 members, but only 7 voting members, and the rest is non-voting members.
Configure copy set
4. In this experiment, we configure a copy set of 1 master, 1 slave and 1 arbitration. Two physical machines
Host1 192.168.67.101:27017host2 192.168.67.102:27017host2 192.168.67.102:27018
5. Start the mongo process separately
Start primary on host1
Mongod-port 27017-dbpath / mongodbs/mgdata/-logpath / mongodbs/mgdata/db.log-replSet rs0-fork
Start secondary on host2
Mongod-port 27017-dbpath / mongodbs/mgdata/-logpath / mongodbs/mgdata/db.log-replSet rs0-fork
Start arb on host2
Mongod-port 28018-dbpath / mongodbs/arb/-logpath / mongodbs/arb/db.log-replSet rs0-fork
6. Configuration copy set, because there are only two physical machines, I hope that under normal circumstances, host1 machines can be used as more primary of mongo. So priority is set to 2
# mongo > cfg= {_ id: "rs0", members: [{_ id:0,host:'192.168.67.101:27017',priority:2}, {_ id:2,host:'192.168.67.102',priority:1}, {_ id:1,host:'192.168.67.102:28018',arbiterOnly:true}]}
7. Perform initialization node configuration
> rs.initiate (cfg)
8. View node configuration & information
> rs.conf () > rs.status ()
9. The specific command output is as follows
> cfg= {_ id: "rs0", members: [{_ id:0,host:'192.168.67.101:27017',priority:2}, {_ id:2,host:'192.168.67.102',priority:1}, {_ id:1,host:'192.168.67.102:28018',arbiterOnly:true}]} {"_ id": "rs0", "members": [{"_ id": 0 "host": "192.168.67.101id 27017", "priority": 2}, {"_ id": 2, "host": "192.168.67.102", "priority": 1}, {"_ id": 1, "host": "192.168.67.102host 28018", "arbiterOnly": true}]} > rs.initiate (cfg) {"ok": 1} rs0:OTHER > rs.conf () {"_ id": "rs0" "version": 1, "protocolVersion": NumberLong (1), "members": [{"_ id": 0, "host": "192.168.67.101 hidden 27017", "arbiterOnly": false, "buildIndexes": true, "hidden": false, "priority": 2, "tags": {}, "slaveDelay": NumberLong (0), "votes": 1}, {"_ id": 2 "host": "192.168.67.102 votes 27017", "arbiterOnly": false, "buildIndexes": true, "hidden": false, "priority": 1, "tags": {}, "slaveDelay": NumberLong (0), "votes": 1}, {"_ id": 1, "host": "192.168.67.102 false", "arbiterOnly": true, "buildIndexes": true, "hidden": false, "priority": 1 "tags": {}, "slaveDelay": NumberLong (0), "votes": 1}], "settings": {"chainingAllowed": true, "heartbeatIntervalMillis": 2000, "heartbeatTimeoutSecs": 10, "electionTimeoutMillis": 10000, "getLastErrorModes": {}, "getLastErrorDefaults": {"w": 1, "wtimeout": 0}} rs0:SECONDARY > rs.status () {"set": "rs0" "date": ISODate ("2018-07-24T06:59:34.195Z"), "myState": 2, "term": NumberLong (2), "syncingTo": "192.168.67.101 id 27017", "heartbeatIntervalMillis": NumberLong (2000), "members": [{"_ id": 0, "name": "192.168.67.101 24T06:59:34.195Z 27017", "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 60 "optime": {"ts": Timestamp (1532415537, 2), "t": NumberLong (2)}, "optimeDate": ISODate ("2018-07-24T06:58:57Z"), "lastHeartbeat": ISODate ("2018-07-24T06:59:32.964Z"), "lastHeartbeatRecv": ISODate ("2018-07-24T06:59:33.372Z"), "pingMs": NumberLong (0), "electionTime": Timestamp (0) "electionDate": ISODate ("1970-01-01T00:00:00Z"), "configVersion": 1}, {"_ id": 1, "name": "192.168.67.102 configVersion", "health": 1, "state": 7, "stateStr": "ARBITER", "uptime": 60, "lastHeartbeat": ISODate ("2018-07-24T06:59:32.957Z"), "lastHeartbeatRecv": ISODate ("2018-07-24T06:59:30.356Z") "pingMs": NumberLong (0), "configVersion": 1}, {"_ id": 2, "name": "192.168.67.102 configVersion 27017", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 615, "optime": {"ts": Timestamp (1532415537, 2), "t": NumberLong (2)}, "optimeDate": ISODate ("2018-07-24T06:58:57Z") "syncingTo": "192.168.67.101 self 27017", "configVersion": 1, "self": true}], "ok": 1}
More commands
10. Add slave command
Rs0:PRIMARY > rs.add ('host3:27017')
You can specify priority
Rs0:PRIMARY > rs.add ({host: "host3:27017", priority: 1})
11. Remove a slave library
First close the mongoDB of the slave library, and then remove the slave library on the master library
Rs0:PRIMARY > rs.remove ('host3:27019')
twelve。 Separation of reading and writing
Both the master library and the slave library support read operations. However, by default, reads are also read from the main library.
You can open the slave library to support read operations by setting ReadPreference. ReadPreference has several modes:
Primary # read from the master library. The default primaryPreferred # is basically read from the master library. When the master is unavailable, read secondary # read from the slave library # read from the slave library # basically read from the slave library, when never available, read from the master library # read from the library with the least network latency
Basically, what is commonly used is Primary,secondary,nearest
13. Members can be classified through Tag in the settings of the copy set, and the type of reading can be specified by the following methods:
. When the program connects, specify the type of ReadPreference to read
. Use the mongo command to connect, valid only for the current connection
Rs0:SECONDARY > db.getMongo (). SetReadPref ('secondaryPreferred')
14. Special member type
Secondary also has some special member types:
Priority 0 # cannot be promoted to primary level and can be used in multi-data center scenarios.
Hidden # is invisible to the client and is generally used for backup or statistical reporting
Delayed # data is later than the secondary set and is generally used as rolling backup or history snapshot
The configuration of the copy set is also relatively simple. If you have time later, share MongoDB Sharded Cluster again.
Reference link:
Https://www.cnblogs.com/ee900222/p/mongodb_2.html
Https://blog.51cto.com/zero01/2059033
Http://chenzhou123520.iteye.com/blog/1634676
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.