In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB
Master-Slave
MongoDB supports asynchronous data replication between different services for failover and redundancy.
Only one service node (primary or master) supports writes at a time.
MongoDB supports two modes of replication:
Master/Slave, master-slave replication, roles include master and slave. Replication Set, role includes primary and secondary.
Official address of Master/Slave:
http://www.mongodb.org/display/DOCS/Master+Slave
The official address of Replica Set:
http://www.mongodb.org/display/DOCS/Replica+Sets
Today's actual combat is replica set, which is replication set replication.
Replica sets enable automatic failover and automatic recovery. A replica set consists of two or more nodes that replicate each other. The replica set automatically selects primary nodes, none of which are fixed primary. Mongos automatically detects changes to the primary node of a replica set and sends writes to the new primary node.
Usually used in the following scenarios
Data redundancy. Automatic failover provides high availability services. Spread the load of reading. Simplified maintenance (as opposed to master-slave). Disaster recovery.
First, start mongod. There are two parameters for replica set:
--replSet, the name of the replication set.
--oplogSize, the size of the operation log in MB.
This time we configure replica set under ubuntu, starting with two mongod nodes.
mongod --dbpath /home/andyshi/mongo1/ --logpath /home/andyshi/mongo1/log.log --replSet shard1 --port 10001 --bind_ip 192.168.0.21mongod --dbpath /home/andyshi/mongo2/ --logpath /home/andyshi/mongo2/log.log --replSet shard1 --port 10002 --bind_ip 192.168.0.21
You'll notice that the two mongod startup parameters replSet above specify the same value shard1, meaning that both mongod nodes are in the same replica set.
Initializing a Replica Set
Light started two mongod nodes, but can not provide any services, at this time you use mongo connection, db.book.insert will prompt you no master, that is, there is no primary node, so he does not know which node to write data to.
After starting the two mongod nodes, initialization is required.
First connect to any mongod node with mongo, and then execute the following command:
cfg={_id:'shard1',members:[{_id:0,host:'192.168.0.21:10001'},{_id:1,host:'192.168.0.21:10002'}]}rs.initiate(cfg)
The following prompt message appears on behalf of success, if not successful, you can google the error prompt, you will find a lot of answers.
{"info" : "Config now saved locally. Should come online in about a minute. ","ok" : 1}
continue to implement
rs.status()
You can view the status of the replica set, including name, time, whether the currently logged in mongod is primary or secondary, and member information.
Among the information in the replica set, the important ones are:
The value of myState, if 1 means the current login is primary; if 2 means the current login is secondary.
Member information includes address, health status, primary or secondary, etc.
The most important information about members is
state: 1 indicates that the host can read and write at present, 2: cannot read and write health: 1 indicates that the host is normal at present, 0: abnormal
At this point, log in to the mongod of primary and insert a piece of data.
//Assuming that 10001 is primary, you can get mongo 192.168.0.21:10001use testdb.book.insert({'title':'computer'}) by querying rs.status.
Then look at the secondary log file and see that replication has occurred.
At this time log in secondary, use test, db.book.find(), you can report an error.
error: { "$err" : "not master and slaveok=false", "code" : 13435 }
There is no relationship, reading data in the secondary still requires us to do the final step, which is performed on the secondary where data needs to be read.
rs.slaveOK()
At this time db.book.find() again, the normal display results, no problem.
add nodes
Start a new mongod node
mongod --dbpath /home/andyshi/mongo3/ --logpath /home/andyshi/mongo3/log.log --replSet shard1 --port 10003 --bind_ip 192.168.0.21
Connect the primary node and execute the following command:
rs.add ('192.168.0.21:10003') rs.addArb ('192.168.0.21:10003')//reconfigure rs.reconfig(rs.conf())
Force a node to be primary
After mongodb 2.0 you can use the following
Compared with master-slave, the advantage of replica set is that there is no single point of failure. After primary failure, the whole replica set will automatically select a healthy node to become primary and undertake the task of writing. The availability is higher than master-slave, providing higher availability.
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.