• 周一. 4月 29th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

mongo配置复制集

admin

11月 28, 2021

前提

docker运行了三个容器,每个容器跑一个mongo最新的镜像。

1. 运行三个容器,指定复制集的形式

# 运行三个容器
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m3 -p 57017:27017 -d mongo --replSet "rs"
8ef9fcffdbabbe62d77672528ea008e8429de57858c7115f03bc5ea5094fe570
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m2 -p 47017:27017 -d mongo --replSet "rs"
cf2c41c04a79fac5df8711ca53c650d52c76f5532320cc8c24efc6f854341887
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m1 -p 37017:27017 -d mongo --replSet "rs"
39cf4ca9766bd334181eacbe41f0ffbfad83676ece9ff78afa0bac6e74e0efc7

2. 任意一个容器中配置主节点和从节点

# 进入其中一个容器
root@iZwz9434lxf5ptexiu13giZ:~# docker exec -it m2 /bin/bash
# 执行mongo进入shell
root@cf2c41c04a79:/# mongo 
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9677e9f0-5070-4c1f-9d86-7e54d1538ecd") }
MongoDB server version: 4.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2021-05-24T15:05:16.337+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-05-24T15:05:17.094+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        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()
---
# 配置主从节点,host为ip+端口,这里配置的是docker虚拟网卡的地址,真实环境里面配置阿里云远端服务器的公网ip也行。
> rs.initiate({"_id":"rs",members:[{_id:0,host:"172.17.0.1:37017"},{_id:1,host:"172.17.0.1:47017"},{_id:2,host:"172.17.0.1:57017"}]})
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621868882, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1621868882, 1)
}
# 从节点会自动选举到主节点---需要等待几秒钟
rs:SECONDARY> 
rs:PRIMARY> rs.status()  # 查看配置级状态
{
        "set" : "rs",
        "date" : ISODate("2021-05-24T15:08:27.963Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "lastDurableWallTime" : ISODate("2021-05-24T15:08:14.569Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1621868892, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2021-05-24T15:08:12.941Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1621868882, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-05-24T15:08:12.992Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-05-24T15:08:14.488Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.17.0.1:37017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 25,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "optimeDurableDate" : ISODate("2021-05-24T15:08:14Z"),
                        "lastHeartbeat" : ISODate("2021-05-24T15:08:26.955Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-24T15:08:26.473Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "172.17.0.1:47017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "172.17.0.1:47017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 191,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1621868892, 1),
                        "electionDate" : ISODate("2021-05-24T15:08:12Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "172.17.0.1:57017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 25,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "optimeDurableDate" : ISODate("2021-05-24T15:08:14Z"),
                        "lastHeartbeat" : ISODate("2021-05-24T15:08:26.960Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-24T15:08:26.471Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "172.17.0.1:47017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621868894, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1621868894, 1)
}
# 主节点插入数据
rs:PRIMARY> use test
switched to db test
rs:PRIMARY> db.test.insert({"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })
rs:PRIMARY> db.test.find()
{ "_id" : ObjectId("60abc28e51c11e78886399a0"), "name" : "zhangsan" }
rs:PRIMARY> exit
bye
# 进入从节点,查看主节点数据
root@cf2c41c04a79:/# mongo 172.17.0.1:57017
MongoDB shell version v4.4.6
connecting to: mongodb://172.17.0.1:57017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("83d2dea9-1011-4ee0-a7bb-e3b58143cf09") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting: 
        2021-05-24T15:05:04.708+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-05-24T15:05:05.573+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        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()
---
rs:SECONDARY> use test
switched to db test
# 查看失败,从节点默认没有读和写的权限
rs:SECONDARY> db.test.find()
Error: error: {
        "topologyVersion" : {
                "processId" : ObjectId("60abc0a04de154c436394d5f"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1621869283, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotPrimaryNoSecondaryOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621869283, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
# 从节点开启读写权限
rs:SECONDARY> rs.sliverOk()
uncaught exception: TypeError: rs.sliverOk is not a function :
@(shell):1:1
rs:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
# 从节点查看数据成功
rs:SECONDARY> db.test.find()
{ "_id" : ObjectId("60abc28e51c11e78886399a0"), "name" : "zhangsan" }
rs:SECONDARY> exit
bye
root@cf2c41c04a79:/# mongo 172.17.0.1:37017/test
MongoDB shell version v4.4.6
connecting to: mongodb://172.17.0.1:37017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9ca87c84-778b-4593-8299-4f7174da88d7") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting: 
        2021-05-24T15:05:25.351+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-05-24T15:05:26.116+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        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()
---
rs:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
rs:SECONDARY> db.test.find()
{ "_id" : ObjectId("60abc28e51c11e78886399a0"), "name" : "zhangsan" }
rs:SECONDARY>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注