首页 > 数据库 >8.MongoDB系列之创建副本集(一)

8.MongoDB系列之创建副本集(一)

时间:2022-10-16 18:47:51浏览次数:73  
标签:副本 07 创建 -- MongoDB study NumberLong Timestamp localhost

1. 复制简介

在MongoDB中,创建副本集后就可以使用复制功能了,副本集是一组服务器,其中一个是用于处理写操作的主节点,还有多个用于保存主节点的数据副本的从节点,如果主节点崩溃了,则从节点会从中选举中一个新的主节点

2. 创建副本集
 mongod --replSet study --dbpath /home/data/rs1 --port 27018 --bind_ip_all --oplogSize 200
 mongod --replSet study --dbpath /home/data/rs2 --port 27019 --bind_ip_all --oplogSize 200
 mongod --replSet study --dbpath /home/data/rs3 --port 27020 --bind_ip_all --oplogSize 200
# 可以看到副本集已创建
# ps -ef|grep mongod
root       247   228  1 13:13 pts/2    00:00:00 mongod --replSet study --dbpath /home/data/rs1 --port 27018 --bind_ip_all --oplogSize 200
root       283   240  3 13:13 pts/3    00:00:00 mongod --replSet study --dbpath /home/data/rs2 --port 27019 --bind_ip_all --oplogSize 200
root       336   323  9 13:14 pts/4    00:00:00 mongod --replSet study --dbpath /home/data/rs3 --port 27020 --bind_ip_all --oplogSize 200

在MongoDB3.6中,mongod仅在默认情况下绑定到localhost(127.0.0.1),为了使副本集中每个成员都可以与其他成员进行通信,还必须绑定其他成员可以访问到的IP,如下

mongod --bind_ip localhost,192.168.0.12 --replSet study --dbpath /home/data/rs1 --port 27018 --oplogSize 200

到目前为止,每个mongod都不知道其他mongod的存在。为了能够彼此交互,需要创建一个包含每个成员的配置,并将此配置发送给其中一个mongod进程。它负责将此配置传播给其他成员

# 连接mongod
mongo --port 27018
# 查看当前状态
> rs.status()
{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "no replset config has been received",
        "code" : 94,
        "codeName" : "NotYetInitialized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
# 创建配置并初始化配置
> rsconf = {_id: "study", members: [{_id:0, host:"localhost:27018"},{_id:1, host:"localhost:27019"},{_id:2, host:"localhost:27020"}]}
> rs.initiate(rsconf)
# 再次查看状态
study:SECONDARY> rs.status()
{
        "set" : "study",
        "date" : ISODate("2022-07-10T05:36:36.198Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1657431395, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2022-07-10T05:36:35.602Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1657431395, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2022-07-10T05:36:35.602Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1657431395, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1657431395, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2022-07-10T05:36:35.602Z"),
                "lastDurableWallTime" : ISODate("2022-07-10T05:36:35.602Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1657431394, 3),
        "lastStableCheckpointTimestamp" : Timestamp(1657431394, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2022-07-10T05:36:34.192Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1657431383, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2022-07-10T05:36:34.225Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2022-07-10T05:36:35.558Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY", // 主节点
                        "uptime" : 1369,
                        "optime" : {
                                "ts" : Timestamp(1657431395, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-07-10T05:36:35Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1657431394, 1),
                        "electionDate" : ISODate("2022-07-10T05:36:34Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY", // 从节点
                        "uptime" : 12,
                        "optime" : {
                                "ts" : Timestamp(1657431383, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1657431383, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2022-07-10T05:36:23Z"),
                        "optimeDurableDate" : ISODate("2022-07-10T05:36:23Z"),
                        "lastHeartbeat" : ISODate("2022-07-10T05:36:34.202Z"),
                        "lastHeartbeatRecv" : ISODate("2022-07-10T05:36:35.519Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 12,
                        "optime" : {
                                "ts" : Timestamp(1657431383, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1657431383, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2022-07-10T05:36:23Z"),
                        "optimeDurableDate" : ISODate("2022-07-10T05:36:23Z"),
                        "lastHeartbeat" : ISODate("2022-07-10T05:36:34.202Z"),
                        "lastHeartbeatRecv" : ISODate("2022-07-10T05:36:35.521Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1657431395, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1657431395, 1)
}
3. 观察副本集
study:PRIMARY> use test
switched to db test
study:PRIMARY> for (i=0;i <1000;i++){db.coll.insert({count:i})}
WriteResult({ "nInserted" : 1 })
# 查看主节点
study:PRIMARY> db.isMaster()
{
        "hosts" : [
                "localhost:27018",
                "localhost:27019",
                "localhost:27020"
        ],
        "setName" : "study",
        "setVersion" : 1,
        "ismaster" : true,
        "secondary" : false,
        "primary" : "localhost:27018",
        "me" : "localhost:27018",
        "electionId" : ObjectId("7fffffff0000000000000001"),
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1657431964, 1),
                        "t" : NumberLong(1)
                },
                "lastWriteDate" : ISODate("2022-07-10T05:46:04Z"),
                "majorityOpTime" : {
                        "ts" : Timestamp(1657431964, 1),
                        "t" : NumberLong(1)
                },
                "majorityWriteDate" : ISODate("2022-07-10T05:46:04Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 100000,
        "localTime" : ISODate("2022-07-10T05:46:07.631Z"),
        "logicalSessionTimeoutMinutes" : 30,
        "connectionId" : 1,
        "minWireVersion" : 0,
        "maxWireVersion" : 8,
        "readOnly" : false,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1657431964, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1657431964, 1)
}
// 新开一个shell连接从节点
mongo --port 27020
study:SECONDARY> use test
switched to db test
// 执行查询可看到如下报错
study:SECONDARY> db.coll.find({})
Error: error: {
        "operationTime" : Timestamp(1657432094, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1657432094, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

从节点可能会落后主节点(延迟)而缺少最新的写入,所以默认情况下从节点会拒绝读请求,以防止读取过期数据,如果想让从节点查询,则可以设置

study:SECONDARY> db.setSlaveOk()
study:SECONDARY> db.coll.count({})
1000

从节点不接受写操作。从节点只能通过复制功能写入数据,不接受客户端的写请求

study:SECONDARY> db.coll.insert({count: 33})
WriteCommandError({
        "operationTime" : Timestamp(1657432434, 1),
        "ok" : 0,
        "errmsg" : "not master",
        "code" : 10107,
        "codeName" : "NotMaster",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1657432434, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
})

让主节点停止,发现从节点27020成为了主节点,实现了自动选举主节点

study:PRIMARY> db.adminCommand({'shutdown': 1})
study:SECONDARY> db.isMaster()
{
        "hosts" : [
                "localhost:27018",
                "localhost:27019",
                "localhost:27020"
        ],
        "setName" : "study",
        "setVersion" : 1,
        "ismaster" : false,
        "secondary" : true,
        "primary" : "localhost:27019",
        "me" : "localhost:27020",
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1657432711, 1),
                        "t" : NumberLong(2)
                },
                "lastWriteDate" : ISODate("2022-07-10T05:58:31Z"),
                "majorityOpTime" : {
                        "ts" : Timestamp(1657432711, 1),
                        "t" : NumberLong(2)
                },
                "majorityWriteDate" : ISODate("2022-07-10T05:58:31Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 100000,
        "localTime" : ISODate("2022-07-10T05:58:34.144Z"),
        "logicalSessionTimeoutMinutes" : 30,
        "connectionId" : 20,
        "minWireVersion" : 0,
        "maxWireVersion" : 8,
        "readOnly" : false,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1657432711, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1657432711, 1)
}
4. 更改副本集配置
// 添加副本集
study:PRIMARY> rs.add('localhost:27021')
// 移除副本集
study:PRIMARY> rs.remove('localhost:27018')
// 查看配置是否生效
study:PRIMARY> rs.config()
{
        "_id" : "study",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 1,
                        "host" : "localhost:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

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

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 3,
                        "host" : "localhost:27021",
                        "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("62ca6557fe65de5513877e3c")
        }
}
# 修改配置
study:PRIMARY> var config = rs.config()
study:PRIMARY> config.members[0].host = 'localhost:27019'
localhost:27019

欢迎关注公众号算法小生沈健的技术博客

标签:副本,07,创建,--,MongoDB,study,NumberLong,Timestamp,localhost
From: https://www.cnblogs.com/shenjian-online/p/16796727.html

相关文章

  • 9.MongoDB系列之创建副本集(二)
    1.如何设计副本集大多数:选取主节点时需要由大多数决定,主节点只有在得到大多数支持时才能继续作为主节点,写操作被复制到大多数成员时就是安全的写操作。这里的大多数定义......
  • 10.MongoDB系列之副本集组成
    1.同步复制是指多台服务器保持相同的数据副本。MongoDB通过保存操作日志(oplog)实现复制功能。oplog存在于主节点local数据库中的一个固定集合,包含了主节点执行的每一次......
  • Docker安装MongoDB并使用Navicat连接
    MongoDB简介:MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。是一个介于关系数据库和非关系数据库之间......
  • MongoDb基本命令
    基本命令查看当前有哪些数据库#方法1showdatabases#方法2showdbs创建数据库/选择数据库usetest2#如果当前没有test2数据库就会创建test2并切换,有的话直接......
  • 创建Django项目的两种方式
    有两种方式可创建django项目:方式一:命令行1.cmd命令行,进入到指定的目录,执行:django-adminstartproject mydiary【mydiary为项目名】,创建成功之后,出现如下:mydiary/......
  • DEMO: MIRO 根据物料创建贷项凭证BAPI_INCOMINGINVOICE_CREATE
    QQ群:SAP干货铺, 群号:775662808所有群管理严格,严格禁止一切外来链接、招聘、广告等垃圾信息!MIRODemo*&---------------------------------------------------------------......
  • DEMO: MIRO 根据退货PO创建贷项凭证BAPI_INCOMINGINVOICE_CREATE
    QQ群:SAP干货铺, 群号:775662808所有群管理严格,严格禁止一切外来链接、招聘、广告等垃圾信息!POMIRO或者不勾选计算税额,手动输入税额。demo*&-----------------------------......
  • 在运行时创建一个对话框模板
    在之前的系列文章中,我们花了很长一段时间来学习了对话框模板和对话框管理器。现在,让我们将之前所需到的各个知识点融合一下,来做一些有意思的事情,例如:在程序运行期间创建一......
  • Mongodb安装
    下载地址https://www.mongodb.com/try/download/community安装步骤先安装依赖sudoyuminstalllibcurlopensslxz-libs解压tar包tar-zxvfmongodb-linux-*-5......
  • Ansible 创建用户配置密码通过哈希加密的方式
    创建用户配置密码通过哈希加密的方式  [libin@libinansible]$vimrhca447-26.yaml----name:createuserandset  hosts:web  tasks:  -name:c......