1. 程式人生 > 實用技巧 >mongo-4.4單例項搭建分片叢集

mongo-4.4單例項搭建分片叢集

mongo-4.4單例項搭建分片叢集

官網地址 中文地址 下載地址

1、安裝背景

由於原開發伺服器mongodb資料庫為單機安裝,為了提高訪問效率和資料安全性,需要配置成單機分片叢集。

1.1 實驗環境

1.1.1 分片叢集架構官圖

1.1.2 實驗主機
主機 IP
CentOS 7.4 66.1.30.31
1.1.3 埠規劃
伺服器
route1 27017
conf1 29010
conf2 29011
conf3 29012
shard1_primary 27010
shard1_secondary 27011
shard1_arbiter 27012
shard2_primary 28010
shard2_secondary 28011
shard2_arbiter 28012

1.2 安裝包下載

mongodb-linux-x86_64-rhel70-4.4.2.tgz

1.3配置環境變數

[root@bdp-fmq05 ~]# vim /etc/profile
在 /etc/profile 配置檔案中新增 MongoDB 環境變數,內容如下:
export MONGODB_HOME=/opt/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
[root@bdp-fmq05 ~]# source /etc/profile

2、部署分片

Ⅰ、配置分片資料

2.1埠規劃

shard1:rs-1:27010
shard1:rs-1:27011
shard1:rs-1:27012

2.2 分片副本集路徑規劃建立

2.2.1 Pid檔案路徑
[root@bdp-fmq05 ~]# mkdir -p /var/run/mongodb27010
[root@bdp-fmq05 ~]# mkdir -p /var/run/mongodb27011
[root@bdp-fmq05 ~]# mkdir -p /var/run/mongodb27012
[root@bdp-fmq05 ~]# touch /var/run/mongodb2701{0,1,2}/mongod.pid
2.2.2 日誌儲存路徑
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod27010
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod27011
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod27012
[root@bdp-fmq05 /]# touch /data/log/mongod2701{0,1,2}/mongod.log
2.2.3 分片資料儲存路徑
[root@bdp-fmq05 /]# mkdir -p /data/db/db27010
[root@bdp-fmq05 /]# mkdir -p /data/db/db27011
[root@bdp-fmq05 /]# mkdir -p /data/db/db27012
2.2.4 keyfile檔案路徑
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey27010
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey27011
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey27012
[root@bdp-fmq05 /]# touch /data/keyfile/autokey2701{0,1,2}/autokey

2.3 生成keyfile檔案

注意:所有的keyfile保持一致

[root@bdp-fmq05 /]# openssl rand -base64 756 > /data/keyfile/autokey27010/autokey
[root@bdp-fmq05 /]# cp /data/keyfile/autokey27010/autokey /data/keyfile/autokey27011/
cp: overwrite ‘/data/keyfile/autokey27011/autokey’? y
[root@bdp-fmq05 /]# cp /data/keyfile/autokey27010/autokey /data/keyfile/autokey27012/
cp: overwrite ‘/data/keyfile/autokey27012/autokey’? y
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey27010/autokey
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey27011/autokey
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey27012/autokey

2.4 分片副本集配置資料

  • 配置檔案路徑:
[root@bdp-fmq05 /]# mkdir -p /opt/mongos/conf2701{0,1,2}
2.4.1 rs-1-27010配置
  • rs-1-27010yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf27010/shard27010.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard1
net:
    port: 27010
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod27010/mongod.log
storage:
    dbPath: /data/db/db27010
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb27010/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
    #keyFile: /data/keyfile/autokey27010/autoke
2.4.2 rs-1-27011配置
  • rs-1-27011yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf27011/shard27011.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard1
net:
    port: 27011
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod27011/mongod.log
storage:
    dbPath: /data/db/db27011
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb27011/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
    #keyFile: /data/keyfile/autokey27011/autokey
2.4.3 rs-1-27012配置
  • rs-1-27012yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf27012/shard27012.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard1
net:
    port: 27012
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod27012/mongod.log
storage:
    dbPath: /data/db/db27012
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb27012/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
    #keyFile: /data/keyfile/autokey27012/autokey

Ⅱ、配置分片資料

2.5埠規劃

shard2:rs-2:28010
shard2:rs-2:28011
shard2:rs-2:28012

2.6 分片副本集路徑規劃建立

2.6.1 Pid檔案路徑
[root@bdp-fmq05 /]# mkdir -p /var/run/mongodb28010
[root@bdp-fmq05 /]# mkdir -p /var/run/mongodb28011
[root@bdp-fmq05 /]# mkdir -p /var/run/mongodb28012
[root@bdp-fmq05 /]# touch /var/run/mongodb2801{0,1,2}/mongod.pid
2.6.2 日誌儲存路徑
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod28010
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod28011
[root@bdp-fmq05 /]# mkdir -p /data/log/mongod28012
[root@bdp-fmq05 /]# touch /data/log/mongod2801{0,1,2}/mongod.log
2.6.3 分片資料儲存路徑
[root@bdp-fmq05 /]# mkdir -p /data/db/db28010
[root@bdp-fmq05 /]# mkdir -p /data/db/db28011
[root@bdp-fmq05 /]# mkdir -p /data/db/db28012
2.6.4 keyfile檔案路徑
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey28010
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey28011
[root@bdp-fmq05 /]# mkdir -p /data/keyfile/autokey28012
[root@bdp-fmq05 /]# touch /data/keyfile/autokey2801{0,1,2}/autokey

2.7生成keyfile檔案

注意:所有的keyfile保持一致

[root@bdp-fmq05 /]# cp /data/keyfile/autokey27010/autokey /data/keyfile/autokey28010/autokey
cp: overwrite ‘/data/keyfile/autokey28010/autokey’? y
[root@bdp-fmq05 /]# cp /data/keyfile/autokey27010/autokey /data/keyfile/autokey28011/autokey
cp: overwrite ‘/data/keyfile/autokey28011/autokey’? y
[root@bdp-fmq05 /]# cp /data/keyfile/autokey28010/autokey /data/keyfile/autokey28012/autokey
cp: overwrite ‘/data/keyfile/autokey28012/autokey’? y
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey28010/autokey
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey28011/autokey
[root@bdp-fmq05 /]# chmod 400 /data/keyfile/autokey28012/autokey

2.8分片副本集配置資料

  • 配置檔案路徑
[root@bdp-fmq05 /]# mkdir -p /opt/mongos/conf2801{0,1,2}
2.8.1 rs-2-28010配置
  • rs-2-28010yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf28010/shard28010.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard2
net:
    port: 28010
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod28010/mongod.log
storage:
    dbPath: /data/db/db28010
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb28010/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
   #keyFile: /data/keyfile/autokey28010/autokey
2.8.2 rs-2-28011配置
  • rs-2-28011yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf28010/shard28011.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard2
net:
    port: 28011
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod28011/mongod.log
storage:
    dbPath: /data/db/db28011
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb28011/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
   #keyFile: /data/keyfile/autokey28011/autokey
2.8.3 rs-2-28012配置
  • rs-2-28012yaml配置檔案
[root@bdp-fmq05 /]# vim /opt/mongos/conf28010/shard28012.yaml
sharding:
    clusterRole: shardsvr
replication:
    replSetName: shard2
net:
    port: 28012
    bindIp: 0.0.0.0
systemLog:
    destination: file
    logAppend: true
    path: /data/log/mongod28012/mongod.log
storage:
    dbPath: /data/db/db28012
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb28012/mongod.pid
    timeZoneInfo: /usr/share/zoneinfo
#security:
   #keyFile: /data/keyfile/autokey28012/autokey

3、config配置伺服器配置

3.1 埠及檔案路徑規劃

3.1.1 Pid檔案路徑
[root@bdp-fmq05 keyfile]# mkdir -p /var/run/config/mongodb29010
[root@bdp-fmq05 keyfile]# mkdir -p /var/run/config/mongodb29011
[root@bdp-fmq05 keyfile]# mkdir -p /var/run/config/mongodb29012
[root@bdp-fmq05 keyfile]# touch /var/run/config/mongodb2901{0,1,2}/mongod.pid
3.1.2 日誌儲存路徑
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/log/mongod29010
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/log/mongod29011
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/log/mongod29012
[root@bdp-fmq05 keyfile]# touch /data/config/log/mongod2901{0,1,2}/mongod.log
3.1.3 分片資料儲存路徑
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/db/db29010
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/db/db29011
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/db/db29012
3.1.4 keyfile檔案路徑
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/keyfile/autokey29010
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/keyfile/autokey29011
[root@bdp-fmq05 keyfile]# mkdir -p /data/config/keyfile/autokey29012
[root@bdp-fmq05 keyfile]# touch /data/config/keyfile/autokey2901{0,1,2}/autokey

3.2 生成keyfile檔案

注意:所有的keyfile保持一致

[root@bdp-fmq05 keyfile]# cp /data/keyfile/autokey27010/autokey /data/config/keyfile/autokey29010/autokey
cp: overwrite ‘/data/config/keyfile/autokey29010/autokey’? y
[root@bdp-fmq05 keyfile]# cp /data/keyfile/autokey27010/autokey /data/config/keyfile/autokey29011/autokey
cp: overwrite ‘/data/config/keyfile/autokey29011/autokey’? y
[root@bdp-fmq05 keyfile]# cp /data/keyfile/autokey27010/autokey /data/config/keyfile/autokey29012/autokey
cp: overwrite ‘/data/config/keyfile/autokey29012/autokey’? y
[root@bdp-fmq05 keyfile]# chmod 400 /data/config/keyfile/autokey29010/autokey
[root@bdp-fmq05 keyfile]# chmod 400 /data/config/keyfile/autokey29011/autokey
[root@bdp-fmq05 keyfile]# chmod 400 /data/config/keyfile/autokey29012/autokey

3.3 config配置檔案

  • 配置檔案路徑
[root@bdp-fmq05 keyfile]# mkdir -p /opt/configsvr/conf2901{0,1,2}
3.3.1 config29010配置
  • 29010-yaml配置檔案
[root@bdp-fmq05 keyfile]# vim /opt/configsvr/conf29010/config29010.yaml
sharding:
  clusterRole: configsvr  
replication:
  replSetName: config  	 
net:
  port: 29010              
  bindIp: 0.0.0.0        
systemLog:
  destination: file        
  logAppend: true		   
  path: /data/config/log/mongod29010/mongod.log    
storage:
  dbPath: /data/config/db/db29010              
  journal:
    enabled: true           
processManagement:
  fork: true               
  pidFilePath: /var/run/config/mongodb29010/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo    
#security:
  #keyFile: /data/config/keyfile/autokey29010/autoke
3.3.2 config29011配置
  • 29011-yaml配置檔案
[root@bdp-fmq05 keyfile]# vim /opt/configsvr/conf29011/config29011.yaml
sharding:
  clusterRole: configsvr  
replication:
  replSetName: config  	 
net:
  port: 29011              
  bindIp: 0.0.0.0        
systemLog:
  destination: file        
  logAppend: true		   
  path: /data/config/log/mongod29011/mongod.log    
storage:
  dbPath: /data/config/db/db29011              
  journal:
    enabled: true           
processManagement:
  fork: true               
  pidFilePath: /var/run/config/mongodb29011/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo    
#security:
  #keyFile: /data/config/keyfile/autokey29011/autokey
3.3.3 config29012配置
  • 29012-yaml配置檔案
[root@bdp-fmq05 keyfile]# vim /opt/configsvr/conf29012/config29012.yaml
sharding:
  clusterRole: configsvr  
replication:
  replSetName: config  	 
net:
  port: 29012              
  bindIp: 0.0.0.0        
systemLog:
  destination: file        
  logAppend: true		   
  path: /data/config/log/mongod29012/mongod.log    
storage:
  dbPath: /data/config/db/db29012              
  journal:
    enabled: true           
processManagement:
  fork: true               
  pidFilePath: /var/run/config/mongodb29012/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo    
#security:
  #keyFile: /data/config/keyfile/autokey29012/autokey

4、mongos路由配置

4.1埠及檔案路徑配置

4.1.1 pid檔案路徑
[root@bdp-fmq05 keyfile]# mkdir -p /data/mongos/pidfile
[root@bdp-fmq05 keyfile]# touch /data/mongos/pidfile/mongos.pid
4.1.2 日誌儲存路徑
[root@bdp-fmq05 keyfile]# mkdir -p /data/mongos/log/
[root@bdp-fmq05 keyfile]# touch /data/mongos/log/route.log
4.1.3 keyfile檔案路徑
[root@bdp-fmq05 mongos]# mkdir -p /data/mongos/keyfile/
[root@bdp-fmq05 mongos]# touch /data/mongos/keyfile/autokey

4.2 keyfile檔案路徑

寫入autokey(可以直接拷貝上面生成檔案,全域性key保持一致)

[root@bdp-fmq05 keyfile]# cp /data/keyfile/autokey27010/autokey /data/mongos/keyfile/autokey
cp: overwrite ‘/data/mongos/keyfile/autokey’? y
[root@bdp-fmq05 keyfile]# chmod 400 /data/mongos/keyfile/autokey

4.3 mongos配置檔案

  • route-yaml配置檔案
[root@bdp-fmq05 mongos]# vi /opt/mongoroute/mongoroute.yaml
sharding:
    configDB: config/192.168.17.160:29010,192.168.17.160:29011,192.168.17.160:29012
net:
    port: 27017
    bindIp: 0.0.0.0
 
systemLog:
    destination: file
    logAppend: true
    path: /data/mongos/log/route.log
 
processManagement:
    fork: true
    pidFilePath: /data/mongos/pidfile/mongos.pid
    timeZoneInfo: /usr/share/zoneinfo
 
security:
  keyFile: /data/mongos/keyfile/autokey

5、配置伺服器、分片配置啟動

5.1 啟動服務

按順序依次啟動所有分片副本集(rs-1,rs-2)和配置伺服器(config)和mongos路由3

5.1.1啟動配置伺服器(config)
  • 先將yaml配置檔案中security部分註釋掉,然後啟動
[root@bdp-fmq05 opt]# mongod -f /opt/configsvr/conf29010/config29010.yaml
[root@bdp-fmq05 opt]# mongod -f /opt/configsvr/conf29011/config29011.yaml
[root@bdp-fmq05 opt]# mongod -f /opt/configsvr/conf29012/config29012.yaml
  • 登入,進行初始化

注意 _id:副本集名稱要和config.yaml中的replSetName配置名稱相同; 只需初始化一次即可

檢視rs.status()時出現 "could not find member to sync from" ,等待一會就好了

回車等一會就會出現 "PRIMARY"

[root@bdp-fmq05 /]# mongo --port 29010
> rs.initiate({_id: "config",configsvr: true,members: [{ _id : 0, host : "66.1.30.31:29010" },{ _id : 1, host : "66.1.30.31:29011" },{ _id : 2, host : "66.1.30.31:29012" }]})
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1609244619, 1),
                "electionId" : ObjectId("000000000000000000000000")
        },
        "lastCommittedOpTime" : Timestamp(0, 0),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609244619, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609244619, 1)
}
  • 檢視叢集狀態
config:PRIMARY> rs.status();
{
        "set" : "config",
        "date" : ISODate("2020-12-29T12:23:56.203Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609244635, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:23:55.675Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609244635, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:23:55.675Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609244635, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609244635, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:23:55.675Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:23:55.675Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609244629, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-12-29T12:23:49.606Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609244619, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:23:49.617Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:23:50.587Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:29010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 205,
                        "optime" : {
                                "ts" : Timestamp(1609244635, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:23:55Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1609244629, 1),
                        "electionDate" : ISODate("2020-12-29T12:23:49Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:29011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 17,
                        "optime" : {
                                "ts" : Timestamp(1609244634, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609244634, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:23:54Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:23:54Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:23:55.612Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:23:54.618Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:29010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:29012",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 17,
                        "optime" : {
                                "ts" : Timestamp(1609244634, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609244634, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:23:54Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:23:54Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:23:55.611Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:23:54.617Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:29010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1609244619, 1),
                "electionId" : ObjectId("7fffffff0000000000000001")
        },
        "lastCommittedOpTime" : Timestamp(1609244635, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609244635, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609244635, 1)
}
  • 為config叢集新增root使用者

1、這裡必須要連線到primary節點下建立使用者,在secondary節點上建立使用者會報錯

2、連線上primary節點後,一定要切換到admin資料庫下建立root許可權使用者

config:PRIMARY> use admin
switched to db admin
config:PRIMARY> db.createUser({user: "cy_admin",pwd: "Admin@12341234",roles: [ { role: "root", db: "admin" } ]});
Successfully added user: {
        "user" : "cy_admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
config:PRIMARY> quit()
  • kill掉mongod服務程序,把config.yaml配置檔案中的security認證註釋開啟,重啟mongod服務
[root@bdp-fmq05 db]# kill `ps -ef|grep mongo|awk {'print $2'}`
[root@bdp-fmq05 db]# mongod -f /opt/configsvr/conf29010/config29010.yaml
[root@bdp-fmq05 db]# mongod -f /opt/configsvr/conf29011/config29011.yaml
[root@bdp-fmq05 db]# mongod -f /opt/configsvr/conf29012/config29012.yaml
  • 輸出如下
 #連線到mongod伺服器
[root@bdp-fmq05 db]# mongo --port 29010
 #切換到admin資料庫
config:PRIMARY> use admin
switched to db admin
 #登入,返回1說明登入成功
config:PRIMARY> db.auth('cy_admin','Admin@12341234')
1

 #登陸後驗證
config:PRIMARY> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
config:PRIMARY> rs.status()
{
        "set" : "config",
        "date" : ISODate("2020-12-29T12:28:21.848Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609244901, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:28:21.378Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609244901, 1),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:28:21.378Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609244901, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609244901, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:28:21.378Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:28:21.378Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609244879, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-12-29T12:27:10.354Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609244801, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:27:10.362Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:27:10.471Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:29010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 83,
                        "optime" : {
                                "ts" : Timestamp(1609244901, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:28:21Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1609244830, 1),
                        "electionDate" : ISODate("2020-12-29T12:27:10Z"),
                        "configVersion" : 1,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:29011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 80,
                        "optime" : {
                                "ts" : Timestamp(1609244899, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609244899, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:28:19Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:28:19Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:28:20.361Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:28:20.867Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:29010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:29012",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 78,
                        "optime" : {
                                "ts" : Timestamp(1609244899, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609244899, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:28:19Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:28:19Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:28:20.361Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:28:20.869Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:29010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                }
        ],
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(0, 0),
                "electionId" : ObjectId("7fffffff0000000000000002")
        },
        "lastCommittedOpTime" : Timestamp(1609244901, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609244901, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609244901, 1)
}
config:PRIMARY> quit()
5.1.2 啟動分片副本集Ⅰ
  • 使用yaml檔案啟動,先註釋掉sercurity認證
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27010/shard27010.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27011/shard27011.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27012/shard27012.yaml
  • 初始化(一主一從一仲裁)
[root@bdp-fmq05 db]# mongo --port 27010
 #注意 _id:副本集名稱要和shard.yaml中的replSetName配置相同;
> rs.initiate({_id : "shard1",members: [{ _id : 0, host : "66.1.30.31:27010" ,priority : 2 },{ _id : 1, host : "66.1.30.31:27011" ,priority : 1 },{ _id : 2, host : "66.1.30.31:27012" ,arbiterOnly :true }]})
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245083, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245083, 1)
}
  • 檢視狀態
shard1:PRIMARY> rs.status();
{
        "set" : "shard1",
        "date" : ISODate("2020-12-29T12:31:35.156Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609245094, 2),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:31:34.025Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609245094, 2),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:31:34.025Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609245094, 2),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609245094, 2),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:31:34.025Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:31:34.025Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609245094, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-12-29T12:31:33.993Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609245083, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 2,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:31:34.016Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:31:34.315Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:27010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 140,
                        "optime" : {
                                "ts" : Timestamp(1609245094, 2),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:31:34Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1609245093, 1),
                        "electionDate" : ISODate("2020-12-29T12:31:33Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:27011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 11,
                        "optime" : {
                                "ts" : Timestamp(1609245083, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609245083, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:31:23Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:31:23Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:31:33.995Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:31:34.805Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:27012",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 11,
                        "lastHeartbeat" : ISODate("2020-12-29T12:31:33.995Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:31:34.261Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245094, 2),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245094, 2)
}
  • 為share1新增root使用者(必須在PRIMARY狀態下,可以按回車鍵可以看到PRIMARY)
shard1:PRIMARY> use admin
switched to db admin
shard1:PRIMARY> db.createUser({user: "cy_admin",pwd: "Admin@12341234",roles: [ { role: "root", db: "admin" } ]});
Successfully added user: {
        "user" : "cy_admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
shard1:PRIMARY> quit()
  • kill掉mongod服務程序,把shard1.yaml配置檔案中的security認證註釋開啟,重啟mongod服務
[root@bdp-fmq05 db]# kill `ps -ef |grep 'mongod -f /opt/mongos/conf270'|awk {'print $2'}`
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27010/shard27010.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27011/shard27011.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf27012/shard27012.yaml
  • 輸出如下
 #連線到mongod伺服器
[root@bdp-fmq05 db]# mongo --port 27010
 #切換到admin資料庫
shard1:PRIMARY> use admin
switched to db admin
 #登入,返回1說明登入成功
shard1:PRIMARY> db.auth('cy_admin','Admin@12341234')
1

 #登陸後驗證
shard1:PRIMARY> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
shard1:PRIMARY> rs.status()
{
        "set" : "shard1",
        "date" : ISODate("2020-12-29T12:34:46.857Z"),
        "myState" : 1,
        "term" : NumberLong(3),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609245281, 1),
                        "t" : NumberLong(3)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:34:41.948Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609245281, 1),
                        "t" : NumberLong(3)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:34:41.948Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609245281, 1),
                        "t" : NumberLong(3)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609245281, 1),
                        "t" : NumberLong(3)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:34:41.948Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:34:41.948Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609245204, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-12-29T12:34:11.943Z"),
                "electionTerm" : NumberLong(3),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609245204, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 2,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:34:11.947Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:34:12.688Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:27010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 48,
                        "optime" : {
                                "ts" : Timestamp(1609245281, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:34:41Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1609245251, 1),
                        "electionDate" : ISODate("2020-12-29T12:34:11Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:27011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 45,
                        "optime" : {
                                "ts" : Timestamp(1609245281, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609245281, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:34:41Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:34:41Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:34:45.947Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:34:45.181Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:27010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:27012",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 43,
                        "lastHeartbeat" : ISODate("2020-12-29T12:34:45.946Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:34:44.981Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245281, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245281, 1)
}
shard1:PRIMARY> quit()
  • rs-1分片集搭建完成
5.1.3 啟動分片副本集Ⅱ
  • 使用yaml檔案啟動,先註釋掉sercurity認證
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf28010/shard28010.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf28011/shard28011.yaml
[root@bdp-fmq05 db]# mongod -f /opt/mongos/conf28012/shard28012.yaml
  • 初始化(一主一從一仲裁):
[root@bdp-fmq05 conf28010]# mongo --port 28010
> rs.initiate({_id : "shard2",members: [{ _id : 0, host : "66.1.30.31:28010" ,priority : 2 },{ _id : 1, host : "66.1.30.31:28011" ,priority : 1 },{ _id : 2, host : "66.1.30.31:28012" ,arbiterOnly :true }]})
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245615, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245615, 1)
}
  • 檢視狀態:
shard2:PRIMARY> rs.status();
{
        "set" : "shard2",
        "date" : ISODate("2020-12-29T12:40:34.222Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609245625, 4),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:40:25.574Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609245625, 4),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:40:25.574Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609245625, 4),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609245625, 4),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:40:25.574Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:40:25.574Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609245625, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-12-29T12:40:25.561Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609245615, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 2,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:40:25.570Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:40:26.282Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:28010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 330,
                        "optime" : {
                                "ts" : Timestamp(1609245625, 4),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:40:25Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1609245625, 1),
                        "electionDate" : ISODate("2020-12-29T12:40:25Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:28011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 19,
                        "optime" : {
                                "ts" : Timestamp(1609245625, 4),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609245625, 4),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:40:25Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:40:25Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:40:33.564Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:40:32.770Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:28010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:28012",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 19,
                        "lastHeartbeat" : ISODate("2020-12-29T12:40:33.564Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:40:33.220Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245625, 4),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245625, 4)
}
  • 為share2新增root許可權使用者(必須在PRIMARY狀態下):
shard2:PRIMARY> use admin
switched to db admin
shard2:PRIMARY> db.createUser({user: "cy_admin",pwd: "Admin@12341234",roles: [ { role: "root", db: "admin" } ]});
Successfully added user: {
        "user" : "cy_admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
shard2:PRIMARY> quit()
  • kill掉mongod服務程序,把shard2.yaml配置檔案中的security認證註釋開啟,重啟mongod服務:
[root@bdp-fmq05 conf28010]# kill `ps -ef |grep 'mongod -f /opt/mongos/conf280'|awk '{print $2}'`
[root@bdp-fmq05 conf28010]# mongod -f /opt/mongos/conf28010/shard28010.yaml
[root@bdp-fmq05 conf28010]# mongod -f /opt/mongos/conf28011/shard28011.yaml
[root@bdp-fmq05 conf28010]# mongod -f /opt/mongos/conf28012/shard28012.yaml
  • 輸出如下
 #連線到mongod伺服器
[root@bdp-fmq05 db]# mongo --port 28010
 #切換到admin資料庫
shard2:PRIMARY> use admin
switched to db admin
 #登入,返回1說明登入成功
config:PRIMARY> db.auth('cy_admin','Admin@12341234')
1

 #登陸後驗證
shard2:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
shard2:PRIMARY> rs.status()
{
        "set" : "shard2",
        "date" : ISODate("2020-12-29T12:46:04.951Z"),
        "myState" : 1,
        "term" : NumberLong(4),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1609245958, 1),
                        "t" : NumberLong(4)
                },
                "lastCommittedWallTime" : ISODate("2020-12-29T12:45:58.408Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1609245958, 1),
                        "t" : NumberLong(4)
                },
                "readConcernMajorityWallTime" : ISODate("2020-12-29T12:45:58.408Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1609245958, 1),
                        "t" : NumberLong(4)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1609245958, 1),
                        "t" : NumberLong(4)
                },
                "lastAppliedWallTime" : ISODate("2020-12-29T12:45:58.408Z"),
                "lastDurableWallTime" : ISODate("2020-12-29T12:45:58.408Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1609245928, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "priorityTakeover",
                "lastElectionDate" : ISODate("2020-12-29T12:44:58.403Z"),
                "electionTerm" : NumberLong(4),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1609245896, 1),
                        "t" : NumberLong(3)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1609245896, 1),
                        "t" : NumberLong(3)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 2,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-12-29T12:44:58.407Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-12-29T12:44:59.410Z")
        },
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(3),
                "lastVoteDate" : ISODate("2020-12-29T12:44:46.976Z"),
                "electionCandidateMemberId" : 1,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1609245855, 1),
                        "t" : NumberLong(1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1609245855, 1),
                        "t" : NumberLong(1)
                },
                "priorityAtElection" : 2
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "66.1.30.31:28010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 91,
                        "optime" : {
                                "ts" : Timestamp(1609245958, 1),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:45:58Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1609245898, 1),
                        "electionDate" : ISODate("2020-12-29T12:44:58Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "66.1.30.31:28011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 88,
                        "optime" : {
                                "ts" : Timestamp(1609245958, 1),
                                "t" : NumberLong(4)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1609245958, 1),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-12-29T12:45:58Z"),
                        "optimeDurableDate" : ISODate("2020-12-29T12:45:58Z"),
                        "lastHeartbeat" : ISODate("2020-12-29T12:46:04.406Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:46:03.478Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "66.1.30.31:28010",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                },
                {
                        "_id" : 2,
                        "name" : "66.1.30.31:28012",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 87,
                        "lastHeartbeat" : ISODate("2020-12-29T12:46:04.405Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-29T12:46:03.776Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : -1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609245958, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1609245958, 1)
}
shard2:PRIMARY> quit()
  • rs-2分片集搭建完成
5.1.4 啟動route路由叢集

啟動route路由叢集並整合conf叢集和shard叢集

  • 啟動route

注意:這裡啟動的時候和config叢集shard叢集不同,這裡的security認證要開啟,不然會報錯

[root@bdp-fmq05 /]# mongos -f /opt/mongoroute/mongoroute.yaml
  • 連線到mongo路由: 這裡的登入密碼就是config叢集配置的使用者名稱和密碼;埠為定義的27017,可以不指定,使用cy_admin賬戶登陸:
[root@bdp-fmq05 /]# mongo
 #切換到admin資料庫
mongos> use admin
switched to db admin
 #登入,返回1說明登入成功
mongos> db.auth('cy_admin','Admin@12341234')
1
5.1.5 新增shard分片叢集

把shard1、shard2分片叢集都新增進去,每個分片新增一個節點即可,主或從都可以

mongos> use admin
mongos> sh.addShard( "shard1/66.1.30.31:27010");
mongos> sh.addShard( "shard2/66.1.30.31:28010");
  • 輸出如下
 #切換到admin資料庫
mongos> use admin
switched to db admin
mongos> sh.addShard( "shard1/66.1.30.31:27010");
{
        "shardAdded" : "shard1",
        "ok" : 1,
        "operationTime" : Timestamp(1609253098, 3),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609253098, 3),
                "signature" : {
                        "hash" : BinData(0,"+i2UcW0+XtX0hnX7mXuGlAwq/NE="),
                        "keyId" : NumberLong("6911653052818653205")
                }
        }
}
mongos> sh.addShard( "shard2/66.1.30.31:28010");
{
        "shardAdded" : "shard2",
        "ok" : 1,
        "operationTime" : Timestamp(1609253115, 2),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1609253115, 2),
                "signature" : {
                        "hash" : BinData(0,"KBO8lLYcnjcWEVmwfYzVcpI/z3M="),
                        "keyId" : NumberLong("6911653052818653205")
                }
        }
}
  • 檢視叢集狀態
mongos> use admin
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5feb1fd5d08cb972b338cd4e")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/66.1.30.31:27010,66.1.30.31:27011",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/66.1.30.31:28010,66.1.30.31:28011",  "state" : 1 }
  active mongoses:
        "4.4.2" : 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 }

至此一個帶有許可權控制的可水平擴容的分片mongodb4.4.2版本叢集搭建完成。

6、資料匯入匯出

6.1 mongodb4.4版本單機資料備份

6.1.1匯出全部資料:
[root@bdp-fmq05 /]# mongodump --host=127.0.0.1 --port=27017 -o /home/mongodback/
6.1.2匯入全部資料:
[root@bdp-fmq05 /]# mongorestore --dir /data/mongoback/mongodback/ -u cy_admin -p Admin@12341234 --authenticationDatabase admin

6.2 開啟均衡器和分片功能

  • 開啟均衡器
mongos> sh.startBalancer()
{
	"ok" : 1,
	"operationTime" : Timestamp(1592882192, 3),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1592882192, 3),
		"signature" : {
			"hash" : BinData(0,"Irm0/RdXjYrJFzPNicDZKZiZy3w="),
			"keyId" : NumberLong("6841344940694306847")
		}
	}
}
 
檢視結果:
mongos> sh.status()
  • 開啟文件分片功能(對匯入的文件)
mongos> use admin
switched to db admin
mongos> show dbs
mongos> db.runCommand({enablesharding:"test"})
mongos> db.runCommand( { shardCollection : "test.fs.chunks" , key : { files_id : 1 , n : 1 } } )

6.3 資料庫配置sharding:

 #允許hyq1225資料庫進行sharding
mongos> sh.enableSharding("hyq1225")
 #對hyq1225.hyq1225集合以id列為shard key進行hashed sharding
mongos> sh.shardCollection("hyq1225.hyq1225",{id:"hashed"})
 #通過db.t.getIndexes()可以看到自動為id列建立了索引。
mongos> db.hyq1225.getIndexes()
 #在不同shard的primary上使用db.hyq1225.find().count()
mongos> db.hyq1225.find().count()
 #檢視分片結果
mongos> db.hyq1225.stats()
 #檢視本庫內所有集合的分片資訊
mongos> sh.status()
 #如果資料庫沒有開啟分片功能,需要先開啟
mongos> sh.enableSharding("hyq1225")
 #如果collection裡面已經有資料,需要先建立對應的shardkey索引
mongos> use hyq1225
mongos> db.myCollection.createIndex( {"id": "hashed"} )
 #對指定collection開啟shard
mongos> sh.shardCollection("hyq1225.hyq1225", {"id": "hashed"})