mongodb副本集+分片搭建记录

mongodb副本集+分片搭建(2020-01-14):

1、架构简介
单机搭建mongodb伪分布式集群(副本集 + 分片),演示mongodb分布式集群的安装部署

2、环境准备
172.31.97.98物理机
centos7.6
mongodb-linux-x86_64-rhel70-3.4.20.tgz
参考文档:https://www.cnblogs.com/littleatp/p/8563273.html

3、配置说明
  1  configdb_33001.cnf  config副本集  33001
  2  configdb_33002.cnf  config副本集  33002
  3  configdb_33003.cnf  config副本集  33003
  4  shard1-1.cnf  shard分片1  31001
  5  shard1-2.cnf  shard分片1  31002
  6  shard1-3.cnf  shard分片1  31003
  7  shard2-1.cnf  shard分片2  32001
  8  shard2-2.cnf  shard分片2  32002
  9  shard2-3.cnf  shard分片2  32003
  10  mongos  mongos路由  34000
  11  keyfile keyfile文件

[root@172-31-97-98 data]# ps -ef |grep mongo
root      4985     1  0 10:09 ?        00:00:25 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33001.cnf
root      5363     1  0 10:10 ?        00:00:25 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33002.cnf
root      5485     1  0 10:10 ?        00:00:24 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33003.cnf
root     13607     1  0 10:41 ?        00:00:15 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-1.cnf
root     13677     1  0 10:41 ?        00:00:15 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-2.cnf
root     13758     1  0 10:41 ?        00:00:15 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-3.cnf
root     14858     1  0 10:46 ?        00:00:14 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-1.cnf
root     14924     1  0 10:47 ?        00:00:14 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-2.cnf
root     14985     1  0 10:47 ?        00:00:14 /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-3.cnf
root     18197     1  0 11:04 ?        00:00:04 /opt/zxmao/mongodb/bin/mongos -f /data/zxmao/mongodb/etc/mongos.cnf
root     20435 51356  0 11:16 pts/3    00:00:00 /opt/zxmao/mongodb/bin/mongo --port=34000
[root@172-31-97-98 data]# 

4、安装mongodb
cd /opt/zxmao
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.20.tgz
tar -xzvf mongodb-linux-x86_64-rhel70-3.4.20.tgz
mkdir -p /mongodb
mv /opt/zxmao/mongodb-linux-x86_64-rhel70-3.4.20/bin /opt/zxmao/mongodb

5、创建节点目录
mkdir -p /data/zxmao/mongodb/etc
mkdir -p /data/zxmao/mongodb/logs
mkdir -p /data/zxmao/mongodb/run
mkdir -p /data/zxmao/mongodb/data
及下述目录:
[root@172-31-97-98 data]# pwd
/data/zxmao/mongodb/data
[root@172-31-97-98 data]# ll
total 40
drwxr-xr-x. 7 root root 4096 Jan 14 11:38 configdb_33001
drwxr-xr-x. 7 root root 4096 Jan 14 11:38 configdb_33002
drwxr-xr-x. 7 root root 4096 Jan 14 11:39 configdb_33003
drwxr-xr-x. 2 root root 4096 Jan 14 09:41 mongos
drwxr-xr-x. 6 root root 4096 Jan 14 11:38 shard1-1
drwxr-xr-x. 6 root root 4096 Jan 14 11:39 shard1-2
drwxr-xr-x. 6 root root 4096 Jan 14 11:39 shard1-3
drwxr-xr-x. 6 root root 4096 Jan 14 11:39 shard2-1
drwxr-xr-x. 6 root root 4096 Jan 14 11:39 shard2-2
drwxr-xr-x. 6 root root 4096 Jan 14 11:38 shard2-3
[root@172-31-97-98 data]# 

6、搭建集群-config副本集
    a) 创建如下三个配置文件
    /data/zxmao/mongodb/etc/configdb_33001.cnf  
    /data/zxmao/mongodb/etc/configdb_33002.cnf  
    /data/zxmao/mongodb/etc/configdb_33003.cnf

    b) 启动config实例
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33001.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33002.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/configdb_33003.cnf
    
    c) 连接其中一个Config进程,执行副本集初始化
/opt/zxmao/mongodb/bin/mongo --port=33001
config={
    _id:"configdb", 
    members:[
        {_id:0, host:'172.31.97.98:33001'},
        {_id:1, host:'172.31.97.98:33002'}, 
        {_id:2, host:'172.31.97.98:33003'}
    ]};
#初始化副本集配置
rs.initiate(config);

7-1、搭建集群-创建shard1
    a)创建shard1分片如下三个配置文件
    /data/zxmao/mongodb/etc/shard1-1.cnf  
    /data/zxmao/mongodb/etc/shard1-2.cnf  
    /data/zxmao/mongodb/etc/shard1-3.cnf
    
    b)启动shard1分片实例
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-1.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-2.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard1-3.cnf
    
    c)连接其中一个shard1进程,执行初始化
    /opt/zxmao/mongodb/bin/mongo --port=31001
config={
    _id:"shard1", 
    members:[
        {_id:0, host:'172.31.97.98:31001'},
        {_id:1, host:'172.31.97.98:31002'}, 
        {_id:2, host:'172.31.97.98:31003'}
    ]};
rs.initiate(config);
    
7-2、搭建集群-创建shard2
    a)创建shard1分片如下三个配置文件
    /data/zxmao/mongodb/etc/shard2-1.cnf  
    /data/zxmao/mongodb/etc/shard2-2.cnf  
    /data/zxmao/mongodb/etc/shard2-3.cnf
    
    b)启动shard1分片实例
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-1.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-2.cnf
    /opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard2-3.cnf
    
    c)连接其中一个shard1进程,执行初始化
    /opt/zxmao/mongodb/bin/mongo --port=32001
config={
    _id:"shard2", 
    members:[
        {_id:0, host:'172.31.97.98:32001'},
        {_id:1, host:'172.31.97.98:32002'}, 
        {_id:2, host:'172.31.97.98:32003'}
    ]};
rs.initiate(config);

8、搭建集群-创建路由
    a) 创建mongos路由配置文件
    /data/zxmao/mongodb/etc/mongos.cnf
    
    b)启动mongos路由实例
    /opt/zxmao/mongodb/bin/mongos -f /data/zxmao/mongodb/etc/mongos.cnf
    
    c)串联路由服务器与分片副本集
    sh.addShard("shard1/172.31.97.98:31001,172.31.97.98:31002,172.31.97.98:31003")
    sh.addShard("shard2/172.31.97.98:32001,172.31.97.98:32002,172.31.97.98:32003")

9、搭建集群-初始化用户
use admin
db.createUser({
    user:'admin',pwd:'admin',
    roles:[
        {role:'clusterAdmin',db:'admin'},
        {role:'userAdminAnyDatabase',db:'admin'},
        {role:'dbAdminAnyDatabase',db:'admin'},
        {role:'readWriteAnyDatabase',db:'admin'}
]})

db.auth('admin','admin')

sh.status()

10、启动关闭
启动关闭
mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.
关闭时,直接killall杀掉所有进程

killall mongod
killall mongos

11、数据操作
创建appuser用户、为数据库实例appdb启动分片
use appdb
db.createUser({user:'app',pwd:'app',roles:[{role:'dbOwner',db:'appdb'}]})
sh.enableSharding("appdb")

创建集合book,为其执行分片初始化
use appdb
db.createCollection("book")
db.book.ensureIndex({createTime:1})
sh.shardCollection("appdb.book", {bookId:"hashed"}, false, { numInitialChunks: 4} )

继续往device集合写入1000W条记录,观察chunks的分布情况
use appdb
var cnt = 0;
for(var i=0; i<1000; i++){
    var dl = [];
    for(var j=0; j<100; j++){
        dl.push({
                "bookId" : "BBK-" + i + "-" + j,
                "type" : "Revision",
                "version" : "IricSoneVB0001",
                "title" : "Jackson's Life",
                "subCount" : 10,
                "location" : "China CN Shenzhen Futian District",
                "author" : {
                      "name" : 50,
                      "email" : "RichardFoo@yahoo.com",
                      "gender" : "female"
                },
                "createTime" : new Date()
            });
      }
      cnt += dl.length;
      db.book.insertMany(dl);
      print("insert ", cnt);
}

数据分布情况如下:
mongos> db.book.getShardDistribution()

Shard shard1 at shard1/172.31.97.98:31001,172.31.97.98:31002,172.31.97.98:31003
 data : 14.73MiB docs : 54866 chunks : 2
 estimated data per chunk : 7.36MiB
 estimated docs per chunk : 27433

Shard shard2 at shard2/172.31.97.98:32001,172.31.97.98:32002,172.31.97.98:32003
 data : 14.81MiB docs : 55134 chunks : 2
 estimated data per chunk : 7.4MiB
 estimated docs per chunk : 27567

Totals
 data : 29.55MiB docs : 110000 chunks : 4
 Shard shard1 contains 49.87% data, 49.87% docs in cluster, avg obj size on shard : 281B
 Shard shard2 contains 50.12% data, 50.12% docs in cluster, avg obj size on shard : 281B


mongos> 

12、总结
Mongodb集群架构由Mongos、Config副本集和多个分片组成;
安装过程中先初始化Config副本集、分片副本集,最后通过Mongos添加分片
Config副本集存储了集群访问的用户及角色权限,为了方便管理,可以给分片副本集添加本地用户
Mongodb提供了LocalException机制,首次安装数据库时可以在本机直接添加用户

13、新增分片shard3
a) 现状
mongos> db.runCommand({listshards:1})
{
    "shards" : [
        {
            "_id" : "shard1",
            "host" : "shard1/172.31.97.98:31001,172.31.97.98:31002,172.31.97.98:31003",
            "state" : 1
        },
        {
            "_id" : "shard2",
            "host" : "shard2/172.31.97.98:32001,172.31.97.98:32002,172.31.97.98:32003",
            "state" : 1
        }
    ],
    "ok" : 1
}
mongos> 

b) 创建数据目录及配置文件
mkdir /data/zxmao/mongodb/data/shart3-1
mkdir /data/zxmao/mongodb/data/shart3-2
mkdir /data/zxmao/mongodb/data/shart3-3
cp /data/zxmao/mongodb/data/shart1-1.cnf /data/zxmao/mongodb/data/shart3-1.cnf
cp /data/zxmao/mongodb/data/shart1-1.cnf /data/zxmao/mongodb/data/shart3-2.cnf
cp /data/zxmao/mongodb/data/shart1-1.cnf /data/zxmao/mongodb/data/shart3-3.cnf

c) 启动shard3分片实例
/opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard3-1.cnf
/opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard3-2.cnf
/opt/zxmao/mongodb/bin/mongod -f /data/zxmao/mongodb/etc/shard3-3.cnf


d) 连接其中一个shard3进程,执行初始化
/opt/zxmao/mongodb/bin/mongo --port=36001
config={
    _id:"shard3", 
    members:[
        {_id:0, host:'172.31.97.98:36001'},
        {_id:1, host:'172.31.97.98:36002'}, 
        {_id:2, host:'172.31.97.98:36003'}
    ]};
rs.initiate(config);

e) mongos串联路由服务器与分片副本集
sh.addShard("shard3/172.31.97.98:36001,172.31.97.98:36002,172.31.97.98:36003")

mongos> db.runCommand({listshards:1})
{
    "shards" : [
        {
            "_id" : "shard1",
            "host" : "shard1/172.31.97.98:31001,172.31.97.98:31002,172.31.97.98:31003",
            "state" : 1
        },
        {
            "_id" : "shard2",
            "host" : "shard2/172.31.97.98:32001,172.31.97.98:32002,172.31.97.98:32003",
            "state" : 1
        },
        {
            "_id" : "shard3",
            "host" : "shard3/172.31.97.98:36001,172.31.97.98:36002,172.31.97.98:36003",
            "state" : 1
        }
    ],
    "ok" : 1
}
mongos>

f) 数据分布现状
mongos> use appdb
switched to db appdb
mongos> 
mongos> db.book.getShardDistribution()

Shard shard1 at shard1/172.31.97.98:31001,172.31.97.98:31002,172.31.97.98:31003
 data : 53.69MiB docs : 199810 chunks : 2
 estimated data per chunk : 26.84MiB
 estimated docs per chunk : 99905

Shard shard2 at shard2/172.31.97.98:32001,172.31.97.98:32002,172.31.97.98:32003
 data : 26.92MiB docs : 100190 chunks : 2
 estimated data per chunk : 13.46MiB
 estimated docs per chunk : 50095

Totals
 data : 80.62MiB docs : 300000 chunks : 4
 Shard shard1 contains 66.6% data, 66.6% docs in cluster, avg obj size on shard : 281B
 Shard shard2 contains 33.39% data, 33.39% docs in cluster, avg obj size on shard : 281B


mongos> 

chunk迁移
关于平衡器的一些命令:
检查平衡器是否启用:sh.getBalancerState()
检查平衡器是否在运行:sh.isBalancerRunning()
禁用平衡器:sh.stopBalancer()
启用平衡器:sh.startBalancer()或sh.setBalancerState(true)
在一个集合禁用平衡器:sh.disableBalancing("databaseName.collectionName")
在一个集合启用平衡器:sh.enableBalancing("students.grades")
sh.status()
for(var i=1;i<=10;i++){sh.splitAt('appdb.book',{bookId:i*100000})}

sh.splitFind("test1.users003",{"name" : "u_100"})
db.adminCommand({moveChunk:'appdb.book',find:{bookId:"BBK-0-36"},to:'shard3'})
db.runCommand({moveChunk:'appdb.book',find:{"bookId" : "BBK-0-26"},to:'shard3'})

mongos    db.adminCommand({"moveChunk":"team.team","bounds":[{"_id":NumberLong("-6148914691236517200")},{"_id":NumberLong("-5380242722759272487")}],"to":"anav_team_1","_secondaryThrottle":true})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值