MongoDB replica set is a master-slave replication mode with failover cluster, any member may be master
,
When Master hangs up, it will quickly re-elect a node to act as master.
Constituent primary members of a replication set
- Primary
数据读写 master节点
- Secondary
备份Primary的数据 默认设置下 不可读 不可写
- Arbiter
投票节点 此节点不会存数据 只参与投票 ,当primary节点出现异常挂掉之后 arbiter节点负责从secondary 节点中选举一个节点升级为Primary节点
which can be set secondary node readable, so that the primary node is responsible for writing, which enables an efficient and simple read-write separation.
Environment construction
3 examples to illustrate the construction process of the replication cluster are: 127.0.0.1:12345,127.0.0.1:12346,127.0.0.1:12347.
You need to pre-create the folders in each directory before you start the error, there is a pit is the Pidfilepath configuration item must be an absolute path,
Otherwise, the Replset will also have a consistent name in the same replication set.
Note In the old version is the use of the master slave mode is currently used by the 3.4 official does not support the use of this way,
Officials want to use Replset instead of Master slave. So when you configure Master or slave, you will get an error.
127.0.0.1:12345 Configuration
Port=12345fork=truedbpath=data/12345logpath=log/12345/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12345/12345.pidreplset=mydbcenteroplogsize =512 127.0.0.1:12346 Configuration
Port=12346fork=truedbpath=data/12346logpath=log/12346/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12346/12346.pidreplset=mydbcenteroplogsize =512 |
127.0.0.1:12347 Configuration
Port=12347fork=truedbpath=data/12347logpath=log/12347/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12347/12347.pidreplset=mydbcenteroplogsize =512 |
After starting each of the 3 instances, go to an instance
Initializing a replication cluster first create 1 configuration objects in JS is a simple object, JSON string
- var rs_conf={
- " _id": "Mydbcenter",
- "Members": [
- {
- "_id": 0,
- "host": "127.0.0.1:12345"
- },
- {
- "_id": 1,
- "host": "127.0.0.1:12346"
- },
- {
- "_id": 2,
- "host": "127.0.0.1:12347"
- }
- ]
- }
Apply the configuration to the cluster
rs.initiate(rs_conf)
One limitation here is that the nodes in the cluster need not have the data to be emptied first or initiate
the error will occur.
After a successful configuration rs.status()
, use the command to view the status of each node, some normal can see the status information of each node
rs.status()
After the replication cluster is set up, the command line identifier becomes the appropriate member type, as
- mydbcenter:P rimary>
- mydbcenter:secondary>
This is also a small sign of success in verifying that the cluster is built.
You can then try writing a piece of data in the primary. This data is immediately synchronized to each secondary node.
Of course, it is said that the default secondary is not readable and will report the following error.
- {
- "OK": 0,
- "errmsg": "not Master and Slaveok=false",
- "code": 13435,
- " codename": "Notmasternoslaveok "
- }
So we need to do it in secondary.
db.getMongo().setSlaveOk()
There are a lot of postings on the web saying that just perform db.getmongo () in the primary node. Setslaveok ()
Can read the data in the secondary node, but it is not possible to try it in version 3.4. Need to be executed in secondary.
db.getMongo().setSlaveOk()
Let the secondary be readable.
Adding nodes
If you have a good one replication cluster now, the boss would like to add a backup machine to go in?
Just start the instance of the new machine and call the Rs.add () method in primary to
rs.add({"host" : "127.0.0.1:12348"})
Add a polling node to invoke the rs.addArb()
method.
Reprint to: Smile Pine Station http://blog.seoui.com/2017/01/11/mongodb-rs/
MongoDB Build Replset Replication cluster