部署规划
搭建复本集环境
在hcmaster上创建脚本 mongodb-rs-shard.sh
#! /bin/sh
# 使用方式:mongodb-rs-shard.sh 'hcmaster' 'rsh'
# 主机名称
host=$1
# 副本集名称rsh
name=$2
###################################
baseDir=/usr/local/mongodb4.2.3
dataFolder=$baseDir/data
logsFolder=$baseDir/logs
confFolder=$baseDir/conf
logFile=$logsFolder/mongodb.log
###################################
# 生成配置文件
#$1 27107
#$2 数据文件夹和配置文件序号
function writeConf(){
ssh hc@$host "echo bind_ip=$host >> $confFolder/confFile$2.conf"
ssh hc@$host "echo port=$1 >> $confFolder/confFile$2.conf"
ssh hc@$host "echo dbpath=$dataFolder/d$2 >> $confFolder/confFile$2.conf"
ssh hc@$host "echo logpath=/usr/local/mongodb4.2.3/logs/mongodb$2.log >> $confFolder/confFile$2.conf"
ssh hc@$host "echo pidfilepath=/usr/local/mongodb4.2.3/mongodb$2.pid >> $confFolder/confFile$2.conf"
ssh hc@$host "echo logappend=true >> $confFolder/confFile$2.conf"
ssh hc@$host "echo fork=true >> $confFolder/confFile$2.conf"
ssh hc@$host "echo maxConns=500 >> $confFolder/confFile$2.conf"
ssh hc@$host "echo noauth=true >> $confFolder/confFile$2.conf"
ssh hc@$host "echo replSet=$name >> $confFolder/confFile$2.conf"
ssh hc@$host "echo shardsvr=true >> $confFolder/confFile$2.conf"
}
###################################
# 生成数据文件
echo 'create folder and files ...'
ssh hc@$host "mkdir -p $dataFolder/d1 $dataFolder/d2 $dataFolder/d3 $logsFolder $confFolder"
# 生成配置文件
echo 'create config file ....'
writeConf '27017' '1'
writeConf '27018' '2'
writeConf '27019' '3'
# 启动MongoDB服务
echo 'run mongod ...'
ssh hc@$host "/usr/local/mongodb4.2.3/bin/mongod -f $confFolder/confFile1.conf"
ssh hc@$host "/usr/local/mongodb4.2.3/bin/mongod -f $confFolder/confFile2.conf"
ssh hc@$host "/usr/local/mongodb4.2.3/bin/mongod -f $confFolder/confFile3.conf"
echo 'initialize ...'
/usr/local/mongodb4.2.3/bin/mongo $host:27017 rs.status()
{
"set" : "rsh2",
"date" : ISODate("2020-03-24T09:13:41.488Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-03-24T09:13:38.679Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-03-24T09:13:38.679Z"),
"appliedOpTime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-03-24T09:13:38.679Z"),
"lastDurableWallTime" : ISODate("2020-03-24T09:13:38.679Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1585041168, 1),
"lastStableCheckpointTimestamp" : Timestamp(1585041168, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-03-24T09:11:47.657Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1585041096, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-03-24T09:11:48.668Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-03-24T09:11:49.268Z")
},
"members" : [
{
"_id" : 0,
"name" : "hcslave2:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 128,
"optime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-03-24T09:13:38Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1585041107, 1),
"electionDate" : ISODate("2020-03-24T09:11:47Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "hcslave2:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 124,
"optime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-03-24T09:13:38Z"),
"optimeDurableDate" : ISODate("2020-03-24T09:13:38Z"),
"lastHeartbeat" : ISODate("2020-03-24T09:13:39.788Z"),
"lastHeartbeatRecv" : ISODate("2020-03-24T09:13:41.379Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hcslave2:27017",
"syncSourceHost" : "hcslave2:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "hcslave2:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 124,
"optime" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1585041218, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-03-24T09:13:38Z"),
"optimeDurableDate" : ISODate("2020-03-24T09:13:38Z"),
"lastHeartbeat" : ISODate("2020-03-24T09:13:39.788Z"),
"lastHeartbeatRecv" : ISODate("2020-03-24T09:13:41.343Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hcslave2:27017",
"syncSourceHost" : "hcslave2:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1585041218, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1585041218, 1)
}
发现:hcslave2:27017是PRIMARY
搭建分片环境 在hcmaster上创建脚本mongodb-rs-shard2.sh#! /bin/sh
# 使用方式:mongodb-rs-shard2.sh "cfgser" "27020"
repSet=$1
pot=$2
ssh hc@hcmaster "mkdir -p /usr/local/mongodb4.2.3/data/$repSet /usr/local/mongodb4.2.3/logs"
ssh hc@hcslave1 "mkdir -p /usr/local/mongodb4.2.3/data/$repSet /usr/local/mongodb4.2.3/logs"
ssh hc@hcslave2 "mkdir -p /usr/local/mongodb4.2.3/data/$repSet /usr/local/mongodb4.2.3/logs"
ssh hc@hcmaster "/usr/local/mongodb4.2.3/bin/mongod --dbpath /usr/local/mongodb4.2.3/data/cfgser --logpath /usr/local/mongodb4.2.3/logs/cfgser.log --bind_ip hcmaster --port $pot --fork --configsvr --replSet $repSet --logappend"
ssh hc@hcslave1 "/usr/local/mongodb4.2.3/bin/mongod --dbpath /usr/local/mongodb4.2.3/data/cfgser --logpath /usr/local/mongodb4.2.3/logs/cfgser.log --bind_ip hcslave1 --port $pot --fork --configsvr --replSet $repSet --logappend"
ssh hc@hcslave2 "/usr/local/mongodb4.2.3/bin/mongod --dbpath /usr/local/mongodb4.2.3/data/cfgser --logpath /usr/local/mongodb4.2.3/logs/cfgser.log --bind_ip hcslave2 --port $pot --fork --configsvr --replSet $repSet --logappend"
/usr/local/mongodb4.2.3/bin/mongo hcmaster:$pot sh.addShard('rsh1/hcslave1:27019')
{
"shardAdded" : "rsh1",
"ok" : 1,
"operationTime" : Timestamp(1585041741, 7),
"$clusterTime" : {
"clusterTime" : Timestamp(1585041741, 7),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard('rsh2/hcslave2:27017')
{
"shardAdded" : "rsh2",
"ok" : 1,
"operationTime" : Timestamp(1585041771, 8),
"$clusterTime" : {
"clusterTime" : Timestamp(1585041771, 8),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
测试
查看当前分片的状态
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e79d00780b0a4e4e03caab8")
}
shards:
{ "_id" : "rsh1", "host" : "rsh1/hcslave1:27017,hcslave1:27018,hcslave1:27019", "state" : 1 }
{ "_id" : "rsh2", "host" : "rsh2/hcslave2:27017,hcslave2:27018,hcslave2:27019", "state" : 1 }
active mongoses:
"4.2.3" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
指定哪个数据库使用分片
mongos> sh.enableSharding('shop')
{
"ok" : 1,
"operationTime" : Timestamp(1585041955, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1585041955, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
指定哪个数据表使用分片
mongos> sh.shardCollection('shop.user',{userid:1})
{
"collectionsharded" : "shop.user",
"collectionUUID" : UUID("1bea255f-7cf3-4b02-873d-a99da05df508"),
"ok" : 1,
"operationTime" : Timestamp(1585042028, 12),
"$clusterTime" : {
"clusterTime" : Timestamp(1585042028, 12),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
创建片键:每隔1000条分一下片
mongos> sh.splitAt('shop.user',{userid:1000})
{
"ok" : 1,
"operationTime" : Timestamp(1585042079, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1585042084, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看结果
mongos> sh.splitAt('shop.user',{userid:2000})
{
"ok" : 1,
"operationTime" : Timestamp(1585042088, 30),
"$clusterTime" : {
"clusterTime" : Timestamp(1585042099, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.splitAt('shop.user',{userid:3000})
{
"ok" : 1,
"operationTime" : Timestamp(1585042099, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1585042102, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e79d00780b0a4e4e03caab8")
}
shards:
{ "_id" : "rsh1", "host" : "rsh1/hcslave1:27017,hcslave1:27018,hcslave1:27019", "state" : 1 }
{ "_id" : "rsh2", "host" : "rsh2/hcslave2:27017,hcslave2:27018,hcslave2:27019", "state" : 1 }
active mongoses:
"4.2.3" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
2 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
rsh1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : rsh1 Timestamp(1, 0)
{ "_id" : "shop", "primary" : "rsh2", "partitioned" : true, "version" : { "uuid" : UUID("34141f3f-4311-457a-be46eaed4787"), "lastMod" : 1 } }
shop.user
shard key: { "userid" : 1 }
unique: false
balancing: true
chunks:
rsh1 2
rsh2 2
{ "userid" : { "$minKey" : 1 } } -->> { "userid" : 1000 } on : rsh1 Timestamp(3, 0)
{ "userid" : 1000 } -->> { "userid" : 2000 } on : rsh1 Timestamp(6, 0)
{ "userid" : 2000 } -->> { "userid" : 3000 } on : rsh2 Timestamp(6, 1)
{ "userid" : 3000 } -->> { "userid" : { "$maxKey" : 1 } } on : rsh2 Timestamp(5, 2)
mongos>
体验集群中的副本和分片
往表中添加数据
mongos> use shop
switched to db shop
mongos> for(var i =1;i show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
rsh1:PRIMARY> use shop
switched to db shop
rsh1:PRIMARY>
rsh1:PRIMARY> db.user.count()
1999
- 登录SECONDARY
[hc@hcslave1 mongodb4.2.3]$ mongo 172.16.85.141:27017
MongoDB shell version v4.2.3
connecting to: mongodb://172.16.85.141:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8bac0e76-59a9-40ea-82c8-ff5b04fb97e8") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten]
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten]
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-03-24T17:11:07.278+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
rsh1:SECONDARY> rs.slaveOk()
rsh1:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
rsh1:SECONDARY> use shop
switched to db shop
rsh1:SECONDARY> db.user.count()
1999
结论:
- PRIMARY和SECONDARY中数据一样,体现 了副本集
- 不管是PRIMARY还是SECONDARY都是只放置了一部分数据,体现了分片
[hc@hcslave2 mongodb4.2.3]$ mongo 172.16.85.142:27017
MongoDB shell version v4.2.3
connecting to: mongodb://172.16.85.142:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("aa9ff28a-74aa-43ed-974c-5f46b5bbb752") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten]
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten]
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-03-24T17:11:34.221+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
rsh2:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
rsh2:PRIMARY> use shop
switched to db shop
rsh2:PRIMARY> show collections
user
rsh2:PRIMARY> db.user.count()
2500
结论: hcsalve1的1999+hcslave2的2500=4499,这体现了分片