当前位置:网站首页>MongoDB 分片总结
MongoDB 分片总结
2022-07-07 11:17:00 【cui_yonghua】
基础篇(能解决工作中80%的问题):
进阶篇:
其它:
一. 分片概述
分片(sharding)
是指:将数据拆分,将其分散在不同机器的过程,有时也用分区(partitioning)来表示这个概念。将数据分散在不同的机器上,不需要强大的大型计算机就能存储更多的数据,可以满足MongoDB数据量大量增长的需求。
当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量。这时,我们就可以通过在多台机器上分割数据,使得数据库系统能存储和处理更多的数据。
注意:
副本集:能解决自动故障转移,主从复制,集群。解决的问题:数据冗余备份,架构高可用;但不能解决单节点压力问题(硬件限制,并发访问压力)
为什么使用分片:
1、本地磁盘不够大
2、当请求量巨大时会出现内存不足。
3、垂直扩展价格昂贵(内存、磁盘、cpu)
二. 分片集群结构
在MongoDB中使用分片集群结构分布:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vs4UlO1S-1657022797641)(evernotecid://B1CD39FE-B044-413D-A086-0649DB3F0070/appyinxiangcom/26430792/ENResource/p1225)]
上图中主要有如下所述三个主要组件:Shard
:用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
Config Server
:mongod实例,存储了整个 ClusterMetadata,其中包括 chunk信息。
Query Routers
:前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
三. 分片实例
分片结构端口分布如下:
Shard Server 1:27031
Shard Server 2:27032
Config Server: 27100
Route Process: 27777
步骤一:启动Shard Server
sudo rm -rf /MongoDB/shard/s1 /MongoDB/shard/s2 /MongoDB/shard/log
sudo mkdir -p /MongoDB/shard/s1 /MongoDB/shard/s2 /MongoDB/shard/log
sudo mongod --port 27031 --dbpath=/MongoDB/shard/s1
sudo mongod --port 27032 --dbpath=/MongoDB/shard/s2
步骤二: 启动Config Server
sudo rm -rf /MongoDB/shard/config
sudo mkdir -p /MongoDB/shard/config
sudo mongod --port 27100 --dbpath=/MongoDB/shard/config
注意:这里我们完全可以像启动普通mongodb服务一样启动,不需要添加—shardsvr和configsvr参数。因为这两个参数的作用就是改变启动端口的,所以我们自行指定了端口就可以。
步骤三: 启动Route Process
mongos --port 27777 --configdb 192.168.17.129:27100
步骤四: 配置Sharding
接下来,我们使用MongoDB Shell登录到mongos,添加Shard节点
mongo admin --port 27777
MongoDB shell version: 2.0.7
connecting to: 127.0.0.1:27777/admin
mongos> db.runCommand({
addshard:"192.168.17.129:27031" })
{
"shardAdded" : "shard0000", "ok" : 1 }
......
mongos> db.runCommand({
addshard:"192.168.17.129:27032" })
{
"shardAdded" : "shard0009", "ok" : 1 }
步骤五:对某个数据库test启用分片
#设置分片存储的数据库
mongos> db.runCommand({
enablesharding:"test" })
{
"ok" : 1 }
步骤六:对collection进行分片
mongos> db.runCommand({
shardcollection: "test.mycol", key: {
_id:1}})
{
"collectionsharded" : "test.mycol", "ok" : 1 }
步骤七:测试
mongo test --port 27777
输出10000条数据
use testvar
num =10000
for (var i=0;i<num;i++){
db.mycol.save({
'_id':i})
}
程序代码内无需太大更改,直接按照连接普通的mongo数据库那样,将数据库连接接入接口27777
步骤八: 查看分片情况
查看分片情况时,必须在config(配置服务器上执行)而且必须在admin(如mongo 127.0.0.1:27100/admin)集合下执行
mongo admin --port 27100 #config(配置服务器上执行)
sh.status()
# 输出如下:
-- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("57cfcdfef06b33543fdeb52e")
}
shards:
{
"_id" : "shard0000", "host" : "localhost:27031" }
{
"_id" : "shard0001", "host" : "localhost:27032" }
active mongoses:
"3.2.7" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{
"_id" : "test", "primary" : "shard0000", "partitioned" : true }
test.mycol
shard key: {
"_id" : 1 }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 1
{
"_id" : {
"$minKey" : 1 } } -->> {
"_id" : 1 } on : shard0001 Timestamp(2, 0)
{
"_id" : 1 } -->> {
"_id" : 57 } on : shard0000 Timestamp(2, 1)
{
"_id" : 57 } -->> {
"_id" : {
"$maxKey" : 1 } } on : shard0000 Timestamp(1, 3)
四. Hashed Sharding
选择哈希片键最大的好处就是保证数据在各个节点分布基本均匀,下面使用_id作为哈希片键做个简单的测试:
mongo admin --port 27777
mongos> db.runCommand({
shardcollection: "test.myhash", key: {
_id:"hashed"}})
{
"collectionsharded" : "test.myhash", "ok" : 1 }
use test
var num =10000
for (var i=0;i<num;i++){
db.myhash.save({
'_id':i})
}
总结:哈希分片将提供的片键散列成一个非常大的长整型作为最终的片键。
边栏推荐
- 线程池拒绝策略最佳实践
- 关于 appium 启动 app 后闪退的问题 - (已解决)
- What if the xshell evaluation period has expired
- 【学习笔记】线段树选做
- Query whether a field has an index with MySQL
- “新红旗杯”桌面应用创意大赛2022
- 为租客提供帮助
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
- Differences between MySQL storage engine MyISAM and InnoDB
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
猜你喜欢
Awk of three swordsmen in text processing
“新红旗杯”桌面应用创意大赛2022
leecode3. 无重复字符的最长子串
[crawler] avoid script detection when using selenium
通过Keil如何查看MCU的RAM与ROM使用情况
为租客提供帮助
【无标题】
关于 appium 启动 app 后闪退的问题 - (已解决)
Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
自定义线程池拒绝策略
随机推荐
人均瑞数系列,瑞数 4 代 JS 逆向分析
The difference between cache and buffer
DETR介绍
HZOJ #240. Graphic printing IV
通过Keil如何查看MCU的RAM与ROM使用情况
RecyclerView的数据刷新
[untitled]
HZOJ #235. Recursive implementation of exponential enumeration
About the problem of APP flash back after appium starts the app - (solved)
自定义线程池拒绝策略
详细介绍六种开源协议(程序员须知)
Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
Leetcode brush question: binary tree 24 (the nearest common ancestor of binary tree)
Day22 deadlock, thread communication, singleton mode
DrawerLayout禁止侧滑显示
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
TPG x AIDU|AI领军人才招募计划进行中!
ORACLE进阶(五)SCHEMA解惑
聊聊伪共享
regular expression