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

Example Analysis of replica set in MongoDB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of the copy set in MongoDB, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

The details are as follows:

Replication set replication set

Replication set, multiple servers maintain the same copy of data, improving the availability of the server.

MongoDB replication is the process of synchronizing data on multiple servers.

Replication provides redundant backup of data, and stores copies of data on multiple servers, which improves the availability of data and ensures the security of data.

Replication also allows you to recover data from hardware failures and service disruptions.

Setup process:

(1) create an example

Suppose you create three sets, and create three instance directories and log directories:

Mkdir / home/m17 / home/m18 / home/m19 / home/mlog

Start three examples with ports 27017, 27018, and 27019, respectively.

/ mongod-- dbpath=/home/m17-- logpath=/home/mlog/m17.log-- fork-- port=27017-- replSet=rs2-- smallfiles./mongod-- dbpath=/home/m18-- logpath=/home/mlog/m18.log-- fork-- port=27018-- replSet=rs2-- smallfiles./mongod-- dbpath=/home/m19-- logpath=/home/mlog/m19.log-- fork-port=27019-- replSet=rs2-- smallfiles

Description:

Parameter-the replSet setting is the same to belong to the same replication set

The parameter-smallfiles can save space and improve speed.

Then use ps aux | grep mongo to view the three ports that start up.

(2) configuration

Configure using the client connection mongo:

[test@localhost bin] $. / mongo

To manage the configuration, switch to admin:

> use admin

(configuration is in json format)

Var rsconf = {_ id:'rs2',members: [{"_ id": 0Participated hostlace 192.168.8.172 id 27017'}, {_ id:1,host:'192.168.8.172:27018'}, {_ id:2,host:'192.168.8.172:27019'}]}

If ip is not configured, use 127.0.0.1

Var rsconf = {_ id:'rs2',members: [{_ id:0,host:'127.0.0.1:27017'}, {_ id:1,host:'127.0.0.1:27018'}, {_ id:2,host:'127.0.0.1:27019'}]}

After execution, use printjson (rsconf) to view the configuration just now.

Then perform initialization:

> rs.initiate (rsconf); > rs.initiate (rsconf); {"ok": 1, "operationTime": Timestamp (1539933041, 1), "$clusterTime": {"clusterTime": Timestamp (1539933041, 1), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}} rs2:SECONDARY >

View the node:

Rs.status ()

Delete a node:

Rs.remove ('127.0.0.1pur27019')

Add a node:

Rs.add ('127.0.0.1pur27019')

Switch nodes:

The default is on port 27017, that is, rs2:PRIMARY > status

Exit mongo client command mode

Switch to another port:

[test@localhost bin] $. / mongo-- port=27018

That is, switch to the rs2:SECONDARY > state.

Test:

On the main service, create libraries and collections

Rs2:PRIMARY > use studentswitched to db studentrs2:PRIMARY > db.user.insert ({uid:1,name:'zhang san'}) WriteResult ({"nInserted": 1}) rs2:PRIMARY > db.user.find (); {"_ id": ObjectId ("5bc9889f85a0986431fd2499"), "uid": 1, "name": "zhang san"}

Check from the service.

Show dbs

Then you see that there is an error. The specific error message is:

...

"errmsg": "not master and slaveOk=false"

...

Because slave does not allow read and write by default:

> rs.slaveOk ()

You can then see the libraries and collections created by the main server.

Similarly, 27019 needs to execute this command to automatically synchronize and read and write.

When the main server 27017 is down

The second 27018 automatically becomes the primary server master state.

But 27019 needs to execute rs.slaveOk () again to automatically synchronize reads and writes.

Shell script:

#! / bin/bashIP=127.0.0.1NA=rs2sudo mkdir-p / home/m17 / home/m18 / home/m19 / home/mlogsudo chmod-R 777 / home/m17 / home/m18 / home/m19 / home/mlog./mongod-dbpath=/home/m17-- logpath=/home/mlog/m17.log-- fork-- port=27017-- replSet=$ {NA}-- smallfiles./mongod-- dbpath=/home/m18-- logpath=/home/mlog/m18.log-- fork-- port=27018-- replSet=$ {NA }-smallfiles./mongod-dbpath=/home/m19-logpath=/home/mlog/m19.log-fork-port=27019-replSet=$ {NA}-smallfiles./mongo

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

Wechat

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

12
Report