码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB 分片集群

时间:2018-07-13 17:38:26      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:count   enabled   副本   启用   查看   storage   关闭   ble   des   

mongodb集群
 
组件:
mongos:请求分发
config server 配置服务器
shard 分片数据服务器
 
三台机器
192.168.174.128
192.168.174.129
192.168.174.130
服务器规划
 
技术分享图片技术分享图片
 
端口分配:
 
mongos:20000
config:21000
shard1:27001
shard2:27002
shard3:27003
 
服务器需要同步时间,否则测试不通过
 
128机器操作
 
1、下载官网:
 
2、解压
    tar -xf mongodb-linux-x86_64-rhel70-3.6.5.tgz && mv mongodb-linux-x86_64-rhel70-3.6.5 /usr/local/mongodb
 
3、加环境变量,方便操作(3台机器)
    vim /etc/profile
        export PATH=$PATH:/usr/local/mongodb/bin
    source /etc/profile
 
4、创建所需目录
 
    mkdir -p /usr/local/mongodb/{conf,config/{data,log},shard1/{data,log},shard2/{data,log},shard3/{data,log},mongos/log}
 
5、编写配置文件config.conf、shard1.conf、shard2.conf、shard3.conf、mongos.conf
 
====================================================================================
vim /usr/local/mongodb/conf/config.conf
systemLog:
  destination: file
  logAppend: true
  path: /usr/local/mongodb/config/log/configsvr.log
storage:
  dbPath: /usr/local/mongodb/config/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /usr/local/mongodb/config/log/configsvr.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 21000
  bindIp: 192.168.174.128
  maxIncomingConnections: 20000
replication:
  replSetName: configs
sharding:
  clusterRole: configsvr    
======================================================================================    
vim /usr/local/mongodb/conf/shard1.conf
systemLog:
  destination: file
  logAppend: true
  path: /usr/local/mongodb/shard1/log/shard1.log
storage:
  dbPath: /usr/local/mongodb/shard1/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /usr/local/mongodb/shard1/log/shard1.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27001
  bindIp: 192.168.174.128
  maxIncomingConnections: 20000
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
=======================================================================================
vim /usr/local/mongodb/conf/shard2.conf
systemLog:
  destination: file
  logAppend: true
  path:  /usr/local/mongodb/shard2/log/shard2.log
storage:
  dbPath:  /usr/local/mongodb/shard2/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath:  /usr/local/mongodb/shard2/log/shard2.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27002
  bindIp: 192.168.174.128
  maxIncomingConnections: 20000
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
=========================================================================================
vim /usr/local/mongodb/conf/shard3.conf
systemLog:
  destination: file
  logAppend: true
  path:  /usr/local/mongodb/shard3/log/shard3.log
storage:
  dbPath:  /usr/local/mongodb/shard3/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath:  /usr/local/mongodb/shard3/log/shard3.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27003
  bindIp: 192.168.174.128
  maxIncomingConnections: 20000
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
===========================================================================================
vim /usr/local/mongodb/conf/mongos.conf
systemLog:
  destination: file
  logAppend: true
  path: /usr/local/mongodb/mongos/log/mongos.log
processManagement:
  fork: true
  pidFilePath: /usr/local/mongodb/mongos/log/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 20000
  bindIp: 192.168.174.128
  maxIncomingConnections: 20000
sharding:
  configDB: configs/192.168.174.128:21000,192.168.174.130:21000,192.168.174.131:21000
============================================================================================
 
6、将mongodb目录scp到其两台服务器
    scp -r /usr/local/mongodb 192.168.174.130:/usr/local/mongodb
    scp -r /usr/local/mongodb 192.168.174.131:/usr/local/mongodb
 
    **注意:130和131节点,配置文件中的bindIp需要修改为对应的IP
 
7、创建用户,授权(三台机器)
    useradd mongod
    chown -R mongod:mongod /usr/local/mongodb/
 
============================================================================
8、启动配置服务器(3台)
    mongod -f /usr/local/mongodb/conf/config.conf
 
9、初始化副本集(登陆任意一个实例即可)
    #连接
        mongo 192.168.174.128:21000
    #初始化副本集        
rs.initiate(
  {
    _id: "configs",
    configsvr: true,
    members: [
      { _id : 0, host : "192.168.174.128:21000" },
      { _id : 1, host : "192.168.174.130:21000" },
      { _id : 2, host : "192.168.174.131:21000" }
    ]
  }
)
 
    #查看状态
    rs.status();
==============================================================================
10、启动shard1、shard2、shard3(3台)
    mongod -f /usr/local/mongodb/conf/shard1.conf
    mongod -f /usr/local/mongodb/conf/shard2.conf
    mongod -f /usr/local/mongodb/conf/shard3.conf
 
11、初始化副本集(在一台执行即可)
 
    #连接
        mongo 192.168.174.130:27001
    #初始化副本集        
rs.initiate(
  {
    _id : "shard1",
    members: [
      { _id : 0, host : "192.168.174.128:27001",priority:2},
      { _id : 1, host : "192.168.174.130:27001",priority:1},
      { _id : 2, host : "192.168.174.131:27001",arbiterOnly: true}
    ]
  }
)
    #查看状态
    rs.status();
 
    #连接
        mongo 192.168.174.131:27002
    #初始化副本集        
rs.initiate(
  {
    _id : "shard2",
    members: [
      { _id : 0, host : "192.168.174.128:27002",arbiterOnly: true},
      { _id : 1, host : "192.168.174.130:27002",priority:2},
      { _id : 2, host : "192.168.174.131:27002",priority:1}
    ]
  }
)
    #查看状态
    rs.status();
 
    #连接
        mongo 192.168.174.128:27003
    #初始化副本集        
rs.initiate(
  {
    _id : "shard3",
    members: [
      { _id : 0, host : "192.168.174.128:27003",priority:1},
      { _id : 1, host : "192.168.174.130:27003",arbiterOnly: true},
      { _id : 2, host : "192.168.174.131:27003",priority:2}
    ]
  }
)
 
    #查看状态
    rs.status();
===============================================================================
12、启动mongos(3台)
    mongos -f /usr/local/mongodb/conf/mongos.conf
 
13、启用分片(登陆任意一个实例)
        mongo 192.168.174.128:20000
        
        use  admin        
        sh.addShard("shard1/192.168.174.128:27001,192.168.174.130:27001,192.168.174.130:27001");
        sh.addShard("shard2/192.168.174.128:27002,192.168.174.130:27002,192.168.174.130:27002");
        sh.addShard("shard3/192.168.174.128:27003,192.168.174.130:27003,192.168.174.130:27003");
    
        #查看集群状态
        sh.status();
 
===============================================================================
 
14、测试
        
        设置chunk大小
            use config
            db.settings.save({ "_id" : "chunksize", "value" : 1 })
            #为方便测试设为1M,否则需要插入大量数据才会分片存储
 
        登陆
            mongo 192.168.127.128:20000
        指定testdb分片生效
            use admin;
            db.runCommand({ enablesharding :"testdb"});
        指定需要分片的集合和片键
            db.runCommand({ shardcollection : "testdb.table1",key : {id: 1} });
 
               use testdb;
        插入数据
            for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"});
        查看是分片情况
               db.table1.stats();
           可以看到数据分到3个分片,各自分片数量为: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已经成功了!
 
启动关闭
 
    mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.
 
        mongod -f /usr/local/mongodb/conf/config.conf
        mongod -f /usr/local/mongodb/conf/shard1.conf
        mongod -f /usr/local/mongodb/conf/shard2.conf
        mongod -f /usr/local/mongodb/conf/shard3.conf
        mongos -f /usr/local/mongodb/conf/mongos.conf
 
关闭时,直接killall杀掉所有进程(测试使用)
 
    pkill mongo

MongoDB 分片集群

标签:count   enabled   副本   启用   查看   storage   关闭   ble   des   

原文地址:https://www.cnblogs.com/zuimao/p/9305908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!