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

How to read and write massive data efficiently by MongoDB

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how MongoDB reads and writes massive data efficiently". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "MongoDB how to efficiently read and write massive data" can help you solve your doubts.

Introduction to MongoDB

High performance, easy to deploy, easy to use

Common application scenarios

Distributed log collection: the maximum elk per node is 32g, and the more mongodb, the better.

Sensors (electronic products)-database-MongoDB

Geographic map

Web crawler

3V in the era of big data

Massive Volume

Multiple Variety

Real-time Velocity

Three highs in the era of large database

High concurrency

Highly scalable

High performance

MongoDB cluster 1. One master and one slave

Docker-compose.yml

Version: '2'services: master: image:mongo:3.4 volumes:-/ data/mongodbml/master:/data/db command: mongod-- dbpath / data/db-- master slaver: image:mongo:3.4 volumes:-/ data/mongodbml/slaver:/data/db command: mongod-- dbpath / data/db-- slave-- source master:27017 links:-master

Note: the slave library cannot be read by default, and the status of SlaveOk needs to be set:

Docker-compose up-ddocker psdocker exec-it masterid / bin/bashmongo > show databases; > use test; > db.userinfo.insert ({"name": "master"}); > db.userinfo.find ({}); exitdocker exec-it slaveid / bin/bashmongo > show databases; > db.getMongo (). SetSlaveOk (); > use test; > db.userinfo.find ({}); docker-compose rm

Disadvantages: when the master library is down, you need to manually switch to the slave library.

two。 One master and two slaves

Docker-compose.yml

Version: '2'services: rs1: image:mongo:3.4 volumes:-/ data/mongodbtest/replset/rs1:/data/db command: mongod-dbpath / data/db-replset myset rs2: image:mongo:3.4 volumes:-/ data/mongodbtest/replset/rs2:/data/db command: mongod-- dbpath / data/db-- replset myset rs3: image:mongo:3.4 volumes:- / data/mongodbtest/replset/rs3:/data/db command: mongod-dbpath / data/db-replset myset

Settings: arrange rs1 into master node, rs2 and rs3 into slave node.

Docker-compose up-ddocker ps docker exec-it rs1id / bin/bashmongo > rs.initiate () myset:SECONDARY > rs.add ('rs2:27017'); myset:PRIMARY > rs.add (' rs3:27017'); myset:PRIMARY > rs.conf () myset:PRIMARY > show databases;myset:PRIMARY > use test;myset:PRIMARY > db.userinfo.insert ({"name": "rs1"}); myset:PRIMARY > db.userinfo.find ({}); exitdocker exec-it rs2id / bin/bashmongomyset:SECONDARY > rs.status (); myset:SECONDARY > rs.slaveOk () Myset:SECONDARY > show databases;myset:SECONDARY > use test;myset:SECONDARY > db.userinfo.find ({}); exitdocker exec-it rs3id / bin/bashmongomyset:SECONDARY > rs.slaveOk (); myset:SECONDARY > show databases;myset:SECONDARY > use test;myset:SECONDARY > db.userinfo.find ({})

Test: forcibly stop the master library rs1, check whether the slave library can be allocated and switch to the master library, and continue to forcibly stop the master library rs2

Docker stop rs1iddocker exec-it rs2id / bin/bashdocker stop rs2iddocker exec-it rs3id / bin/bashdocker-compose rm

Conclusion:

When the master node rs1 is down, there will be a slave node to replace the location of the original master library, but when there is only the last slave library left, it cannot be switched to the master library.

When the master node rs1 restarts and recovers, it will only become a slave library immediately and is no longer the master library. It is not possible to return to the location of the main library until the current main library is dead.

Problem: when the master node rs1 is down, there will be a slave node to replace the location of the original master library, but there is no way to control which slave library. So we need to add a blanking to solve this problem.

3. One master, one slave, one arbitration.

Docker-compose.yml

Version: '2'services: master: image:mongo:3.4 volumes:-/ data/mongodbnode/replset/rs1:/data/db command: mongod-- dbpath / data/db-- replset newset-- oplogSize 128 slave: image:mongo:3.4 volumes:-/ data/mongodbnode/replset/rs2:/data/db command: mongod-- dbpath / data/db-- replset newset-- oplogSize 128 arbiter: image:mongo:3.4 command : mongod-dbpath / data/db-replset newset-smallfiles-oplogSize 128

Configuration:

Docker-compose up-ddocker ps docker exec-it masterid / bin/bashmongo > rs.initiate () newset:SECONDARY > rs.add ('slave:27017'); newset:PRIMARY > rs.add (' arbiter:27017', true); newset:PRIMARY > re.conf () newset:PRIMARY > show databases;newset:PRIMARY > use test;newset:PRIMARY > db.userinfo.insert ({"name": "master"}); newset:PRIMARY > db.userinfo.find ({}); exitdocker exec-it slaveid / bin/bashnewset:SECONDARY > res.slaveOk (); newset:SECONDARY > use test Newset:SECONDARY > db.userinfo.find ({}); exitdocker exec-it arbiterid / bin/bashnewset:ARBITER > res.slaveOk (); newset:ARBITER > show databases;newset:ARBITER > db.userinfo.find ({})

Note: when there is an arbiter node (arbitration only, no data is stored)

After reading this, the article "how to read and write massive data efficiently by MongoDB" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report