当前位置:网站首页>Mongodb - replica set and sharding
Mongodb - replica set and sharding
2022-06-28 20:40:00 【Did HYK write the algorithm today】
Catalog
MongoDB Copy ( Replica set )
Introduce
MongoDB Replication is the process of synchronizing data on multiple servers .
Replication provides redundant backup of data , And store copies of data on multiple servers , Improved data availability , And can guarantee the security of data .
Replication also allows you to recover data from hardware failures and service outages .
What is replication
1 Secure data
2 High availability of data (24*7)
3 disaster recovery
4 No need to shut down for maintenance ( Backup , Rebuild index , Compress )
5 Distributed read data
6MongoDB The principle of replication
7mongodb Replication of requires at least two nodes . One of them is the master node , Responsible for handling client requests , The rest are from nodes , Responsible for copying the data on the primary node .
mongodb Common collocation methods of each node
mongodb The common collocation mode of each node is : A master from 、 One master, many followers
All operations recorded on the master node oplog, Periodically polling the master from the node for these operations , Then perform these operations on your own copy of the data , So as to ensure that the data of the slave node is consistent with that of the master node .
MongoDB Copy the structure diagram as follows :

Title replica set features
1、N Cluster of nodes
2、 Any node can be the master node
3、 All writes are on the primary node
4、 Automatic failover
5、 Automatic recovery
6、MongoDB Replica set settings
Main operation steps
1、 Close running MongoDB The server .
Now let's specify --replSet Option to start mongoDB.–replSet The basic syntax is as follows :
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
example
mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
The above example will start a named rs0 Of MongoDB example , Its port number is 27017.
After startup, open the command prompt box and connect mongoDB service .
stay Mongo The client uses the command rs.initiate() To start a new replica set .
We can use rs.conf() To view the configuration of the replica set
View replica set status using rs.status() command
2、 Replica set add members
Add members of the replica set , We need to use multiple servers to start mongo service . Get into Mongo client , And use rs.add() Method to add members of the replica set .
grammar
rs.add() The basic syntax of the command is as follows :
>rs.add(HOST_NAME:PORT)
example
Suppose you have started a program called mongod1.net, The port number is 27017 Of Mongo service . Use... In the client command window rs.add() Command to add it to the replica set , The command is as follows :
>rs.add("mongod1.net:27017")
>
MongoDB In, you can only use the main node to Mongo Add service to replica set , Determine the current running Mongo Whether the service is the master node can use the command db.isMaster() .
MongoDB The replica set of is different from our common master-slave , After the master-slave host goes down, all services will stop , The replica set after the host goes down , The replica takes over the master node and becomes the master node , No downtime .
MongoDB Fragmentation
Introduce
stay Mongodb There's another kind of cluster , It's sharding technology , You can meet MongoDB The demand for a huge increase in data volume .
When MongoDB When storing massive amounts of data , A machine may not be enough to store data , It may not be enough to provide acceptable read and write throughput . At this time , We can split data on multiple machines , Make database system can store and process more data .
Why use shards
1、 Copy all writes to the primary node
2、 Delayed sensitive data will be queried at the primary node
3、 A single replica set is limited to 12 Nodes
4、 When the request volume is large, there will be insufficient memory .
5、 Not enough local disks
6、 Vertical expansion is expensive
MongoDB The distribution of the cluster structure
The figure below shows MongoDB Use the partition cluster structure to distribute :

In the figure above, there are three main components as follows :
Shard:
Used to store actual data blocks , In a real production environment shard server A character can be made up of several machines replica set To undertake , Prevent single point failure of the main engine
Config Server:
mongod example , Store the whole ClusterMetadata, These include chunk Information .
Query Routers:
Front-end routing , The client is connected by this , And make the whole cluster look like a single database , Front end applications can be used transparently .
Slice instance
The port distribution of slice structure is as follows :
Shard Server 1:27020
Shard Server 2:27021
Shard Server 3:27022
Shard Server 4:27023
Config Server :27100
Route Process:40000
Step one : start-up Shard Server
[root@100 /]# mkdir -p /www/mongoDB/shard/s0
[root@100 /]# mkdir -p /www/mongoDB/shard/s1
[root@100 /]# mkdir -p /www/mongoDB/shard/s2
[root@100 /]# mkdir -p /www/mongoDB/shard/s3
[root@100 /]# mkdir -p /www/mongoDB/shard/log
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork
....
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork
Step two : start-up Config Server
[root@100 /]# mkdir -p /www/mongoDB/shard/config
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork
Be careful : Here we can start up like ordinary mongodb Start like a service , No need to add —shardsvr and configsvr Parameters . Because the function of these two parameters is to change the startup port , So we can specify the port by ourselves .
Step three : start-up Route Process
/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500
mongos In the startup parameter ,chunkSize This item is used to specify chunk The size of the , The unit is MB, The default size is 200MB.
Step four : To configure Sharding
Next , We use MongoDB Shell Log in to mongos, add to Shard node
[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000
MongoDB shell version: 2.0.7
connecting to: 127.0.0.1:40000/admin
mongos> db.runCommand({
addshard:"localhost:27020" })
{
"shardAdded" : "shard0000", "ok" : 1 }
......
mongos> db.runCommand({
addshard:"localhost:27029" })
{
"shardAdded" : "shard0009", "ok" : 1 }
mongos> db.runCommand({
enablesharding:"test" }) # Set the database stored in slices
{
"ok" : 1 }
mongos> db.runCommand({
shardcollection: "test.log", key: {
id:1,time:1}})
{
"collectionsharded" : "test.log", "ok" : 1 }
Step five : No major changes are needed in the program code , Connect ordinary... Directly according to mongo Like the database , Connect the database to the interface 40000
边栏推荐
- LeetCode每日一题——522. 最长特殊序列 II
- 软件watchdog和ANR触发memory dump讲解
- API 网关 Apache APISIX 助力雪球双活架构演进
- 2022 welder (elementary) special operation certificate examination question bank and answers
- C # connect to the database to complete the operation of adding, deleting, modifying and querying
- Pie (poj3122) super detailed and easy to understand binary introduction
- A few lines of code can realize complex excel import and export. This tool class is really powerful!
- With a market value of $120billion, how did intuit, an old tax giant, do it?
- 稳定性总结
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
猜你喜欢

RT-Thread线程同步与线程通信

我也差点“跑路”

API 网关 Apache APISIX 助力雪球双活架构演进

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

Flatten of cnn-lstm

Analysis of variance

Lecture 30 linear algebra Lecture 4 linear equations

Rsync remote synchronization

不同框架的绘制神经网络结构可视化

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
随机推荐
题解 Ananagrams(UVa156)紫书P113map的应用
1. 整合 Servlet
Visualization of neural network structure in different frames
APISIX 助力中东社交软件,实现本地化部署
Apisik helps Middle East social software realize localized deployment
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
with torch.no_grad():的使用原因
iterator中的next()为什么要强转?
RT thread thread synchronization and thread communication
Is the inter-bank certificate of deposit reliable and safe
Why does next() in iterator need to be forcibly converted?
28 rounds of interviews with 10 companies in two and a half years
请允许当下国内ToB的「不完美」
Globalsign's Pan domain SSL certificate
openGauss内核分析之查询重写
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
Résumé de la stabilité
MySQL system error occurred 1067
Anr no response introduction
Lecture 30 linear algebra Lecture 4 linear equations