mongo-4.4单实例搭建分片集群

官网地址 中文地址 下载地址

1、安装背景

由于原开发服务器mongodb数据库为单机安装,为了提高访问效率和数据安全性,需要配置成单机分片集群。

1.1 实验环境

1.1.1 分片集群架构官图

mongodb 鲲鹏主机_vim

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"})

落叶聚还散,寒鸦栖复惊。