Mongo - 04 Mongo cluster fragmentation

First, the concept of fragmentation

1. With a replica set, why do we need fragmentation?

Replica set resource utilization is not high
fragmentation can improve resource utilization

2. slice disadvantages

The machine needs more ideally
configuration and operation and maintenance become complicated and difficult
to plan well is particularly important, want to change the architecture becomes very difficult once established

Second, the fragmentation works

1. Routing Service mongos

Routing service available agent, the request for the user data destination tile shard

2. Data node shard

Node is responsible for processing data, each shard portion of the sheet is partial cluster

3. The slice configuration information server config

Save data distribution on which shard
to save the configuration letter to all shard of
providing access service to mongos

4. The key sheet

Stored data to distinguish between rules which shard of the
piece is the key index
to select key piece of authority:
to be frequently accessed fields in
the index field is large enough base

分类:
区间片键
id  name   host  sex 
1   zhang  SH    boy
2   ya     BJ    boy 
3   yaya   SZ    girl

如果以id作为片键:
索引:id
1-100   shard1
100-200 shard2
200-300 shard3 
300-+无穷 shard4

如果以host作为片键:
SH  shard1
BJ  shard2 
SZ  shard3 

hash片键:
足够平均,足够随机
id  name   host  sex 
1   zhang  SH    boy
2   ya     BJ    boy 
3   yaya   SZ    girl

如果以id作为片键:
索引:id
1   hash 之后 shard2 
2   hash 之后 shard3 
3   hash 之后 shard1 

Three, IP port planning directory

1.IP port plan

1.IP端口规划
db01    10.0.0.51   
        Shard1_Master   28100
        Shard2_Slave    28200
        Shard3_Arbiter  28300
        Config Server   40000
        mongos Server   60000
        
db02    10.0.0.52   
        Shard2_Master   28100
        Shard3_Slave    28200
        Shard1_Arbiter  28300
        Config Server   40000
        mongos Server   60000
        
db03    10.0.0.53   
        Shard3_Master   28100
        Shard1_Slave    28200
        Shard2_Arbiter  28300
        Config Server   40000
        mongos Server   60000
        

2. Contents Planning

服务目录:
/opt/master/{conf,log,pid}
/opt/slave//{conf,log,pid}
/opt/arbiter/{conf,log,pid}
/data/config/{conf,log,pid}
/data/mongos/{conf,log,pid}

数据目录:
/data/master/
/data/slave/
/data/arbiter/
/data/config/s

Fourth, the structures of the replica set cluster fragmentation step

1. Install the software

注意:三台服务器都操作!!!
tar xf mongodb-linux-x86_64-3.6.13.tgz -C /opt/
ln -s /opt/mongodb-linux-x86_64-3.6.13 /opt/mongodb
echo 'export PATH=$PATH:/opt/mongodb/bin' >> /etc/profile
source /etc/profile

2. Create a directory

注意:三台服务器都操作!!!
mkdir -p /opt/master/{conf,log,pid}
mkdir -p /opt/slave/{conf,log,pid}
mkdir -p /opt/arbiter/{conf,log,pid}

mkdir -p /data/master/
mkdir -p /data/slave/
mkdir -p /data/arbiter/

3. Create a configuration file


db01 create a profile

#master节点配置文件
cat >/opt/master/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/master/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/master/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/master/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28100
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard1

sharding:
  clusterRole: shardsvr
EOF

#slave节点配置文件
cat >/opt/slave/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/slave/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/slave/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/slave/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28200
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard2

sharding:
  clusterRole: shardsvr
EOF

#aribiter节点配置文件
cat >/opt/arbiter/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/arbiter/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/arbiter/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/arbiter/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28300
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard3

sharding:
  clusterRole: shardsvr
EOF

##############################################################################

db02 create a profile

#master节点配置文件
cat >/opt/master/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/master/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/master/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/master/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28100
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard2

sharding:
  clusterRole: shardsvr
EOF

#slave节点配置文件
cat >/opt/slave/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/slave/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/slave/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/slave/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28200
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard3

sharding:
  clusterRole: shardsvr
EOF

#aribiter节点配置文件
cat >/opt/arbiter/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/arbiter/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/arbiter/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/arbiter/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28300
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard1

sharding:
  clusterRole: shardsvr
EOF

db03 create a profile

#master节点配置文件
cat >/opt/master/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/master/log/mongod.log

storage:
  journal:
    enabled: true
  dbPath: /data/master/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/master/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28100
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard3

sharding:
  clusterRole: shardsvr
EOF

#slave节点配置文件
cat >/opt/slave/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/slave/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/slave/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/slave/pid/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28200
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard1

sharding:
  clusterRole: shardsvr
EOF

#aribiter节点配置文件
cat >/opt/arbiter/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/arbiter/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/arbiter/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/arbiter/pid/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28300
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  oplogSizeMB: 1024 
  replSetName: shard2

sharding:
  clusterRole: shardsvr
EOF

4. Optimize warning

注意!三台服务器都操作!!!
useradd mongod -s /sbin/nologin -M 
echo "never"  > /sys/kernel/mm/transparent_hugepage/enabled
echo "never"  > /sys/kernel/mm/transparent_hugepage/defrag

5. Start Services

mongod -f /opt/master/conf/mongod.conf 
mongod -f /opt/slave/conf/mongod.conf 
mongod -f /opt/arbiter/conf/mongod.conf
netstat -lntup|grep mongod

6. initialization replica set

#db01 master节点初始化shard1的副本
mongo --port 28100
rs.initiate()
rs.add("10.0.0.53:28200")
rs.addArb("10.0.0.52:28300")

#db02 master节点初始化shard2的副本
mongo --port 28100
config = {
    _id:"shard2", 
    members:[
        {_id:0,host:"10.0.0.52:28100"},
        {_id:1,host:"10.0.0.51:28200"},
        {_id:2,host:"10.0.0.53:28300",arbiterOnly:true},
    ] 
}
rs.initiate(config)

#db03 master节点初始化shard3的副本
mongo --port 28100
config = {
    _id:"shard3", 
    members:[
        {_id:0,host:"10.0.0.53:28100"},
        {_id:1,host:"10.0.0.52:28200"},
        {_id:2,host:"10.0.0.51:28300",arbiterOnly:true},
    ] 
}
rs.initiate(config)

7. Check command

mongo --port 28100
rs.status()

V. fragmentation step cluster structures config

1. Create a directory

mkdir -p /opt/config/{conf,log,pid}
mkdir -p /data/config/

2. Create a profile

cat >/opt/config/conf/mongod.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/config/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/config/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/config/pid/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 40000
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  replSetName: configset

sharding:
  clusterRole: configsvr
EOF

3. Start

mongod -f /opt/config/conf/mongod.conf

4. Initialize the replica set

mongo localhost:40000
rs.initiate({
    _id:"configset", 
    configsvr: true,
    members:[
        {_id:0,host:"10.0.0.51:40000"},
        {_id:1,host:"10.0.0.52:40000"},
        {_id:2,host:"10.0.0.53:40000"},
    ] })

5. Check

rs.status()

Six, mongos configuration

1.创建目录
mkdir -p /opt/mongos/{conf,log,pid}

2.创建配置文件
cat >/opt/mongos/conf/mongos.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/mongos/log/mongos.log

processManagement:
  fork: true
  pidFilePath: /opt/mongos/pid/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 60000
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

sharding:
  configDB: 
    configset/10.0.0.51:40000,10.0.0.52:40000,10.0.0.53:40000
EOF

3.启动
mongos -f /opt/mongos/conf/mongos.conf

4.登录
mongo --port 60000

Seven slice configuration

#1.区间分片:
数据库开启分片
mongo localhost:60000 .
#先切换到admin库下
use admin 
db.runCommand( { enablesharding : "test" } )

#创建索引
mongo localhost:60000 
#进到test库
mongos> use test
switched to db test

db.range.ensureIndex( { id: 1 } )

#对集合开启分片,片键是id
use admin
db.runCommand( { shardcollection : "test.range",key : {id: 1} } )

#插入测试数据
use test
for(i=1;i<10000;i++){ db.range.insert({"id":i,"name":"shanghai","age":28,"date":new Date()}); }
db.range.stats()
db.range.count()

#hash分片:
数据库开启分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "oldboy" } )

#创建索引
use oldboy
db.hash.ensureIndex( { id: "hashed" } )

#集合开启哈希分片
use admin
sh.shardCollection( "oldboy.hash", { id: "hashed" } )

#生成测试数据
use oldboy
for(i=1;i<10000;i++){ db.hash.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }

#分片验证
shard1
mongo db01:28100
use oldboy
db.hash.count()
33755


shard2
mongo db02:28100
use oldboy
db.hash.count()
33142


shard3
mongo db03:28100
use oldboy
db.hash.count()
33102

Eight cluster fragmentation common management commands

1列出分片所有详细信息
db.printShardingStatus()
sh.status()

2列出所有分片成员信息
use admin
db.runCommand({ listshards : 1})

3列出开启分片的数据库
use config
db.databases.find({"partitioned": true })

4查看分片的片键
use config
db.collections.find().pretty()

5查看集合的分片信息
use test
db.getCollection('range').getShardDistribution()
db.getCollection('hash').getShardDistribution()

Guess you like

Origin www.cnblogs.com/gongjingyun123--/p/12088328.html