[root@wallet01 ~]# vim /etc/mongod.conf 
replication:
  replSetName: "rs01"
  oplogSizeMB: 2048
[root@wallet01 ~]# systemctl restart mongod

[root@wallet02 ~]# vim /etc/mongod.conf 
replication:
  replSetName: "rs01"
  oplogSizeMB: 2048
[root@wallet02 ~]# systemctl restart mongod

[root@wallet03 ~]# vim /etc/mongod.conf 
replication:
  replSetName: "rs01"
  oplogSizeMB: 2048
[root@wallet03 ~]# systemctl restart mongod

[root@wallet01 ~]# mongo
> rs.initiate() 
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "192.168.40.31:27017",
        "ok" : 1,
        "operationTime" : Timestamp(1621403780, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621403780, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

rs01:PRIMARY> rs.add("192.168.40.32:27017")
{
        "ok" : 1,
        "operationTime" : Timestamp(1621404239, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621404239, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

rs01:PRIMARY> rs.add("192.168.40.33:27017")
{
        "ok" : 1,
        "operationTime" : Timestamp(1621404762, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621404762, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

rs01:PRIMARY> rs.conf()
{
        "_id" : "rs01",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.40.31:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "192.168.40.32:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "192.168.40.33:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("60a4a884a2c4b7f2c1c729f4")
        }
}

rs01:PRIMARY> rs.status()
{
        "set" : "rs01",
        "date" : ISODate("2021-05-19T06:14:34.410Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1621404870, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1621404870, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1621404870, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1621404870, 1),
                        "t" : NumberLong(1)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1621404860, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2021-05-19T05:56:20.654Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1621403780, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2021-05-19T05:56:20.656Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-05-19T05:56:20.671Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.40.31:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1149,
                        "optime" : {
                                "ts" : Timestamp(1621404870, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-19T06:14:30Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1621403780, 2),
                        "electionDate" : ISODate("2021-05-19T05:56:20Z"),
                        "configVersion" : 3,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "192.168.40.32:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 635,
                        "optime" : {
                                "ts" : Timestamp(1621404870, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621404870, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-19T06:14:30Z"),
                        "optimeDurableDate" : ISODate("2021-05-19T06:14:30Z"),
                        "lastHeartbeat" : ISODate("2021-05-19T06:14:32.605Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-19T06:14:33.611Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "192.168.40.31:27017",
                        "syncSourceHost" : "192.168.40.31:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "192.168.40.33:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 111,
                        "optime" : {
                                "ts" : Timestamp(1621404870, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621404870, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-19T06:14:30Z"),
                        "optimeDurableDate" : ISODate("2021-05-19T06:14:30Z"),
                        "lastHeartbeat" : ISODate("2021-05-19T06:14:32.605Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-19T06:14:33.757Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "192.168.40.31:27017",
                        "syncSourceHost" : "192.168.40.31:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1621404870, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621404870, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

rs01:PRIMARY> db.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.40.32:27017
        syncedTo: Wed May 19 2021 14:16:20 GMT+0800 (CST)
        0 secs (0 hrs) behind the primary 
source: 192.168.40.33:27017
        syncedTo: Wed May 19 2021 14:16:20 GMT+0800 (CST)
        0 secs (0 hrs) behind the primary 

rs01:PRIMARY> cfg = rs.conf()
rs01:PRIMARY> cfg.members[0].priority = 5
5
rs01:PRIMARY> rs.reconfig(cfg)
{
        "ok" : 1,
        "operationTime" : Timestamp(1621476164, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621476164, 1),
                "signature" : {
                        "hash" : BinData(0,"Xn5HgUIEUAb6oaTIZwOcludInCM="),
                        "keyId" : NumberLong("6963920330909810691")
                }
        }
}
rs01:PRIMARY> use admin
switched to db admin
rs01:PRIMARY> db.createUser ({ user: "root", pwd: "abcd.1234",roles: ["root"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

[root@wallet01 ~]# openssl rand -base64 666 > /var/lib/mongo/keyfile
[root@wallet01 ~]# chown mongod:mongod /var/lib/mongo/keyfile
[root@wallet01 ~]# chmod 600 /var/lib/mongo/keyfile
[root@wallet01 ~]# ll /var/lib/mongo/keyfile
-rw------- 1 mongod mongod 902 May 20 09:48 /var/lib/mongo/keyfile

[root@wallet01 ~]# scp /var/lib/mongo/keyfile root@192.168.40.32:/var/lib/mongo/
[root@wallet01 ~]# scp /var/lib/mongo/keyfile root@192.168.40.33:/var/lib/mongo/

[root@wallet02 ~]# chown mongod:mongod /var/lib/mongo/keyfile
[root@wallet03 ~]# chown mongod:mongod /var/lib/mongo/keyfile

[root@wallet01 ~]# vim /etc/mongod.conf 
security:
  authorization: enabled
  clusterAuthMode: keyFile
  keyFile: /var/lib/mongo/keyfile
[root@wallet01 ~]# systemctl restart mongod

[root@wallet02 ~]# vim /etc/mongod.conf 
security:
  authorization: enabled
  clusterAuthMode: keyFile
  keyFile: /var/lib/mongo/keyfile
[root@wallet02 ~]# systemctl restart mongod

[root@wallet03 ~]# vim /etc/mongod.conf 
security:
  authorization: enabled
  clusterAuthMode: keyFile
  keyFile: /var/lib/mongo/keyfile
[root@wallet03 ~]# systemctl restart mongod

[root@wallet01 ~]# mongo -u "root" -p "abcd.1234" --authenticationDatabase "admin" 192.168.40.31
MongoDB shell version v4.0.21
connecting to: mongodb://192.168.40.201:27017/test?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("01307bb4-5f68-480a-bd36-3e01110f66b3") }
MongoDB server version: 4.0.21
rs01:PRIMARY> for(var i=0;i<10000;i++){db.customer.insert({"name":"user"+i})}
WriteResult({ "nInserted" : 1 })
rs01:PRIMARY> db.customer.count()
10000

[root@wallet02 ~]# mongo -u "root" -p "abcd.1234" --authenticationDatabase "admin" 192.168.40.32
MongoDB shell version v4.0.21
connecting to: mongodb://192.168.40.202:27017/test?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e1a5d332-4101-4a94-8e68-a2631a8d18eb") }
MongoDB server version: 4.0.21
rs01:SECONDARY> rs.secondaryOk()
rs01:SECONDARY> db.customer.count()
10000