Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb replica set

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In fact, mongodb can also act as a master-slave mechanism. Mongodb one master and one slave is similar to mysql, but slave is read-only in the architecture, and the master cannot be switched automatically after the host goes down. Now the official use is no longer recommended, use the copy set instead.

In this mode there is one master (primary) and multiple slaves (secondary), read-only. It is supported to set the weight for them. When the master is down, the slave with the highest weight will switch to the master.

An arbiter role can also be established in this architecture, which is only responsible for adjudication and does not store data. In this architecture, both read and write data are on the master. In order to achieve the purpose of load balancing, you need to manually specify the target server of the read library.

1.MongoDB replica set building

Three machines: 192.168.1.100 (primary) 192.168.1.101 (secondary) 192.168.1.102 (secondary)

Edit the configuration file / etc/mongod.conf for the three machines and add:

#

Replication:

# # oplog size

OplogSizeMB: 20

# # replication set name

ReplSetName: monreplset / / name is optional

#

Restart three machines separately

Connect the mongo on the master

> use admin

> config= {_ id: "monreplset", members: [{_ id:0,host: "192.168.1.100 monreplset 27017"}, {_ id:1,host: "192.168.1.101Freight 27017"}, {_ id:2,host: "192.168.1.102Freight 27017"}]}

> rs.initiate (config)

> rs.add ("192.168.1.101")

> rs.add ("192.168.1.102")

Rs.status () / / View status

If the status of the two slaves is "stateStr": "STARTUP", you need to do the following

> var config= {_ id: "monreplset", members: [{_ id:0,host: "192.168.1.100 monreplset 27017"}, {_ id:1,host: "192.168.1.101Freight 27017"}, {_ id:2,host: "192.168.1.102Freight 27017"}]}

> rs.reconfig (config)

If you look at rs.status () again at this time, you will find that it has changed from the state of to SECONDARY.

2.MongoDB replica set test

Build libraries and collections on the Lord

> use mydb

> db.test.insert ({ID:1,UserName: "123", password:" 123456 "})

> show dbs

View from above

> show dbs

If there is an error Error:listDatabasesfailed: {"note": "from execCommand", "ok": 0, "errmsg": "not master"}

Execute > rs.slaveok ()

3.MongoDB copy set change weight

By default, the weights of the three machines are all 1. If any one of them is set to a higher weight than the others, the machine will immediately switch to the primary role, so we preset the weights of the three machines as 100,101, and 102, respectively.

Execute on the Lord

Cfg= rs.config ()

Cfg.members [0] .priority = 3

Cfg.members [1] .priority = 2

Cfg.members [2] .priority = 1

Rs.reconfig (cfg)

In that case, the second node will be the candidate primary node.

Execute iptables-I INPUT-p tcp--dport 27017-j DROP on the host to disable access to port 27017

At this point, 101 will become the main primary.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report