.net wap网站模板,做影评的网站模版,浙江新中环建设有限公司 网站,凡科网做网站Mongodb分片概括 分片在多台服务器上分布数据的方法#xff0c; Mongodb使用分片来支持具有非常大的数据集和高吞吐量的操作的部署 具有大数据集和高吞吐量应用程序的数据库系统#xff0c;可以挑战单台服务器的容量。 例如#xff0c;高查询率可以耗尽服务器的cpu容量 Mongodb使用分片来支持具有非常大的数据集和高吞吐量的操作的部署 具有大数据集和高吞吐量应用程序的数据库系统可以挑战单台服务器的容量。 例如高查询率可以耗尽服务器的cpu容量工作集大小大于系统的RAM强制磁盘驱动器的I/O容量 有两种方法来解决系统增长垂直和水平缩放。 垂直缩放 涉及增加的单个服务器的容量例如使用更强大的CPU加入更多的RAM或增加的存储空间量。可用技术中的限制可能限制单个机器对于给定工作负载足够强大。此外基于云的提供商具有基于可用硬件配置的硬上限。因此对于垂直缩放存在实际的最大值。 包括将系统数据和负载在多个服务器添加额外的服务器需要增加容量。虽然单个机器的总速度或容量可能不高但是每个机器处理整个工作负载的子集潜在地提供比单个高速大容量服务器更好的效率。扩展部署的容量仅需要根据需要添加额外的服务器这可以是比单个机器的高端硬件低的总体成本。权衡是基础设施的复杂性和部署的维护。 Mongodb的支持水平扩展分片。
1、分片目的
对于单台数据库服务器庞大的数据量及高吞吐量的应用程序对它而言无疑是个巨大的挑战。频繁的CRUD操作能够耗尽服务器的CPU资源快速的数据增长也会让硬盘存储无能为力最终内存无法满足数据需要导致大量的I/O主机负载严重。为了解决这种问题对于数据库系统一般有两种方法垂直扩展和分片水平扩展。
【垂直扩展】添加更多的CPU和存储资源来增加系统性能。这种方式缺点是拥有大量CPU和RAM资源的高端机器比普通PC机器昂贵得太多而且单点故障会影响整个系统的服务。
【分片】相反地分片将大的数据集分配到多台主机上每个分片是一个独立的数据库这些分片整体上构成一个完整的逻辑数据库。分片减少了每台服务器上的数据操作量随着集群的增长每台分片处理越来越少的数据结果增加了系统整体服务能力。另外分片还减少了每台服务器需要存储的数据量。
2、MongoDB中的分片
MongoDB通过配置分片集群来支持分片一个分片集群包括以下几个组件分片查询路由配置服务器
**分片**用来存储数据为了提供系统可用性和数据一致性一个生产环境的分片集群通常每个分片是一个副本集。查询路由指客户端应用访问每个分片的路径。配置服务器存储集群的元数据这些数据包含了集群数据集到各分片的映射关系。查询路由就是通过这些元数据到特定的分片上执行指定的数据操作。从v3.2开始配置服务器也可以作为副本集但是必须使用WiredTiger存储引擎反对使用3个镜像实例作为配置服务器
数据划分
MongoDB的数据划分是以集合级别为标准。分片通过shard key来划分集合数据。
shard key
为了对集合分片你需要指定一个shard key。shard key既可以是集合的每个文档的索引字段也可以是集合中每个文档都有的组合索引字段。MongoDB将shard keys值按照块chunks划分并且均匀的将这些chunks分配到各个分片上。MongoDB使用基于范围划分或基于散列划分来划分chunks的。
基于范围划分
MongoDB通过shard key值将数据集划分到不同的范围就称为基于范围划分。对于数值型的shard key你可以虚构一条从负无穷到正无穷的直线理解为x轴每个shard key 值都落在这条直线的某个点上然后MongoDB把这条线划分为许多更小的没有重复的范围成为块chunks一个chunk就是就某些最小值到最大值的范围。
基于散列划分
MongoDB计算每个字段的hash值然后用这些hash值建立chunks。
基于范围和基于散列划分的性能比较
基于范围划分对于范围查询比较高效。假设在shard key上进行范围查询查询路由很容易能够知道哪些块与这个范围重叠然后把相关查询按照这个路线发送到仅仅包含这些chunks的分片。但是基于范围划分很容易导致数据不均匀分布这样会削弱分片集群的功能。例如当shard key是个成直线上升的字段如时间。那么所有在给定时间范围内的请求都会映射到相同的chunk也就是相同的分片上。这种情况下小部分的分片将会承受大多数的请求那么系统整体扩展并不理想。
相反的基于散列划分是以牺牲高效范围查询为代价它能够均匀的分布数据散列值能够保证数据随机分布到各个分片上。
使用标签来自定义数据分布
MongoDB允许DBA们通过标签标记分片的方式直接平衡数据分布策略DBA可以创建标签并且将它们与shard key值的范围进行关联然后分配这些标签到各个分片上最终平衡器转移带有标签标记的数据到对应的分片上确保集群总是按标签描述的那样进行数据分布。标签是控制平衡器行为及集群中块分布的主要方法
4、维持数据分布平衡
新加入的数据及服务器都会导致集群数据分布不平衡MongoDB采用两种方式确保数据分布的平衡
拆分
拆分是一个后台进程防止块变得太大。当一个块增长到指定块大小的时候拆分进程就会块一分为二整个拆分过程是高效的。不会涉及到数据的迁移等操作。
平衡
平衡器是一个后台进程管理块的迁移。平衡器能够运行在集群任何的mongd实例上。当集群中数据分布不均匀时平衡器就会将某个分片中比较多的块迁移到拥有块较少的分片中直到数据分片平衡为止。举个例子如果集合users有100个块在分片1里50个块在分片2中那么平衡器就会将分片1中的块迁移到分片2中直到维持平衡。
分片采用后台操作的方式管理着源分片和目标分片之间块的迁移。在迁移的过程中源分片中的块会将所有文档发送到目标分片中然后目标分片会获取并应用这些变化。最后更新配置服务器上关于块位置元数据。
从集群中增加和删除分片
添加新分片到集群中会产生数据不平衡因为新分片中没有块当MongoDB开始迁移数据到新分片中时等到数据分片平衡恐怕需要点时间。
当删除一个分片时平衡器将会把分片中所有块迁移到另一个分片中在完成这些迁移并更新元数据后你就可以安全的删除分片了。
分片集群 一个mongodb分片集群由以下几部分组成 shard 每个shard包含分片数据的子集每个shard可以部署一个副本集 一台机器的一个数据表 Collection1 存储了 1T 数据压力太大了在分给4个机器后每个机器都是256G则分摊了集中在一台机器的压力。也许有人问一台机器硬盘加大一点不就可以了为什么要分给四台机器呢不要光想到存储空间实际运行的数据库还有硬盘的读写、网络的IO、CPU和内存的瓶颈。在mongodb集群只要设置好了分片规则通过mongos操作数据库就能自动把对应的数据操作请求转发到对应的分片机器上。在生产环境中分片的片键可要好好设置这个影响到了怎么把数据均匀分到多个分片机器上不要出现其中一台机器分了1T其他机器没有分到的情况这样还不如不分片 mongos MongoS充当一个查询的路由器提供客户端应用程序和所述分片簇之间的接口,mongos作为数据库集群请求的入口所有的请求都是通过mongos来进行协调的不需要在应用程序添加一个路由选择器mongos自己就是一个请求分发中心它负责把对应的数据请求转发到对应的shard服务器上在生产环境中通常有多个monogs作为请求的入口防止其中一个挂掉所有mongos请求都没有办法操作 config servers 为集群配置的服务器存储元数据和配置设置从Mongodb3.4开始配置服务器必须部署为复制集mongos本身没有物理存储分片服务器和数据路由信息只是缓存在内存当中配置服务器则实际存储这些数据mongos第一次启动或者关掉重启会从configserver中加载配置信息以后如果配置信息有变化会通过所有的mongos更新自己的状态这样mongs就能继续准确路由在生产环境中通常有多个config server配置服务器因为它存储了分片路由的元数据如果就一个如果挂掉一个整个mongodb基础就会挂掉。
片键 片键 1、在分发集合中文件时mongodb的分区使用的收集片键关键在片键由存在目标集合中的每个文档中的一个不可变或多个字段 2、在分割集合的时候选择片键分片键完成之后是不能更改的分片集合只能有1个片键到片键的非空集合集合必须有一个索引与片键启动对于空空集合如果集合尚未具有指定分片键的相关索引则Mongodb会创建索引 3、分片键的选择会影响分片集群的性能和效率以及可伸缩性具有最佳可能的硬件可以通过分片达到瓶颈片键和其支持指数的选择也可以影响数据的拆分但集群可以使用 4、片键决定了集群中一个集合的文件咋不同的片键中的分布片键字段必须被索引且在集合中的每条记录都不能为空可以是单个字段或者是复合字段 5、Mongodb使用片键的范围是吧数据分布在分片中每个范围又称为数据块定义了一个不重叠的片键范围Mongodb把数据块与他们存储的文档分布到集群中的不同分布中当一个数据块的大小超过数据块最大大小的时候Mongodb会宜聚片键的范围将数据块分裂为更小的数据块 片键的使用语法 1、在分片集合必须制定目标集合和片键的sh.shardCollection()
sh.shardCollection(namespace, key)2、哈希片键使用单字段的哈希索引进行数据在分片之间的平均分发除数取余和一致性哈希 3、被选为片键的字段必须有足够大的基数或者有足够多的不同的值对于单调的递增的字段如果ObjectID或是时间戳哈希索引效果更好 4、如果在一个空集合创建哈希片键Mongodb会自动创建并迁移数据块以保证每个分片上都有两个数据块也可以执行shardCollection指定numInitialChunks参数以控制初始化时Mongodb创建数据块数目或者手动调用split命令在分片上分裂数据块 5、对使用了哈希片键分片的集合进行请求时Mongodb会自动计算哈希值应用不需要解析哈希值
shard集群部署
部署ip规划 172.17.237.33:30001 config1 172.17.237.34:30002 config2 172.17.237.36:30003 config3 172.17.237.37:40000 mongos 172.17.237.38:50000 shard1 172.17.237.39:50001 shard2 172.17.237.40:50002 shard3 172.17.237.41:60000 sha1 172.17.237.42:60001 sha2 172.17.237.43:60002 sha3
配置config server 副本集
配置confi1配置文件
[rootMy-Dev db2]# vim config1.conf
[rootMy-Dev db1]# vim configsvr.conf
logpath/home/mongodb/test/db1/log/db1.log
pidfilepath/home/mongodb/test/db1/db1.pid
logappendtrue
port30000
forktrue
dbpath/home/mongodb/test/db1/data
configsvrtrue # 在配置文件添加此项就行
oplogSize512
replSetconfig配置confi2配置文件
[rootMy-Dev db2]# vim config2.conf
logpath/home/mongodb/test/db2/log/db2.log
pidfilepath/home/mongodb/test/db2/db2.pid
logappendtrue
port30001
forktrue
dbpath/home/mongodb/test/db2/data
oplogSize512
replSetconfig
configsvrtrue配置confi3配置文件
[rootMy-Dev db2]# vim config3.conf
logpath/home/mongodb/test/db3/log/db3.log
pidfilepath/home/mongodb/test/db3/db3.pid
logappendtrue
port30002
forktrue
dbpath/home/mongodb/test/db3/data
oplogSize512
replSetconfig
configsvrtrue启动config server
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db1/config1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 5260
child process started successfully, parent exiting[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db2/config2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 5202
child process started successfully, parent exiting[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db3/config3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4260
child process started successfully, parent exiting配置config副本集 use admin
switched to db admin config { _id:config,members:[ {_id:0,host:conf1:30000,priority:2}}, {_id:1,host:conf2:30001,priority:1}, {_id:2,host:conf3:30002,priority:1}] } #定义副本集
{_id : config,members : [{_id : 0,host : conf1:30000},{_id : 1,host : conf2:30001},{_id : 2,host : conf3:30002}]
}rs.initiate(config) #初始化副本集
{ ok : 1 }配置mongos
添加配置mongos配置文件 遇到坑了在启动mongos的时候启动失败结果是mongodb3.0以后的版本config server必须是复制集才行结果我的版本是3.4最新的版本所以说还需要添加两台confi server
[rootMy-Dev db4]# vim mongos.conf logpath/home/mongodb/test/db4/log/db4.log
pidfilepath/home/mongodb/test/db4/db4.pid
logappendtrue
port40004
forktrue
configdbmongos/172.17.237.33:30000,172.17.237.34:30001,172.17.237.36:30002 #如果有多个mongo confi的话就用逗号分隔开 启动mongos
[rootMy-Dev bin]# ./mongos -f /home/mongodb/test/db4/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 6268
child process started successfully, parent exitingshard2副本集集群部署
配置sha配置文件
[rootMy-Dev db8]# more shard21.conf
logpath/home/mongodb/test/db8/log/db8.log
pidfilepath/home/mongodb/test/db8/db8.pid
directoryperdbtrue
logappendtrue
port60000
forktrue
dbpath/home/mongodb/test/db8/data
oplogSize512
replSetsha
shardsvrtrue[rootMy-Dev db9]# more shard22.conf
logpath/home/mongodb/test/db9/log/db9.log
pidfilepath/home/mongodb/test/db9/db9.pid
directoryperdbtrue
logappendtrue
port60001
forktrue
dbpath/home/mongodb/test/db9/data
oplogSize512
replSetsha
shardsvrtrue[rootMy-Dev db10]# more shard23.conf
logpath/home/mongodb/test/db10/log/db10.log
pidfilepath/home/mongodb/test/db10/db10.pid
directoryperdbtrue
logappendtrue
port60002
forktrue
dbpath/home/mongodb/test/db10/data
oplogSize512
replSetsha
shardsvrtrue启动shard
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db8/shard21.conf
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db9/shard22.conf
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db10/shard23.conf 配置shard2副本集集群 use admin
switched to db admin
#特殊说明priority 权重值越高级别就越高也优先是主。sha { _id:sha,members:[ {_id:0,host:sha1:60000,priority:2}, {_id:1,host:sha2:60001,priority:1}, {_id:2,host:sha3:60002,arbiterOnly:true}}]}
{_id : sha,members : [{_id : 0,host : sha1:60000},{_id : 1,host : sha2:60001},{_id : 2,host : sha3:60002}]
}rs.initiate(sha)
{ ok : 1 }shard1副本集集群部署
配置shard配置文件
[rootMy-Dev db5]# vim shard1.conf
logpath/home/mongodb/test/db5/log/db5.log
pidfilepath/home/mongodb/test/db5/db5.pid
directoryperdbtrue
logappendtrue
port50000
forktrue
dbpath/home/mongodb/test/db5/data
oplogSize512
replSetshard
shardsvrtrue[rootMy-Dev db6]# vim shard2.conf
logpath/home/mongodb/test/db6/log/db6.log
pidfilepath/home/mongodb/test/db6/db6.pid
directoryperdbtrue
logappendtrue
port50001
forktrue
dbpath/home/mongodb/test/db6/data
oplogSize512
replSetshard
shardsvrtrue[rootMy-Dev db7]# vim shard3.conf
logpath/home/mongodb/test/db7/log/db7.log
pidfilepath/home/mongodb/test/db7/db7.pid
directoryperdbtrue
logappendtrue
port50002
forktrue
dbpath/home/mongodb/test/db7/data
oplogSize512
replSetshard
shardsvrtrue启动shard
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db7/shard1.conf
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db7/shard2.conf
[rootMy-Dev bin]# ./mongod -f /home/mongodb/test/db7/shard3.conf 配置shard2副本集集群 use admin
switched to db adminshard { _id:shard,members:[ {_id:0,host:shard1:50000,priority:1}, {_id:1,host:shard2:50001,priority:2}, {_id:2,host:shard3:50002,arbiterOnly:true}}] }
{_id : shard,members : [{_id : 0,host : shard1:50000},{_id : 1,host : shard2:50001},{_id : 2,host : shard3:50002}]
}rs.initiate(shard)
{ ok : 1 }分片配置 分片集合中是否有数据 默认第一个添加的shard就是主shard存放没有被分割的shard就是主shard 在创建分片的时必须在索引中创建的如果这个集合中有数据则首先自己先创建索引然后进行分片如果是分片集合中没有数据的话则就不需要创建索引就可以分片 登陆mongos配置分片向分区集群中添加shard服务器和副本集
[rootMy-Dev bin]# ./mongo mongos:40004 #登陆到mongos中mongos sh.status() #查看分片状态
--- Sharding Status --- sharding version: {_id : 1,minCompatibleVersion : 5,currentVersion : 6,clusterId : ObjectId(589b0cff36b0915841e2a0a2)
}shards:active mongoses:3.4.1 : 1autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noBalancer lock taken at Wed Feb 08 2017 20:20:16 GMT0800 (CST) by ConfigServer:BalancerFailed balancer rounds in last 5 attempts: 0Migration Results for the last 24 hours: No recent migrationsdatabases:添加shard副本集
#首先要登陆到shard副本集中查看那个是主节点本次实验室使用了两个shard副本集 sh.addShard(replSetName/主节点IP/port)
mongos sh.addShard(shard/shard1:50000)
{ shardAdded : shard, ok : 1 }mongos sh.addShard(sha/sha:60000)
{ shardAdded : shard, ok : 1 }mongos sh.status() #查看分片集群已经成功把shard加入分片中
--- Sharding Status --- sharding version: {_id : 1,minCompatibleVersion : 5,currentVersion : 6,clusterId : ObjectId(589b0cff36b0915841e2a0a2)
}shards:{ _id : sha, host : sha/sha1:60000,sha2:60001,sha3:60002, state : 1 }{ _id : shard, host : shard/shard1:50000,shard2:50001,shard3:50002, state : 1 }active mongoses:3.4.1 : 1autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noBalancer lock taken at Wed Feb 08 2017 20:20:16 GMT0800 (CST) by ConfigServer:BalancerFailed balancer rounds in last 5 attempts: 5Last reported error: Cannot accept sharding commands if not started with --shardsvrTime of Reported error: Thu Feb 09 2017 17:42:21 GMT0800 (CST)Migration Results for the last 24 hours: No recent migrationsdataba指定那个数据库使用分片创建片键
mongos sh.enableSharding(zhao) #指定zhao数据库中使用分片
{ ok : 1 }mongos sh.shardCollection(zhao.call,{name:1,age:1}) #在zhao数据库和call集合中创建了name和age为升序的片键
{ collectionsharded : zhao.call, ok : 1 }查看sh.status()信息
mongos sh.status()
--- Sharding Status --- sharding version: {_id : 1,minCompatibleVersion : 5,currentVersion : 6,clusterId : ObjectId(589b0cff36b0915841e2a0a2)
}shards:{ _id : sha, host : sha/sha1:60000,sha2:60001,sha3:60002, state : 1 }{ _id : shard, host : shard/shard1:50000,shard2:50001,shard3:50002, state : 1 }active mongoses:3.4.1 : 1autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noBalancer lock taken at Wed Feb 08 2017 20:20:16 GMT0800 (CST) by ConfigServer:BalancerFailed balancer rounds in last 5 attempts: 5Last reported error: Cannot accept sharding commands if not started with --shardsvrTime of Reported error: Thu Feb 09 2017 17:56:02 GMT0800 (CST)Migration Results for the last 24 hours: No recent migrationsdatabases:{ _id : zhao, primary : shard, partitioned : true }zhao.callshard key: { name : 1, age : 1 }unique: falsebalancing: truechunks:shard 1{ name : { $minKey : 1 }, age : { $minKey : 1 } } -- { name : { $maxKey : 1 }, age : { $maxKey : 1 } } on : shard Timestamp(1, 0) 测试批量插入数据验证
mongos for ( var i1;i10000000;i){db.call.insert({name:useri,age:i})};查看当前是否已经分片到两个shard中去了
mongos sh.status()
--- Sharding Status --- sharding version: {_id : 1,minCompatibleVersion : 5,currentVersion : 6,clusterId : ObjectId(589b0cff36b0915841e2a0a2)
}shards:{ _id : sha, host : sha/sha1:60000,sha2:60001,sha3:60002, state : 1 }{ _id : shard, host : shard/shard1:50000,shard2:50001,shard3:50002, state : 1 }active mongoses:3.4.1 : 1autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noBalancer lock taken at Wed Feb 08 2017 20:20:16 GMT0800 (CST) by ConfigServer:BalancerFailed balancer rounds in last 5 attempts: 5Last reported error: Cannot accept sharding commands if not started with --shardsvrTime of Reported error: Thu Feb 09 2017 17:56:02 GMT0800 (CST)Migration Results for the last 24 hours: 4 : Successdatabases:{ _id : zhao, primary : shard, partitioned : true }zhao.callshard key: { name : 1, age : 1 }unique: falsebalancing: truechunks: #数据已经分片到两个chunks里面了sha 4shard 5{ name : { $minKey : 1 }, age : { $minKey : 1 } } -- { name : user1, age : 1 } on : sha Timestamp(4, 1) { name : user1, age : 1 } -- { name : user1, age : 21 } on : shard Timestamp(5, 1) { name : user1, age : 21 } -- { name : user1, age : 164503 } on : shard Timestamp(2, 2) { name : user1, age : 164503 } -- { name : user1, age : 355309 } on : shard Timestamp(2, 3) { name : user1, age : 355309 } -- { name : user1, age : 523081 } on : sha Timestamp(3, 2) { name : user1, age : 523081 } -- { name : user1, age : 710594 } on : sha Timestamp(3, 3) { name : user1, age : 710594 } -- { name : user1, age : 875076 } on : shard Timestamp(4, 2) { name : user1, age : 875076 } -- { name : user1, age : 1056645 } on : shard Timestamp(4, 3) { name : user1, age : 1056645 } -- { name : { $maxKey : 1 }, age : { $maxKey : 1 } } on : sha Timestamp(5, 0)
查看当前分片中是否均匀的分配到连个shard当中,true是均匀的 false不是均匀的 mongos sh.getBalancerState()
true选择sharing kes注意点
考虑应该在哪里储存数据应该在哪里读取数据sharding key 应该是主键sharding key 应该你能尽量保证避免分片查询
sharing 进级
如果sharing 分片不均匀没有分片均匀sharding 新增shard和移除shard
mongos sh.addShard(sha4/192.168.2.10:21001)Balancer
开启Balncer 开启Balancer之后chunks之后会自动均分
mongos sh.startBalancer()设置Balancer进程运行时间窗口 默认情况ixaBalancing进程在运行时为降低Balancing进程对系统的影响可以设置Balancer进程的运行时间窗口让Balancer进程在指定时间窗口操作
#设置时间窗口
db.settings.update({ _id : balancer }, { $set : { activeWindow : { start : 23:00, stop : 6:00 } } }, true )查看Balancer运行时间窗口
# 查看Balancer时间窗口
mongos db.settings.find();
{ _id : balancer, activeWindow : { start : 23:00, stop : 6:00 }, stopped : false }mongos sh.getBalancerWindow()
{ start : 23:00, stop : 6:00 }删除Balancer进程运行时间窗口 mongos db.settings.update({ _id : balancer }, { $unset : { activeWindow : 1 }});
mongos db.settings.find();
{ _id : chunksize, value : 10 }
{ _id : balancer, stopped : false }在shell脚本中执行mongodb
[rootMy-Dev ~]# echo -e use zhao \n db.call.find() |mongo --port 60001
Mongodb片键的添加
首先进入mongos的的admin数据库中
mongos use admin
switched to db admin
mongos db.runCommand({enablesharding:zl}) #创建zl库中
{ ok : 1 }
mongos db.runCommand(db.runCommand({shardcollection:$ent.t_srvappraise_back,key)分片脚本
#!/bin/bash
url10.241.96.155
port30000
enttest1./mongo $url:$port/admin EOF
db.runCommand({enablesharding:$ent});
db.runCommand({shardcollection:$ent.t_srvappraise_back,key:{sa_seid:hashed}})
exit;
EOF
db.currentOp() 和 db.killOp() 方法说明
-db.currentOp()方法会返回数据库实例当前操作的信息是数据库命令currentOp的封装。
语法
db.currentOp( )operations 是可选项。 可以是boolean 或者 document 类型。 当指定true时会包含空闲连接和系统操作。当指定查询条件的filter文档时则只返回匹配条件的操作。db.currentOp()支持的filter文档有如下3种类型
“$ownOps” 布尔型当设置为true时只返回当前用户的操作信息。
“$all”布尔型当设置为true时返回所有操作的信息包括空闲连接和系统操作。
根据output fields指定过滤条件。 当”$all”: true 和 output fields 同时存在时只有”$all”: true生效。 查询正在等待lock的所有写操作信息
db.currentOp({waitingForLock : true,$or: [{ op : { $in : [ insert, update, remove ] } },{ command.findandmodify: { $exists: true } }]}
)查询所有活动但没有工作active but no yield的操作
db.currentOp({active : true,numYields : 0,waitingForLock : false}
)查询db1 数据库上所有执行超过3秒的活动会话
db.currentOp({active : true,secs_running : { $gt : 3 },ns : /^db1\./}
)查询正在创建索引的操作
db.adminCommand({currentOp: true,$or: [{ op: command, command.createIndexes: { $exists: true } },{ op: none, msg : /^Index Build/ }]}
)db.killOp()方法MongoDB 4.0 会自动将kill 操作发送到其他shard 节点和mongos 实例。在mongos 实例上执行聚合管道命令$currentOp 来查找查询操作的shard 节点。
use admin
db.aggregate( [
{ $currentOp : { allUsers: true } },
{ $match : { op: getmore, command.collection: someCollection }}
] )使用命令行批量进行替换修改操作
调整前结果 db.fastdfs.find()
{ _id : ObjectId(65a9e4cb03febb56295304d4), fastdfs_url : http://1.1.1.1:8099/g10/M00/39/D9/CmQA6WEJ5NGAEtsoAABqUGfXheo386.wav }详细命令如下
db.ent_record_fastdfs_url.find({ $or: [{ fastdfs_url: { $regex: /1.1.1.1/ } }, { original_url: { $regex: /1.1.1.1/ } }] }).forEach(function(doc) {db.ent_record_fastdfs_url.update({ _id: doc._id },{$set: {fastdfs_url: doc.fastdfs_url.replace(/jsdx.ccod.com/g, 2.2.2.2),original_url: doc.original_url.replace(/jsdx.ccod.com/g, 2.2.2.2),// Add more fields here if needed}});
});调整后结果 db.fastdfs.find()
{ _id : ObjectId(65a9e4cb03febb56295304d4), fastdfs_url : http://2.2.2.2:8099/g10/M00/39/D9/CmQA6WEJ5NGAEtsoAABqUGfXheo386.wav }