Mongodb集群部署-分片集群- Sharded Cluster

本文所需所有节点的配置文件 

Mongodb集群部署-分片集群所有节点的配置文件-MongoDB文档类资源-CSDN下载 

1.分片概念

分片 (sharding) 是一种跨多台机器分布数据的方法, MongoDB 使用分片来支持具有非常大的数据集和高吞吐量操作的部署。

2.分片集群包含的组件

MongoDB 分片群集包含以下组件:

  • 分片(存储):每个分片包含分片数据的子集。 每个分片都可以部署为副本集。

  • mongos (路由):mongos充当查询路由器,在客户端应用程序和分片集群之间提供接口。

  • config servers (”调度” 的配置):配置服务器存储群集的元数据和配置设置。 从MongoDB 3.4 开始,必须将配置服务器部署为副本集(CSRS)。 

 3.分片集群架构目标

三个分片节点副本集(3+3+3)+一个配置节点副本集(3)+两个路由节点(1),共13个服务节点。

4. 分片(存储)节点副本集的创建

所有的的配置文件都直接放到sharded_cluster 的相应的子目录下面,默认配置文件名字:mongod.conf

4.1 第一套副本集

分别在三台服务器创建存放数据和日志的目录

 #-----------myshardrs01
 服务器1
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/data/db \ &
 服务器2
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/data/db \ &
 服务器3
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/data/db

4.2第二套副本集

 #-----------myshardrs02
 服务器1
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27118/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27118/data/db \ &
 服务器2
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27018/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27018/data/db \ &
 服务器3
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27218/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs02_27218/data/db

4.2第三套副本集

 #-----------myshardrs03
 服务器1
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27218/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27218/data/db \ &
 服务器2
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27218/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27218/data/db \ &
 服务器3
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27018/log \ &
 mkdir -p /mongodb/sharded_cluster/myshardrs03_27018/data/db

4.3配置节点

#-----------configrs
 #建立数据节点data和日志目录
 服务器1
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/log \ &
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/data/db \ &
 服务器2
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/log \ &
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/data/db \ &
 服务器3
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/log \ &
 mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/data/db

4.4路由节点

 服务器随意
 mkdir -p /mongodb/sharded_cluster/mymongos_29019/log

4.5修改配置文件

新建或修改配置文件:

服务器1:主myshardrs01_27018:

vim /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs01_27018/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.pid"
 # network interfaces
 net:
   port: 27018
   bindIp: 127.0.0.1,服务所在ip地址。
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs01
 sharding:
   #分片角色
   clusterRole: shardsvr

服务器2:从myshardrs01_27118:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs01_27118/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.pid"
 # network interfaces
 net:
   port: 27118
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs01
 sharding:
   #分片角色
   clusterRole: shardsvr  
 

服务器3:仲裁myshardrs01_27118:

          
 
## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs01_27118/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.pid"
 # network interfaces
 net:
   port: 27118
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs01
 sharding:
   #分片角色
   clusterRole: shardsvr     

服务器1:从myshardrs02_27118:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs02_27118/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs02_27118/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27118/log/mongod.pid"
 # network interfaces
 net:
   port: 27118
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs02
 sharding:
   #分片角色
   clusterRole: shardsvr   
           

服务器2:主myshardrs02_27018:

## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs02_27018/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs02_27018/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27018/log/mongod.pid"
 # network interfaces
 net:
   port: 27018
   bindIp: 127.0.0.1,服务所在ip地址。
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs02
 sharding:
   #分片角色
   clusterRole: shardsvr  

服务器3:仲裁 myshardrs02_27218:

## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs02_27218/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs02_27218/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27218/log/mongod.pid"
 # network interfaces
 net:
   port: 27218
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs02
 sharding:
   #分片角色
   clusterRole: shardsvr         

服务器1:仲裁myshardrs03_27218:   

## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs03_27218/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs03_27218/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs03_27218/log/mongod.pid"
 # network interfaces
 net:
   port: 27218
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs03
 sharding:
   #分片角色
   clusterRole: shardsvr   
      

服务器2:从myshardrs03_27218:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs03_27218/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs03_27218/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs03_27218/log/mongod.pid"
 # network interfaces
 net:
   port: 27218
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs03
 sharding:
   #分片角色
   clusterRole: shardsvr              
 ​

服务器3:主myshardrs03_27018:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myshardrs03_27018/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myshardrs03_27018/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myshardrs03_27018/log/mongod.pid"
 # network interfaces
 net:
   port: 27018
   bindIp: 127.0.0.1,服务所在ip地址。
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myshardrs03
 sharding:
   #分片角色
   clusterRole: shardsvr              
 ​

服务器1:主myconfigrs_27019:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myconfigrs_27019/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.pid"
 # network interfaces
 net:
   port: 27019
   bindIp: 127.0.0.1,服务所在ip地址。
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myconfigrs
 sharding:
   #分片角色
   clusterRole: configsvr  
           
 ​

服务器2从 myconfigrs_27119:

 
## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myconfigrs_27119/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.pid"
 # network interfaces
 net:
   port: 27119
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myconfigrs
 sharding:
   #分片角色
   clusterRole: configsvr      
       
 ​

服务器3:从 myconfigrs_27219:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.log"
 ​
 # Where and how to store data.
 storage:
   dbPath: "/mongodb/sharded_cluster/myconfigrs_27219/data/db"
   journal:
     enabled: true
 # how the process runs
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.pid"
 # network interfaces
 net:
   port: 27219
   bindIp: 0.0.0.0
 #operationProfiling:
 replication:
   #副本集的名称
   replSetName: myconfigrs
 sharding:
   #分片角色
   clusterRole: configsvr             
 ​

mymongos_29019:

 ## content
 systemLog:
   destination: file
   logAppend: true
   path: "/mongodb/sharded_cluster/mymongos_29019/log/mongod.log"
 ​
 processManagement:
   fork: true
   pidFilePath: "/mongodb/sharded_cluster/mymongos_29019/log/mongod.pid"
 # network interfaces
 net:
   port: 29019
   bindIp: 0.0.0.0
 #operationProfiling:
 sharding:
   configDB: myconfigrs/192.168.1.188:29019,192.168.1.162:29119,192.168.1.165:29219

5.启动并初始化

5.1分片节点1

启动第一套

 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf

初始化第一套副本集和创建主节点

 /usr/local/mongodb/bin/mongo --host 192.168.1.128 --port 27018

rs.initiate()

添加副本节点 服务器2

 rs.add("192.168.1.162:27118")

添加仲裁节点 服务器3:

 rs.addArb("192.168.1.165:27118")

第一套初始化完成

5.2分片节点2

启动第二套

 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27118/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27018/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27218/mongod.conf

初始化第二套副本集

/usr/local/mongodb/bin/mongo --host 192.168.1.162 --port 27018
 rs.initiate()

添加副本节点 服务器1:

 rs.add("192.168.1.128:27118")

添加仲裁节点 服务器3:

 rs.addArb("192.168.1.165:27218")

第二套完成

5.3分片节点3

启动第三套

 
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs03_27218/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs03_27218/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs03_27018/mongod.conf

初始化第三套副本集

 /usr/local/mongodb/bin/mongo --host 192.168.1.165 --port 27018
 rs.initiate()

添加副本节点 服务器2:

 rs.add("192.168.1.162:27218")

添加仲裁节点 服务器1:

 rs.addArb("192.168.1.128:27218")

第三套完成

5.3配置节点

启动配置节点

 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
 ​
 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf

初始化配置节点

/usr/local/mongodb/bin/mongo --host 192.168.1.128 --port 27019

 rs.initiate()

添加副本节点 服务器2:

 rs.add("192.168.1.162:27119")

添加副本节点 服务器3:

 rs.add("192.168.1.165:27219")

5.4路由节点

启动mongos服务

 
/usr/local/mongodb/bin/mongos -f /mongodb/sharded_cluster/mymongos_29019/mongos.conf

连接

 /usr/local/mongodb/bin/mongo --host 192.168.1.162 --port 29019

添加分片

将第一套分片副本集添加进来:

 sh.addShard("myshardrs01/192.168.1.128:27018,192.168.1.162:27118,192.168.1.165:27118")

继续将第二,三套分片副本集添加进来:

 
sh.addShard("myshardrs02/192.168.1.128:27118,192.168.1.162:27018,192.168.1.165:27218")
 ​
 sh.addShard("myshardrs03/192.168.1.165:27018,192.168.1.162:27218,192.168.1.128:27218")

开启分片功能

数据库分片

 mongos> sh.enableSharding("数据库名称")

集合分片

 sh.shardCollection("数据库名.集合名", {"_id": "hashed"})
 如果集合有数据的话,先创建索引db.集合名.createIndex({_id:'hashed'})   创建哈希索引

模拟数据测试

 for(var i=1; i<=2000; i++){db.集合名.insert({_id: i+"", content: "xwj"+i})}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值