分片(sharding)是一种跨多台机器分布数据的方法, MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作的部署。
换句话说:分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程。有时也用分区(partitioning)来表示这个概念。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存更多的数据,处理更多的负载。
具有大型数据集或高吞吐量应用程序的数据库系统会挑战单个服务器的容量。例如,高查询率会耗尽服务器的CPU容量。工作集大小大于系统的RAM会强调磁盘驱动器的I / O容量。
有两种解决系统增长的方法:垂直扩展和水平扩展。
MongoDB支持通过分片进行水平扩展。
分片为应对高吞吐量与大数据提供了方法
cd /data/logs/mongodb
touch mongodb-2702{
1..3}.log
touch mongodb-3701{
7..9}.log
touch mongodb-47017.log
[root@mongodb mongodb]# ls
mongodb-27017.log mongodb-27019.log mongodb-27021.log mongodb-27023.log mongodb-37018.log mongodb-47017.log
mongodb-27018.log mongodb-27020.log mongodb-27022.log mongodb-37017.log mongodb-37019.log
chmod 777 ./*
mkdir mongodb-2702{
1..3}
mkdir mongodb-3701{
7..9}
mkdir mongodb-47017
cp -a mongod-27017.conf mongod-27021.conf
注意两个复制集的名字不能相同
第一个复制集
sharding:
clusterRole: shardsvr
replication:
oplogSizeMB: 2048
replSetName: maomao
第二个复制集
sed -ri -e 's/27017/27021/g' -e 's/maomao/zhuzhu/g' mongod-27021.conf
sed -ri -e 's/27017/27022/g' -e 's/maomao/zhuzhu/g' mongod-27022.conf
sed -ri -e 's/27017/27023/g' -e 's/maomao/zhuzhu/g' mongod-27023.conf
只在切片服务器上添加
sharding:
clusterRole: shardsvr
sed -ri -e 's/27017/37017/g' mongod-37017.conf
vim mongod-37017.conf
sharding:
clusterRole: configsvr
replication:
oplogSizeMB: 2048
replSetName: configReplSet
sed -ri -e 's/27017/47017/g' mongod-47017.conf
添加config server的地址和端口
sharding:
configDB: configReplSet/192.168.188.101:37017,192.168.188.101:37018,192.168.188.101:37019
并且将存储路注销
#storage:
# dbPath: /data/mongodb-47017
# journal:
# enabled: true
将所有节点打开
mongod -f /mongod-27017.conf
mongod -f mongod-27017.conf
mongod -f mongod-27018.conf
mongod -f mongod-27019.conf
mongod -f mongod-27020.conf
mongod -f mongod-27021.conf
mongod -f mongod-27022.conf
mongod -f mongod-27023.conf
mongod -f mongod-37017.conf
mongod -f mongod-37018.conf
mongod -f mongod-37019.conf
ss -ntl
进入两个主节点
第一个:
config={
_id:"maomao",members:[{
_id:0,host:"192.168.188.101:27017"},{
_id:1,host:"1192.168.188.101:27018"},{
_id:2,host:"192.168.188.101:27019"}]}
rs.initiate(config)
{
"ok" : 1 }
config={
_id:"zhuzhu",members:[{
_id:0,host:"192.168.188.101:27021"},{
_id:1,host:"192.168.188.101:27022"},{
_id:2,host:"192.16
8.188.101:27023"}]}
{
"_id" : "zhuzhu",
"members" : [
{
"_id" : 0,
"host" : "192.168.188.101:27021"
},
{
"_id" : 1,
"host" : "192.168.188.101:27022"
},
{
"_id" : 2,
"host" : "192.168.188.101:27023"
}
]
}
> rs.initiate(config)
{
"ok" : 1 }
mongo --host 192.168.188.101 --port 37017
> config={
_id:"configReplSet",members:[{
_id:0,host:"192.168.188.101:37017"},{
_id:1,host:"192.168.188.101:37018"},{
_id:2,host:
"192.168.188.101:37019"}]}
{
"_id" : "configReplSet",
"members" : [
{
"_id" : 0,
"host" : "192.168.188.101:37017"
},
{
"_id" : 1,
"host" : "192.168.188.101:37018"
},
{
"_id" : 2,
"host" : "192.168.188.101:37019"
}
]
}
> rs.initiate(config)
{
"ok" : 1 }
路由命令较为特殊,使用的是mongos而非mongod
whatis mongos
mongos (1) - MongoDB Sharded Cluster Query Router
whatis mongo
mongo (1) - MongoDB Shell
whatis mongod
mongod (1) - MongoDB Server
mongos -f /usr/local/mongodb/bin/mongod-47017.conf
mongo --host 192.168.188.101 --port 47017
进入路由数据库,可以看到路由数据库的名字为mongos
mongos>
切换到admin
mongos> use admin
switched to db admin
添加分片信息
mongos> db.runCommand({
addshard:"maomao/192.168.188.101:27017,192.168.188.101:27018,192.168.188.101:27019,1
92.168.188.101:27020",name:"shard1"})
{
"shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({
addshard:"zhuzhu/192.168.188.101:27021,192.168.188.101:27022,192.168.188.101:27023"
,name:"shard2"})
{
"shardAdded" : "shard2", "ok" : 1 }
列出分片信息
mongos> db.runCommand({
listshards:1})
{
"shards" : [
{
"_id" : "shard1",
"host" : "maomao/192.168.188.101:27017,192.168.188.101:27018,192.168.188.101:27019"
, "state" : 1
},
{
"_id" : "shard2",
"host" : "zhuzhu/192.168.188.101:27021,192.168.188.101:27022,192.168.188.101:27023"
, "state" : 1
}
],
"ok" : 1
}
整体状态查看
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f0574f7fe0c76b475e50326")
}
shards:
{
"_id" : "shard1", "host" : "kgcrs/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019", "state" : 1 }
{
"_id" : "shard2", "host" : "kgcrs1/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022", "state" : 1 }
active mongoses:
"3.4.24" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
激活分片功能
mongos> db
admin
mongos> db.runCommand({
enablesharding:"test"})
{
"ok" : 1 }
创建索引 :优化查询的重要手段
mongos> use test
switched to db test
mongos> db
test
创建索引
mongos> db.stu.ensureIndex({
id:1})
{
"raw" : {
"zhuzhu/192.168.188.101:27021,192.168.188.101:27022,192.168.188.101:27023" : {
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1618913138, 2),
"t" : NumberLong(1)
},
"electionId" : ObjectId("7fffffff0000000000000001")
}
}
},
"ok" : 1
}
mongos> use admin
switched to db admin
开启分片
mongos> db.runCommand({
shardcollection:"test.stu",key:{
id:1}})
{
"collectionsharded" : "test.stu", "ok" : 1 }
测试
集合分片验证
产生大量数据
mongos> use test;
switched to db test
mongos> for(var i=1;i<=1000000;i++){
db.stu.insert({
'id':i,'name':'Alice'});}
当test大小变成64mb之后 chunks: shard2会变成shard1