mongoDB集群之分片集群
ps auxf|grep mongo |grep -v grep|xargs kill -9 杀死所有redis的进程
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/*
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
同一片内只需要修改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台)
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
/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");
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
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"});
可以了,就开始撸代码测试:
配置链接地址为路由地址和端口:
插入一万条数据,查看分区如下:
总共10000条:
3个ConfigRS加起来刚好10000条.
启动时报错,大部分都是mgdb.conf的路径等有问题,赋值前面还得有个空格,如下: