用户工具

站点工具


docs:mongodb:High_availability_and_redundancy

Base on MongoDB v4.2

The official manual

Deploy a Replica Set

在未启用访问授权的情况下,部署Replica Set:1 Primary,1 Secendary, 1 A

Procedure

• 添加参数replSet

# 在所有node启动参数中添加参数 --replSet "myreplSet" 以指定复制集_id,所有节点保持一致。后面将通过此_id调用和认证。
mongod --replSet "myreplSet"  --dbpath /opt/mongodb-4.2/mongodb_1 --bind_ip 100.64.88.148 --port 27117 --logpath /opt/mongodb-4.2/mongodb_1/mongod.log --fork 
配置文件中添加如下参数

net:
  bindIp: 100.64.88.148
  port: 27117
processManagement:
  fork: true
replication:
  replSetName: myreplSet
storage:
  dbPath: /opt/mongodb-4.2/mongodb_1
systemLog:
  destination: file
  path: /opt/mongodb-4.2/mongodb_1/mongod.log

Connect & Deploy MongoDB replSet

# Connect & Deploy MongoDB replSet
mongo --host 100.64.88.148 --port 27117
MongoDB shell version v4.2.3-46-g2503dfb
connecting to: mongodb://100.64.88.148:27117/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ce11de57-a519-4d24-8cea-cb988db6c0a5") }
MongoDB server version: 4.2.3-46-g2503dfb
Server has startup warnings:
2020-02-16T14:33:53.385+0800 I  CONTROL  [initandlisten]
2020-02-16T14:33:53.385+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-02-16T14:33:53.385+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-02-16T14:33:53.385+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-02-16T14:33:53.385+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---





初始化

# 直接初始化
mongo> rs.initiate(
{
        "_id" : "myreplSet",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "100.64.88.148:27117"
                },
                {
                        "_id" : 1,
                        "host" : "100.64.88.148:27217"
                },
                {
                        "_id" : 2,
                        "host" : "100.64.88.148:27317",
                        "arbiterOnly" : true
                }
        ]
}
)

# 分步初始化
mongo>config = {
...     "_id":"myreplSet",
...     "members":[
...       {"_id":0,"host":"100.64.88.148:27117"},
...       {"_id":1,"host":"100.64.88.148:27217"},
...       {"_id":2,"host":"100.64.88.148:27317",arbiterOnly:true}
...     ]
...     }
{
        "_id" : "myreplSet",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "100.64.88.148:27117"
                },
                {
                        "_id" : 1,
                        "host" : "100.64.88.148:27217"
                },
                {
                        "_id" : 2,
                        "host" : "100.64.88.148:27317",
                        "arbiterOnly" : true
                }
        ]
}

mongo> rs.initiate(config);
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1581834982, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1581834982, 1)
}

状态验证

# 状态验证
myreplSet:SECONDARY> rs.status();
{
        "set" : "myreplSet",
        "date" : ISODate("2020-02-16T06:36:40.602Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1581834994, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-02-16T06:36:34.295Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1581834994, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-02-16T06:36:34.295Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1581834994, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1581834994, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-02-16T06:36:34.295Z"),
                "lastDurableWallTime" : ISODate("2020-02-16T06:36:34.295Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1581834993, 2),
        "lastStableCheckpointTimestamp" : Timestamp(1581834993, 2),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-02-16T06:36:32.859Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1581834982, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-02-16T06:36:33.709Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-02-16T06:36:34.285Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "100.64.88.148:27117",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 168,
                        "optime" : {
                                "ts" : Timestamp(1581834994, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-02-16T06:36:34Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1581834992, 1),
                        "electionDate" : ISODate("2020-02-16T06:36:32Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "100.64.88.148:27217",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 17,
                        "optime" : {
                                "ts" : Timestamp(1581834994, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1581834994, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-02-16T06:36:34Z"),
                        "optimeDurableDate" : ISODate("2020-02-16T06:36:34Z"),
                        "lastHeartbeat" : ISODate("2020-02-16T06:36:38.862Z"),
                        "lastHeartbeatRecv" : ISODate("2020-02-16T06:36:40.285Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "100.64.88.148:27117",
                        "syncSourceHost" : "100.64.88.148:27117",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "100.64.88.148:27317",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 17,
                        "lastHeartbeat" : ISODate("2020-02-16T06:36:38.862Z"),
                        "lastHeartbeatRecv" : ISODate("2020-02-16T06:36:39.199Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1581834994, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1581834994, 1)
}

查看replSet配置

# 查看replSet配置
myreplSet:PRIMARY> rs.conf();
{
        "_id" : "myreplSet",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "100.64.88.148:27117",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

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

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "100.64.88.148:27317",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "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("5e48e2e6134bb0c804416f86")
        }
}
myreplSet:PRIMARY>

Testing Deploy

SEE ALSO

The documentation of the following shell functions for more information:

You may also consider the simple setup script as an example of a basic automatically-configured replica set.

Refer to Replica Set Read and Write Semantics for a detailed explanation of read and write semantics in MongoDB. https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/

Deploy Replica Set With Keyfile AuthenticationDeploy Replica Set With Keyfile Authentication
docs/mongodb/High_availability_and_redundancy.txt · 最后更改: 2020/09/25 21:49 (modify by Amos)