260_Mongodb_集合_分片集合2_集合创建_使用

举报
alexsully 发表于 2021/12/27 13:35:13 2021/12/27
【摘要】 分片集合创建使用

1 分片集群搭建及扩容

1.1 分片规划

10个实例:38017-38026

(1)configserver:
3台构成的复制集(1主两从,不支持arbiter)38018-38020
(2)shard节点:
sh1:38021-23 (1主两从,其中一个节点为arbiter,复制集名字sh1)
sh2:38024-26 (1主两从,其中一个节点为arbiter,复制集名字sh2)
(3)mongos
38017

1.2 配置过程

a. shard复制集配置:

1. 创建

mkdir -p /mongodb/38021/conf /mongodb/38021/log/mongodb/38021/data
mkdir -p /mongodb/38022/conf /mongodb/38022/log/mongodb/38022/data
mkdir -p /mongodb/38023/conf /mongodb/38023/log/mongodb/38023/data
mkdir -p /mongodb/38024/conf /mongodb/38024/log/mongodb/38024/data
mkdir -p /mongodb/38025/conf /mongodb/38025/log/mongodb/38025/data
mkdir -p /mongodb/38026/conf /mongodb/38026/log/mongodb/38026/data

2. 配置文件

sh1:
vi /mongodb/38021/conf/mongodb.conf
根据需求修改相应参数:
systemLog:
  destination: file
  path: /mongodb/38021/log/mongodb.log
  logAppend: true
storage:
  journal:
  enabled: true

  dbPath: /mongodb/38021/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
	
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38021
replication:
  oplogSizeMB: 2048
  replSetName: sh1
sharding:
  clusterRole: shardsvr
processManagement:
  fork: true

cp /mongodb/38021/conf/mongodb.conf /mongodb/38022/conf/
cp /mongodb/38021/conf/mongodb.conf /mongodb/38023/conf/
sed 's#38021#38022#g' /mongodb/38022/conf/mongodb.conf -i
sed 's#38021#38023#g' /mongodb/38023/conf/mongodb.conf -i
sh2:
vi /mongodb/38024/conf/mongodb.conf
根据需求修改相应参数:
systemLog:
  destination: file
  path: /mongodb/38021/log/mongodb.log
  logAppend: true
storage:
  journal:
  enabled: true

  dbPath: /mongodb/38024/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
	
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38024
replication:
  oplogSizeMB: 2048
  replSetName: sh2
sharding:
  clusterRole: shardsvr
processManagement:
  fork: true
  
cp /mongodb/38024/conf/mongodb.conf /mongodb/38025/conf/
cp /mongodb/38024/conf/mongodb.conf /mongodb/38026/conf/
sed 's#38024#38025#g' /mongodb/38025/conf/mongodb.conf -i
sed 's#38024#38026#g' /mongodb/38026/conf/mongodb.conf -i

3. 所有节点,并搭建复制集:

mongod -f /mongodb/38021/conf/mongodb.conf
mongod -f /mongodb/38022/conf/mongodb.conf
mongod -f /mongodb/38023/conf/mongodb.conf
mongod -f /mongodb/38024/conf/mongodb.conf
mongod -f /mongodb/38025/conf/mongodb.conf
mongod -f /mongodb/38026/conf/mongodb.conf
mongo --port 38021
use admin

config = {_id: 'sh1', members: [
{_id: 0, host:'10.0.0.51:38021'},
{_id: 1, host:'10.0.0.51:38022'},
{_id: 2, host:'10.0.0.51:38023',"arbiterOnly":true}]
}

rs.initiate(config)

mongo --port 38024
use admin

mongo --port 38024
use admin
config = {_id: 'sh2', members: [
{_id: 0, host:'10.0.0.51:38024'},
{_id: 1, host:'10.0.0.51:38025'},
{_id: 2, host:'10.0.0.51:38026',"arbiterOnly":true}]
}
rs.initiate(config)

b.config节点配置:

1. 目录创建:

mkdir -p /mongodb/38018/conf /mongodb/38018/log/mongodb/38018/data
mkdir -p /mongodb/38019/conf /mongodb/38019/log/mongodb/38019/data
mkdir -p /mongodb/38020/conf /mongodb/38020/log/mongodb/38020/data

2. 修改配置文件:

vi /mongodb/38018/conf/mongodb.conf

systemLog:
  destination: file
  path: /mongodb/38018/log/mongodb.log
  logAppend: true
storage:
  journal:
  enabled: true

  dbPath: /mongodb/38018/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
  collectionConfig:
      blockCompressor: zlib
  indexConfig:
      prefixCompression: true
	
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38018
replication:
  oplogSizeMB: 2048
  replSetName: configReplSet
sharding:
  clusterRole: configsvr
processManagement:
  fork: true
  
cp /mongodb/38018/conf/mongodb.conf /mongodb/38019/conf/
cp /mongodb/38018/conf/mongodb.conf /mongodb/38020/conf/
sed 's#38018#38019#g' /mongodb/38019/conf/mongodb.conf -i
sed 's#38018#38020#g' /mongodb/38020/conf/mongodb.conf -i

3. 启动节点,并配置复制集

mongod -f /mongodb/38018/conf/mongodb.conf
mongod -f /mongodb/38019/conf/mongodb.conf
mongod -f /mongodb/38020/conf/mongodb.conf

mongo --port 38018
use admin
config = {_id: 'configReplSet', configsvr: true,
 members: [
{_id: 0, host:'10.0.0.51:38018'},
{_id: 1, host:'10.0.0.51:38019'},
{_id: 2, host:'10.0.0.51:38020'}]
}
rs.initiate(config)

注:configserver 可以是一个节点,官方建议复制集。configserver不能有arbiter。
新版本中,要求必须是复制集。
注:mongodb 3.4之后,虽然要求config server为replica set,但是不支持arbiter

c. mongos节点配置:

1. 创建目录:
mkdir -p /mongodb/38017/conf /mongodb/38017/log
2配置文件:

vi /mongodb/38017/conf/mongos.conf
systemLog:
  destination: file
  path: /mongodb/38017/log/mongos.log
  logAppend: true
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38017
sharding:
  configDB:
    configReplSet/10.0.0.51:38018,10.0.0.51:38019,10.0.0.51:38020
processManagement:
  fork: true
  
3. 启动mongos
mongos -f /mongodb/38017/conf/mongos.conf

d. 分片集群操作:

连接到其中一个mongos(10.0.0.51),做以下配置
(1)连接到mongs的admin数据库
# su - mongod
$ mongo 10.0.0.51:38017/admin
(2)添加分片-配置副本集信息 包括副本集名称与副本集成员IP & port 
db.runCommand( { addshard :"sh1/10.0.0.51:38021,10.0.0.51:38022,10.0.0.51:38023",name:"shard1"} )
db.runCommand( { addshard :"sh2/10.0.0.51:38024,10.0.0.51:38025,10.0.0.51:38026",name:"shard2"} )

(3)列出分片
mongos> db.runCommand( { listshards : 1 } )
(4)整体状态查看
mongos> sh.status();

e. 使用分片集群

##RANGE分片配置及测试
test库下的vast大表进行手工分片

1 激活数据库分片功能 
sh.enablesharding("<数据库名>")

mongo --port 38017 admin
admin> ( { enablesharding : "数据库名称" } )
eg:
admin> db.runCommand( { enablesharding : "test" } )
admin> sh.enablesharding ("test")

2、指定分片建对集合分片 (将欲当成片键的字段设定成索引)
db.collection_name.ensureIndex({"<字段>":1})
eg:范围片键--创建索引(将分片键设置为索引)
use test
db.collection_namem.ensureIndex({"<字段>":1})
> db.vast.ensureIndex( { id: 1 } )

--开启分片对集合进行分片
sh.shardCollecion("<数据库名>.<集合名>",{"<索引字段>":1})
use admin
> db.runCommand( { shardcollection : "test.vast",key :{id: 1} } )
> db.shardcollection("test.vast", {_id: 1})
3、集合分片验证
admin> use test
test> for(i=1;i<500000;i++){db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
test> db.vast.stats()

f. Hash分片例子:

创建哈希索引
(1)对于test开启分片功能
mongo --port 38017 admin
use admin
admin> db.runCommand( { enablesharding : "alex" } )
admin> sh.enablesharding ("alex")

(2)对于alex库下的vast表建立hash索引
use alex
test> db.vast.ensureIndex( { id: "hashed" } )

(3)开启分片
use admin
admin > sh.shardCollection( "alex.vast", { _id: "hashed" })

(4)录入10w行数据测试
use alex
for(i=1;i<100000;i++){db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }


(5)hash分片结果测试
mongo --port 38021
use test
db.vast.count();
mongo --port 38024
use test
db.vast.count();

g. 分片的管理

1 判断是否Shard集群

admin> db.runCommand({ isdbgrid : 1})
2 列出所有分片信息
admin> db.runCommand({ listshards : 1})
3 列出开启分片的数据库
admin> use config
config> db.databases.find( { "partitioned": true } )
或者:
config> db.databases.find() //列出所有数据库分片情况

4、查看分片的片键
config> mongos> db.databases.find({"partitioned":true})
{ "_id" : "alex", "primary" : "shard_3", "partitioned" : true, "version" : { "uuid" : UUID("89cfcb5d-e9c9-4908-95ce-8717f1e2217c"), "lastMod" : 1 } }



5、查看分片的详细信息
admin> db.printShardingStatus()
or
admin> sh.status()
6、删除分片节点(谨慎)
(1)确认blance是否在工作
sh.getBalancerState()
//true 
(2)删除shard2节点(谨慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即触发blancer。

7、balancer操作

介绍:
mongos的一个重要功能,自动巡查所有shard节点上的chunk的情况,自动做
chunk迁移。
什么时候工作?
1、自动运行,会检测系统不繁忙的时候做迁移
2、在做节点删除的时候,立即开始迁移工作
3、balancer只能在预设定的时间窗口内运行
有需要时可以关闭和开启blancer(备份的时候)

mongos> sh.stopBalancer()
mongos> sh.startBalancer()
8、自定义 自动平衡进行的时间段
https://docs.mongodb.com/manual/tutorial/manage-sharded-cluster-balancer/#schedule-the-balancing-window
// connect to mongos

use config
sh.setBalancerState( true )
db.settings.update({ _id : "balancer" }, { $set : {
activeWindow : { start : "3:00", stop : "5:00" } } }, true
)
sh.getBalancerWindow()
sh.status()

查看分片状态信息

db.printShardingStatus()

sh.status()

初始化数据库时, databases 只有config数据库存在,如果建立其它databases,则databases中会存放其它信息

属性

说明

Shards

记录副本集,分片集群信息

Active mongoses

Mongos的版本及数量

Autosplit

Chunk自动切割的功能是否启动,默认启动

Balancer

平衡器的相关信息

Databases

数据库存放的相关信息

db.printShardingStatus("vast")
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("618xxxxxxxxxxxxxxxxxxxxd")
  }
  shards:
        {  "_id" : "shard_1",  "host" : "shard_1/25.213.0.249:8637,25.213.50.237:8637",  "state" : 1 }
        {  "_id" : "shard_2",  "host" : "shard_2/25.213.10.75:8637,25.213.27.9:8637",  "state" : 1 }
        {  "_id" : "shard_3",  "host" : "shard_3/25.213.2.43:8637,25.213.59.13:8637",  "state" : 1 }
  active mongoses:
        {  "_id" : "host-25-213-60-150:8635",  "advisoryHostFQDNs" : [ ],  "bindIps" : "25.213.60.150,192.168.0.132,192.168.0.14",  "mongoVersion" : "4.0.3",  "ping" : ISODate("2021-12-27T02:28:08.980Z"),  "up" : NumberLong(559676),  "waiting" : true }
        {  "_id" : "host-25-213-26-28:8635",  "advisoryHostFQDNs" : [ ],  "bindIps" : "25.213.26.28,192.168.0.242,192.168.0.160",  "mongoVersion" : "4.0.3",  "ping" : ISODate("2021-12-27T02:28:00.435Z"),  "up" : NumberLong(559667),  "waiting" : true }
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "alex",  "primary" : "shard_3",  "partitioned" : true,  "version" : {  "uuid" : UUID("89cfcb5d-e9c9-4908-95ce-8717f1e2217c"),  "lastMod" : 1 } }
                alex.vast
                        shard key: { "_id" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                shard_1 2
                                shard_2 2
                                shard_3 2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-6148914691236517204") } on : shard_1 Timestamp(1, 0)
                        { "_id" : NumberLong("-6148914691236517204") } -->> { "_id" : NumberLong("-3074457345618258602") } on : shard_1 Timestamp(1, 1)
                        { "_id" : NumberLong("-3074457345618258602") } -->> { "_id" : NumberLong(0) } on : shard_2 Timestamp(1, 2)
                        { "_id" : NumberLong(0) } -->> { "_id" : NumberLong("3074457345618258602") } on : shard_2 Timestamp(1, 3)
                        { "_id" : NumberLong("3074457345618258602") } -->> { "_id" : NumberLong("6148914691236517204") } on : shard_3 Timestamp(1, 4)
                        { "_id" : NumberLong("6148914691236517204") } -->> { "_id" : { "$maxKey" : 1 } } on : shard_3 Timestamp(1, 5)
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard_2 1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard_2 Timestamp(1, 0)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。