mongoDB集群之分片集群

ps auxf|grep mongo |grep -v grep|xargs kill -9 杀死所有redis的进程

分片是把大型数据集进行分区成更小的可管理的片,这些数据片分散到不同的mongoDB节点,
这些节点组成了分片集群。
 
 
 
总体分为3块  分片服务器  配置服务器  路由器(mongos)组成:
 
 mongoDB集群之分片集群
 
如果有旧的数据先清除:

rm -rf /soft/mongosplit/node27021/db/* 
rm -rf /soft/mongosplit/node27031/db/*
rm -rf /soft/mongosplit/node27041/db/*

rm -rf /soft/mongosplit/node27022/db/*
rm -rf /soft/mongosplit/node27032/db/*
rm -rf /soft/mongosplit/node27042/db/*

rm -rf /soft/mongosplit/node27023/db/*
rm -rf /soft/mongosplit/node27033/db/*
rm -rf /soft/mongosplit/node27043/db/*

rm -rf /soft/mongosplit/node27025/db/*
rm -rf /soft/mongosplit/node27035/db/*
rm -rf /soft/mongosplit/node27045/db/* 

 
1:分片服务器配置 全部3个分片 (由于空间不足,分片放一台,配置和路由放另一台)
还是要在mongosplit目录下复制13个节点:先配置好一个节点再复制。
在第一个节点上创建db,logs两个目录: mkdir node27021/{db,logs}
 
 
1》分片1:每个分片3台作为一个可复制集
mongoDB集群之分片集群
 
 
mongoDB集群之分片集群
 
 
 
首先: vim mgdb.conf 内容如下:
 


replication:
  replSetName: configRS
  oplogSizeMB: 50

storage:
    dbPath: "/usr/local/soft/mongosplit/note27021/db"
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27021/logs/mongodb.log"
net:
    port: 27021
    bindIp: 127.0.0.1,192.168.42.100
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   clusterRole: shardsvr
 

mongoDB集群之分片集群

同一片内只需要修改IP 和 端口号 和 文件 路径

不同片内 还要改这个 :replSetName: configRS2  或者configRS3 ,如下:

replication:
  replSetName: configRS
  oplogSizeMB: 50

storage:
    dbPath: "/usr/local/soft/mongosplit/note27022/db"
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27022/logs/mongodb.log"
net:
    port: 27022
    bindIp: 127.0.0.1,192.168.42.100
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   clusterRole: shardsvr

 

启动:

/usr/local/soft/mongosplit/note27021/bin/mongod --config /usr/local/soft/mongosplit/note27021/mgdb.conf &
/usr/local/soft/mongosplit/note27031/bin/mongod --config /usr/local/soft/mongosplit/note27031/mgdb.conf &
/usr/local/soft/mongosplit/note27041/bin/mongod --config /usr/local/soft/mongosplit/note27041/mgdb.conf &

 

配置复制集

./mongo --port 27021
/usr/local/soft/mongosplit/note27021/bin/mongo --port 27021

use admin

rs.initiate({
    _id: "configRS",
    version: 1,
    members: [{
        _id: 0,
        host: "192.168.42.100:27021"
    }]
});

rs.add("192.168.42.100:27031");  //有几个节点就执行几次方法
rs.add("192.168.42.100:27041");

 

之后第2片如下:


replication:
 
replSetName: configRS2
  oplogSizeMB: 50

storage:
    dbPath: "/usr/local/soft/mongosplit/note27042/db"
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27042/logs/mongodb.log"
net:
    port: 27042
    bindIp: 127.0.0.1,192.168.42.100
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   clusterRole: shardsvr

启动:

/usr/local/soft/mongosplit/note27022/bin/mongod --config /usr/local/soft/mongosplit/note27022/mgdb.conf &
/usr/local/soft/mongosplit/note27032/bin/mongod --config /usr/local/soft/mongosplit/note27032/mgdb.conf &
/usr/local/soft/mongosplit/note27042/bin/mongod --config /usr/local/soft/mongosplit/note27042/mgdb.conf &

配置复制集

./mongo --port 27022
/usr/local/soft/mongosplit/note27032/bin/mongo --port 27032

use admin

rs.initiate({
    _id: "configRS2",
    version: 1,
    members: [{
        _id: 0,
        host: "192.168.42.100:27022"
    }]
});

rs.add("192.168.42.100:27032");  //有几个节点就执行几次方法
rs.add("192.168.42.100:27042");

 

之后第3片如下:


replication:
  replSetName: configRS3
  oplogSizeMB: 50

storage:
    dbPath: "/usr/local/soft/mongosplit/note27043/db"
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27043/logs/mongodb.log"
net:
    port: 27043
    bindIp: 127.0.0.1,192.168.42.100
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   clusterRole: shardsvr

 

启动:

/usr/local/soft/mongosplit/note27023/bin/mongod --config /usr/local/soft/mongosplit/note27023/mgdb.conf &
/usr/local/soft/mongosplit/note27033/bin/mongod --config /usr/local/soft/mongosplit/note27033/mgdb.conf &
/usr/local/soft/mongosplit/note27043/bin/mongod --config /usr/local/soft/mongosplit/note27043/mgdb.conf &


配置复制集

./mongo --port 27023
./usr/local/soft/mongosplit/note27023/bin/mongo --port 27023

use admin

rs.initiate({
    _id: "configRS3",
    version: 1,
    members: [{
        _id: 0,
        host: "192.168.42.100:27023"
    }]
});

rs.add("192.168.42.100:27033");  //有几个节点就执行几次方法
rs.add("192.168.42.100:27043");

以上完成分片的:




配置 Config 服务器 (3台)
节点下 vim mgdb.conf配置如下:
 

replication:
  replSetName: editRS
  oplogSizeMB: 50

storage:
    dbPath: "/usr/local/soft/mongosplit/note27045/db"
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27045/logs/mongodb.log"
net:
    port: 27045
    bindIp: 127.0.0.1,192.168.42.101
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   clusterRole: configsvr

注意: replSetName: editRS 名称不一样了
 
也是一样 要启动:
/usr/local/soft/mongosplit/note27025/bin/mongod --config /usr/local/soft/mongosplit/note27025/mgdb.conf &
/usr/local/soft/mongosplit/note27035/bin/mongod --config /usr/local/soft/mongosplit/note27035/mgdb.conf &
/usr/local/soft/mongosplit/note27045/bin/mongod --config /usr/local/soft/mongosplit/note27045/mgdb.conf &
 
也是一样 要配置可复制集:
 

./mongo --port 27025
/usr/local/soft/mongosplit/note27025/bin/mongo --port 27025

use admin

rs.initiate({
    _id: "editRS",
    version: 1,
    members: [{
        _id: 0,
        host: "192.168.42.101:27025"
    }]
});

rs.add("192.168.42.101:27035");  //有几个节点就执行几次方法
rs.add("192.168.42.101:27045");

 


mongos 路由器  一台(27066)
 
节点下 vim mgdb.conf配置如下:
systemLog:
    destination: file
    path: "/usr/local/soft/mongosplit/note27066/logs/mongodb.log"
net:
    port: 27055
    bindIp: 127.0.0.1,192.168.42.101
processManagement:
    fork: true
setParameter:
    enableLocalhostAuthBypass: false
sharding:
   configDB:editRS/192.168.42.101:27025,192.168.42.101:27035,192.168.42.101:27045
 
 
指定:configDB:editRS
 
然后 启动之后 添加分区:
 
/usr/local/soft/mongosplit/note27066/bin/mongos --config /usr/local/soft/mongosplit/note27066/mgdb.conf &
 

use admin;

sh.addShard("configRS/192.168.42.100:27021,192.168.42.100:27031,192.168.42.100:27041");

sh.addShard("configRS2/192.168.42.100:27022,192.168.42.100:27032,192.168.42.100:27042"); 

//configRS 这个是复制集的名称
sh.addShard("configRS3/192.168.42.100:27023,192.168.42.100:27033,192.168.42.100:27043");

 

对lison数据库启动分片: sh.enableSharding("lison");

对 orders 集合进行分片,分片键为 sh.shardCollection("lison.orders",{"useCode":"hashed"});

 

删除分片
db.runCommand( { removeShard: "xxx" } )
 
查看状态:
sh.status();
 
mongoDB集群之分片集群


可以了,就开始撸代码测试:

配置链接地址为路由地址和端口:

mongoDB集群之分片集群

mongoDB集群之分片集群

插入一万条数据,查看分区如下:

mongoDB集群之分片集群

总共10000条:

mongoDB集群之分片集群

mongoDB集群之分片集群

mongoDB集群之分片集群

3个ConfigRS加起来刚好10000条.

 

 

启动时报错,大部分都是mgdb.conf的路径等有问题,赋值前面还得有个空格,如下:

mongoDB集群之分片集群

 

mongoDB集群之分片集群