搭建多機 複本集+分片 環境

部署規劃

搭建複本集環境

在這裏插入圖片描述

腳本

在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 <<EOF
cfg={
_id:'$name',
members:[ 
{_id:0,host:'$host:27017'},
{_id:1,host:'$host:27018'},
{_id:2,host:'$host:27019'}
] 
};
rs.initiate(cfg)
EOF

在hcslave1上生成複本集

在hcmaster上執行腳本:mongodb-rs-shard.sh ‘hcslave1’ ‘rsh1’

[hc@hcmaster mongodb4.2.3]$ mongodb-rs-shard.sh 'hcslave1' 'rsh1'
create folder and files ...
create config file ....
run mongod ...
about to fork child process, waiting until server is ready for connections.
forked process: 12743
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 12792
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 12841
child process started successfully, parent exiting
initialize ...
MongoDB shell version v4.2.3
connecting to: mongodb://hcslave1:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("398a04e8-5c86-4552-9750-ea02f8f2c5a8") }
MongoDB server version: 4.2.3
{
        "_id" : "rsh1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "hcslave1:27017"
                },
                {
                        "_id" : 1,
                        "host" : "hcslave1:27018"
                },
                {
                        "_id" : 2,
                        "host" : "hcslave1:27019"
                }
        ]
}
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1585036664, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1585036664, 1)
}
bye

在hcslave1查看結果

  • 打開mongo客戶端
[hc@hcslave1 mongodb4.2.3]$ mongo 172.16.85.141
MongoDB shell version v4.2.3
connecting to: mongodb://172.16.85.141:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("dffca18d-9aa4-4568-adaa-27903b5a6805") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten]
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten]
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-03-24T15:57:41.264+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T15:57:41.264+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.status()
{
        "set" : "rsh1",
        "date" : ISODate("2020-03-24T09:11:58.741Z"),
        "myState" : 2,
        "term" : NumberLong(1),
        "syncingTo" : "hcslave1:27019",
        "syncSourceHost" : "hcslave1:27019",
        "syncSourceId" : 2,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1585041111, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-03-24T09:11:51.188Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1585041111, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-03-24T09:11:51.188Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1585041111, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1585041111, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-03-24T09:11:51.188Z"),
                "lastDurableWallTime" : ISODate("2020-03-24T09:11:51.188Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1585041069, 1),
        "lastStableCheckpointTimestamp" : Timestamp(1585041069, 1),
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(1),
                "lastVoteDate" : ISODate("2020-03-24T09:11:20.635Z"),
                "electionCandidateMemberId" : 2,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1585041069, 1),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1585041069, 1),
                        "t" : NumberLong(-1)
                },
                "priorityAtElection" : 1,
                "newTermStartDate" : ISODate("2020-03-24T09:11:21.187Z"),
                "newTermAppliedDate" : ISODate("2020-03-24T09:11:22.604Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "hcslave1:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 52,
                        "optime" : {
                                "ts" : Timestamp(1585041111, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-03-24T09:11:51Z"),
                        "syncingTo" : "hcslave1:27019",
                        "syncSourceHost" : "hcslave1:27019",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "hcslave1:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 49,
                        "optime" : {
                                "ts" : Timestamp(1585041111, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1585041111, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-03-24T09:11:51Z"),
                        "optimeDurableDate" : ISODate("2020-03-24T09:11:51Z"),
                        "lastHeartbeat" : ISODate("2020-03-24T09:11:58.608Z"),
                        "lastHeartbeatRecv" : ISODate("2020-03-24T09:11:58.229Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "hcslave1:27019",
                        "syncSourceHost" : "hcslave1:27019",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "hcslave1:27019",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 49,
                        "optime" : {
                                "ts" : Timestamp(1585041111, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1585041111, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-03-24T09:11:51Z"),
                        "optimeDurableDate" : ISODate("2020-03-24T09:11:51Z"),
                        "lastHeartbeat" : ISODate("2020-03-24T09:11:58.608Z"),
                        "lastHeartbeatRecv" : ISODate("2020-03-24T09:11:58.662Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1585041080, 1),
                        "electionDate" : ISODate("2020-03-24T09:11:20Z"),
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1585041111, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1585041111, 1)
}

發現:hcslave1:27019是PRIMARY

在hcslave2上生成複本集

在hcmaster上執行腳本: mongodb-rs-shard.sh ‘hcslave2’ ‘rsh2’

[hc@hcmaster mongodb4.2.3]$ mongodb-rs-shard.sh 'hcslave2' 'rsh2'
create folder and files ...
create config file ....
run mongod ...
about to fork child process, waiting until server is ready for connections.
forked process: 11833
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 11882
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 11931
child process started successfully, parent exiting
initialize ...
MongoDB shell version v4.2.3
connecting to: mongodb://hcslave2:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a4c8d042-f7c4-4dca-bbe8-7002ac94dfee") }
MongoDB server version: 4.2.3
{
        "_id" : "rsh2",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "hcslave2:27017"
                },
                {
                        "_id" : 1,
                        "host" : "hcslave2:27018"
                },
                {
                        "_id" : 2,
                        "host" : "hcslave2:27019"
                }
        ]
}
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1585036861, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1585036861, 1)
}
bye

查看結果

  • 打開mongo客戶端
[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("9562ec77-df89-4893-baca-5a17a907cc9e") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten]
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten]
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-03-24T16:00:59.184+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T16:00:59.184+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> 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 <<EOF
rs.initiate( {
   _id : "$repSet",
   configsvr: true,
   members: [
      { _id: 0, host: "hcmaster:$pot" },
      { _id: 1, host: "hcslave1:$pot" },
      { _id: 2, host: "hcslave2:$pot" }
   ]
})
EOF

在hcmaster上執行腳本mongodb-rs-shard2.sh

[hc@hcmaster mongodb4.2.3]$ mongodb-rs-shard2.sh 'cfgser' '27020'
about to fork child process, waiting until server is ready for connections.
forked process: 31097
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 14485
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 13570
child process started successfully, parent exiting
MongoDB shell version v4.2.3
connecting to: mongodb://hcmaster:27020/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("448f63b3-b041-4273-b736-011cd0e7927d") }
MongoDB server version: 4.2.3
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1585041403, 1),
                "electionId" : ObjectId("000000000000000000000000")
        },
        "lastCommittedOpTime" : Timestamp(0, 0),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1585041403, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1585041403, 1)
}
bye

Mongos

  • 在hcmaster上啓動
[hc@hcmaster mongodb4.2.3]$ mongos --logpath /usr/local/mongodb4.2.3/logs/mongos.log  --port 27021 --configdb cfgser/hcmaster:27020,hcslave1:27020,hcslave2:27020 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 31201
child process started successfully, parent exiting
  • 在hcmaster上登錄到mongos
[hc@hcmaster mongodb4.2.3]$ mongo --port 27021
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27021/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5658141c-9f2b-4b38-9e23-0818e2df6e80") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T17:20:04.999+0800 I  CONTROL  [main]
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2020-03-24T17:20:04.999+0800 I  CONTROL  [main]
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] ** WARNING: This server is bound to localhost.
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          Remote systems will be unable to connect to this server.
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          Start the server with --bind_ip <address> to specify which IP
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          addresses it should serve responses from, or with --bind_ip_all to
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          bind to all interfaces. If this behavior is desired, start the
2020-03-24T17:20:04.999+0800 I  CONTROL  [main] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-03-24T17:20:04.999+0800 I  CONTROL  [main]
mongos>

添加分片

mongos> 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<4500;i++){  # 共計增加4499個數據
... db.user.insert({userid:i,intro:'aaaaabbbbbbcccccc'})
... }
WriteResult({ "nInserted" : 1 })

在hcslave1中

  • 登錄PRIMARY
[hc@hcslave1 mongodb4.2.3]$ mongo 172.16.85.141:27019
MongoDB shell version v4.2.3
connecting to: mongodb://172.16.85.141:27019/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("192c5b13-355f-434b-a4ee-02d6d5093b53") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten]
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten]
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-03-24T17:11:09.436+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-24T17:11:09.436+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:PRIMARY> 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都是隻放置了一部分數據,體現了分片

在hcsalve2中

[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,這體現了分片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章