项目环境:
CentOS 7.5
MongoDB 4.2.12
一、分片集群规划
IP:172.16.254.136 172.16.254.137 172.16.254.138
mongos(30000) mongos(30000) mongos(30000)
config(27017) config(27017) config(27017)
shard1主节点(40001) shard1副节点(40001) shard1仲裁节点(40001)
shard2仲裁节点(40002) shard2主节点(40002) shard2副节点(40002)
shard3副节点(40003) shard3仲裁节点(40003) shard3主节点(40003)
二、创建目录
su - mongodb
mkdir -p /mongodb
mkdir -p /mongodb/{data,logs,apps}
mkdir -p /mongodb/data/shard{1,2,3}
mkdir -p /mongodb/data/config
/usr/sbin/groupadd -g 10001 mongodb
/usr/sbin/useradd -u 10001 -g mongodb mongodb
id mongodb
passwd mongodb
chown -R mongodb:mongodb /mongodb
chmod -R 775 /mongodb
三、部署config服务器
三台机器都部署
/mongodb/conf/mongodb-config.conf
dbpath=/mongodb/data/config
logpath=/mongodb/logs/mongodb-config.log
port=27017
fork=true
journal = true
maxConns=500
logappend=true
pidfilepath=/mongodb/apps/mongo-config.pid
directoryperdb=true
replSet=mongodb99
configsvr = true
bind_ip=0.0.0.0
启动config实例(三台机器)
mongod -f /mongodb/conf/mongodb-config.conf
创建副本集
rs.initiate({_id:"mongodb99",members:[
{_id:0,host:"172.16.254.136:27017"},
{_id:1,host:"172.16.254.137:27017"},
{_id:2,host:"172.16.254.138:27017"}
]})
检查config副本集信息
mongodb99:PRIMARY> rs.status()
{
"set" : "mongodb99",
"date" : ISODate("2021-03-19T07:17:58.639Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-19T07:17:57.051Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-19T07:17:57.051Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-19T07:17:57.051Z"),
"lastDurableWallTime" : ISODate("2021-03-19T07:17:57.051Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616138263, 3),
"lastStableCheckpointTimestamp" : Timestamp(1616138263, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-19T07:17:43.126Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616138252, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-19T07:17:43.165Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-19T07:17:44.352Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.136:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 843,
"optime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T07:17:57Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1616138263, 1),
"electionDate" : ISODate("2021-03-19T07:17:43Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.16.254.137:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 25,
"optime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T07:17:57Z"),
"optimeDurableDate" : ISODate("2021-03-19T07:17:57Z"),
"lastHeartbeat" : ISODate("2021-03-19T07:17:57.201Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T07:17:58.527Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.136:27017",
"syncSourceHost" : "172.16.254.136:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.254.138:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 25,
"optime" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616138277, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T07:17:57Z"),
"optimeDurableDate" : ISODate("2021-03-19T07:17:57Z"),
"lastHeartbeat" : ISODate("2021-03-19T07:17:57.201Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T07:17:58.527Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.136:27017",
"syncSourceHost" : "172.16.254.136:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1616138252, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1616138277, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1616138277, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616138277, 1)
}
四、部署shard1分片服务器
参数文件,三台机器都需要配置
/mongodb/conf/mongodb-shard1.conf
dbpath=/mongodb/data/shard1
logpath=/mongodb/logs/mongodb-shard1.log
port=40001
fork=true
#auth=true
#noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=5000
logappend=true
pidfilepath=/mongodb/apps/mongo-shard1.pid
#cpu=true
directoryperdb=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
replSet=hdshard1
bind_ip=0.0.0.0
shardsvr = true
启动服务(三台机器)
mongod -f /mongodb/conf/mongodb-shard1.conf
设定副本集成员
rs.initiate({_id:"hdshard1",members:[
{_id:0,host:"172.16.254.136:40001",priority:2},
{_id:1,host:"172.16.254.137:40001",priority:1},
{_id:2,host:"172.16.254.138:40001",arbiterOnly:true}
]})
查看副本集信息
hdshard1:PRIMARY> rs.status()
{
"set" : "hdshard1",
"date" : ISODate("2021-03-19T08:43:12.244Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-19T08:43:03.255Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-19T08:43:03.255Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-19T08:43:03.255Z"),
"lastDurableWallTime" : ISODate("2021-03-19T08:43:03.255Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616143383, 3),
"lastStableCheckpointTimestamp" : Timestamp(1616143383, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-19T08:43:03.187Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616143372, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-19T08:43:03.255Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-19T08:43:04.069Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.136:40001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3286,
"optime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T08:43:03Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1616143383, 1),
"electionDate" : ISODate("2021-03-19T08:43:03Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.16.254.137:40001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616143383, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T08:43:03Z"),
"optimeDurableDate" : ISODate("2021-03-19T08:43:03Z"),
"lastHeartbeat" : ISODate("2021-03-19T08:43:11.294Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T08:43:12.127Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.136:40001",
"syncSourceHost" : "172.16.254.136:40001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.254.138:40001",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 20,
"lastHeartbeat" : ISODate("2021-03-19T08:43:11.294Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T08:43:10.512Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1616143383, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616143383, 3)
}
五、部署shard2分片服务器
参数文件,三台机器都需要配置
/mongodb/conf/mongodb-shard2.conf
dbpath=/mongodb/data/shard2
logpath=/mongodb/logs/mongodb-shard2.log
port=40002
fork=true
#auth=true
#noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=5000
logappend=true
pidfilepath=/mongodb/apps/mongo-shard2.pid
#cpu=true
directoryperdb=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
replSet=hdshard2
bind_ip=0.0.0.0
shardsvr = true
启动服务(三台机器)
mongod -f /mongodb/conf/mongodb-shard2.conf
设置副本集成员
rs.initiate({_id:"hdshard2",members:[
{_id:0,host:"172.16.254.136:40002",arbiterOnly:true},
{_id:1,host:"172.16.254.137:40002",priority:2},
{_id:2,host:"172.16.254.138:40002",priority:1}
]})
查看副本集信息
hdshard2:PRIMARY> rs.status()
{
"set" : "hdshard2",
"date" : ISODate("2021-03-19T09:11:49.553Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-19T09:11:42.844Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-19T09:11:42.844Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-19T09:11:42.844Z"),
"lastDurableWallTime" : ISODate("2021-03-19T09:11:42.844Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616145102, 3),
"lastStableCheckpointTimestamp" : Timestamp(1616145102, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-19T09:11:42.780Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616145092, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-19T09:11:42.844Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-19T09:11:43.578Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.136:40002",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 16,
"lastHeartbeat" : ISODate("2021-03-19T09:11:48.864Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T09:11:49.220Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "172.16.254.137:40002",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 93,
"optime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T09:11:42Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1616145102, 1),
"electionDate" : ISODate("2021-03-19T09:11:42Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.16.254.138:40002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 16,
"optime" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616145102, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T09:11:42Z"),
"optimeDurableDate" : ISODate("2021-03-19T09:11:42Z"),
"lastHeartbeat" : ISODate("2021-03-19T09:11:48.864Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T09:11:47.702Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.137:40002",
"syncSourceHost" : "172.16.254.137:40002",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1616145102, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616145102, 3)
}
六、部署shard3分片服务器
参数文件,三台机器都需要配置
/mongodb/conf/mongodb-shard3.conf
dbpath=/mongodb/data/shard3
logpath=/mongodb/logs/mongodb-shard3.log
port=40003
fork=true
#auth=true
#noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=5000
logappend=true
pidfilepath=/mongodb/apps/mongo-shard3.pid
#cpu=true
directoryperdb=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
replSet=hdshard3
bind_ip=0.0.0.0
shardsvr = true
启动服务(三台机器)
mongod -f /mongodb/conf/mongodb-shard3.conf
设置副本集成员
rs.initiate({_id:"hdshard3",members:[
{_id:0,host:"172.16.254.136:40003",priority:1},
{_id:1,host:"172.16.254.137:40003",arbiterOnly:true},
{_id:2,host:"172.16.254.138:40003",priority:2}
]})
查看副本集信息
hdshard3:PRIMARY> rs.status()
{
"set" : "hdshard3",
"date" : ISODate("2021-03-19T09:14:03.826Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-19T09:14:00.492Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-19T09:14:00.492Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-19T09:14:00.492Z"),
"lastDurableWallTime" : ISODate("2021-03-19T09:14:00.492Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616145230, 3),
"lastStableCheckpointTimestamp" : Timestamp(1616145230, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-19T09:13:50.444Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616145220, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-19T09:13:50.490Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-19T09:13:51.267Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.136:40003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T09:14:00Z"),
"optimeDurableDate" : ISODate("2021-03-19T09:14:00Z"),
"lastHeartbeat" : ISODate("2021-03-19T09:14:02.489Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T09:14:03.370Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.138:40003",
"syncSourceHost" : "172.16.254.138:40003",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "172.16.254.137:40003",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 23,
"lastHeartbeat" : ISODate("2021-03-19T09:14:02.489Z"),
"lastHeartbeatRecv" : ISODate("2021-03-19T09:14:02.743Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.254.138:40003",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 82,
"optime" : {
"ts" : Timestamp(1616145240, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-19T09:14:00Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1616145230, 1),
"electionDate" : ISODate("2021-03-19T09:13:50Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1616145240, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616145240, 1)
}
七、部署路由服务器
配置参数文件(三台机器)
/mongodb/conf/mongodb-route.conf
pidfilepath=/mongodb/apps/mongo-route.pid
logpath=/mongodb/logs/mongodb-route.log
port=30000
fork=true
maxConns=5000
logappend=true
bind_ip=0.0.0.0
configdb = mongodb99/172.16.254.136:27017,172.16.254.137:27017,172.16.254.138:27017
启动服务(三台机器),注意是mongos,而不是mongod
mongos -f /mongodb/conf/mongodb-route.conf
八、启动分片功能
mongo --port 30000
sh.addShard("hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001")
sh.addShard("hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002")
sh.addShard("hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003")
九、测试分片
插入测试数据
mongos> use db5
switched to db db5
mongos> show dbs
admin 0.000GB
config 0.001GB
mongos> db.settings.save({"_id":"chunksize","value":1})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
//设置块大小为1M是方便实验,不然就需要插入海量数据
启用数据库分片
sh.enableSharding("db5")
为表创建索引
mongos> db.db5.createIndex({"id":1})
{
"raw" : {
"hdshard2/172.16.254.137:40002,172.16.254.138:40002" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1616150499, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1616150499, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
启动表分片
mongos> sh.shardCollection("db5.db5",{"id":1})
{
"collectionsharded" : "db5.db5",
"collectionUUID" : UUID("2122da21-7279-4b88-b60f-73dd3d0276fb"),
"ok" : 1,
"operationTime" : Timestamp(1616150605, 9),
"$clusterTime" : {
"clusterTime" : Timestamp(1616150605, 9),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看分片情况
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("60545017224c766911a9c440")
}
shards:
{ "_id" : "hdshard1", "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001", "state" : 1 }
{ "_id" : "hdshard2", "host" : "hdshard2/172.16.254.137:40002,172.16.254.138:40002", "state" : 1 }
{ "_id" : "hdshard3", "host" : "hdshard3/172.16.254.136:40003,172.16.254.138:40003", "state" : 1 }
active mongoses:
"4.2.12" : 3
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:
682 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
hdshard1 342
hdshard2 341
hdshard3 341
too many chunks to print, use verbose if you want to force print
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
db5.db5
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
hdshard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : hdshard2 Timestamp(1, 0)
可以看到数据已经平均分布到三个分片。