DevOps开发运维
成长之路

MongoDB Sharding Cluster 分片集群

规划

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

Shard节点配置过程

目录创建

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

修改配置文件

第一组复制集搭建:21-23(1主 1从 1Arb)
cat > /mongodb/38021/conf/mongodb.conf <<EOF
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.200,127.0.0.1
  port: 38021
replication:
  oplogSizeMB: 2048
  replSetName: sh1
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF

\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
---------------------------
replication:
oplogSizeMB: 2048 复制日志的大小
replSetName: sh1 复制集的名称
sharding:
clusterRole: shardsvr 集群角色
---------------------------
第二组复制集搭建:24-26(1主1从1Arb)
cat > /mongodb/38024/conf/mongodb.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/38024/log/mongodb.log 
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/38024/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 10.0.0.200,127.0.0.1
  port: 38024
replication:
  oplogSizeMB: 2048
  replSetName: sh2
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF

\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

启动所有节点

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 
[mongod@db01 ~]$ ps -ef |grep mongod
mongod 4661 1 3 14:30 ? 00:00:00 mongod -f /mongodb/38021/conf/mongodb.conf
mongod 4694 1 3 14:30 ? 00:00:00 mongod -f /mongodb/38022/conf/mongodb.conf
mongod 4722 1 2 14:30 ? 00:00:00 mongod -f /mongodb/38023/conf/mongodb.conf
mongod 4750 1 2 14:30 ? 00:00:00 mongod -f /mongodb/38024/conf/mongodb.conf
mongod 4778 1 3 14:30 ? 00:00:00 mongod -f /mongodb/38025/conf/mongodb.conf
mongod 4806 1 5 14:30 ? 00:00:00 mongod -f /mongodb/38026/conf/mongodb.conf

搭建复制集

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

config节点配置

目录创建

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

修改配置文件

cat > /mongodb/38018/conf/mongodb.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/38018/log/mongodb.conf
  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
EOF

\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

启动节点

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', 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

mongos节点配置

创建目录

mkdir -p /mongodb/38017/conf /mongodb/38017/log

配置文件

cat > /mongodb/38017/conf/mongos.conf <<EOF
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
EOF

启动mongos

mongos -f /mongodb/38017/conf/mongos.conf

分片集群添加节点

连接到其中一个mongos(10.0.0.200),做以下配置
(1)连接到mongs的admin数据库
su - mongod
[mongod@db01 ~]$ mongo 10.0.0.200:38017/admin
(2)添加分片
db.runCommand( { addshard : "sh1/10.0.0.200:38021,10.0.0.200:38022,10.0.0.200:38023",name:"shard1"} )
db.runCommand( { addshard : "sh2/10.0.0.200:38024,10.0.0.200:38025,10.0.0.200:38026",name:"shard2"} )
(3)列出分片
mongos> db.runCommand( { listshards : 1 } )
(4)整体状态查看
mongos> sh.status();

使用分片集群


RANGE分片配置及测试

test库下的vast大表进行手工分片
1、激活数据库分片功能
mongo --port 38017 admin
执行命令-开启库的分片功能:
mongos> db.runCommand( { enablesharding : "test" } )
2、指定分片键对集合分片
分片键就是按哪个列进行分片
### 创建索引
mongos>use test
mongos> db.vast.ensureIndex( { id: 1 } )
### 开启分片
mongos>use admin
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
3、集合分片验证
mongos> use test
mongos> for(i=1;i<1000000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
mongos> db.vast.stats()
分片结果测试
mongo --port 38021
sh1:PRIMARY>use test
sh1:PRIMARY> db.vast.count();
500001
mongo --port 38024
sh2:PRIMARY> use test
switched to db test
sh2:PRIMARY> db.vast.count();
499998

Hash分片例子

对oldboy库下的vast大表进行hash
创建哈希索引
(1)对于oldboy开启分片功能
mongo --port 38017 admin
use admin
admin> db.runCommand( { enablesharding : "oldboy" } )
(2)对于oldboy库下的vast表建立hash索引
use oldboy
oldboy> db.vast.ensureIndex( { id: "hashed" } )
(3)开启分片 
use admin
admin > sh.shardCollection( "oldboy.vast", { id: "hashed" } )
(4)录入10w行数据测试
use oldboy
for(i=1;i<100000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
(5)hash分片结果测试
mongo --port 38021
sh1:PRIMARY> use oldboy
switched to db oldboy
sh1:PRIMARY> db.vast.count();
50393
sh2:PRIMARY> use oldboy
switched to db oldboy
sh2:PRIMARY> db.vast.count();
49606

分片集群的查询及管理

判断是否Shard集群

mongos> db.runCommand({ isdbgrid : 1})

列出所有分片信息

mongos> db.runCommand({ listshards : 1})

列出开启分片的数据库

mongos> use config
mongos> db.databases.find( { "partitioned": true } )
{ "_id" : "test", "primary" : "shard2", "partitioned" : true }
{ "_id" : "oldboy", "primary" : "shard1", "partitioned" : true }
或者:
mongos> db.databases.find() //列出所有数据库分片情况

查看分片的片键

config> db.collections.find().pretty()
{
    "_id" : "test.vast",
    "lastmodEpoch" : ObjectId("58a599f19c898bbfb818b63c"),
    "lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
    "dropped" : false,
    "key" : {
        "id" : 1
    },
    "unique" : false
}

查看分片的详细信息

sh.status()

删除分片节点(谨慎)

(1)确认blance是否在工作
sh.getBalancerState()
(2)删除shard2节点(谨慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即触发blancer。
赞(0)

评论 抢沙发

评论前必须登录!

 

LNMP社群 不仅仅是技术

关于我们网站地图