阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MongoDB 副本集配置详解

181次阅读
没有评论

共计 5168 个字符,预计需要花费 13 分钟才能阅读完成。

MongoDB 复制是将数据同步在多个服务器的过程。复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性。
复制还允许您从硬件故障和服务中断中恢复数据。

创建副本集时注意

  1. 版本: 各副本集服务器的 MongoDB 版本一致或支持同样的 replSet 功能
  2. 网络: 副本集内的每个成员都必须能够连接到其他成员(包括自身), 启动时注意 bind_ip –bind_ip_all.

配置副本集

  • 1 建立配置文件

mongodb.conf

  1 # Start MongoDB as a daemon on port 27017                                                                                                                 
  2
  3 port = 27017
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db
  7 logpath = /datatest/db/test.log
  8 logappend = true
  9

mongodb2.conf

  2
  3 port = 27017
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db
  7 logpath = /datatest/db/test.log
  8 logappend = true
  9

mongodb3.conf

  2
  3 port = 27017
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db
  7 logpath = /datatest/db/test.log
  8 logappend = true
  9

  • 2 启动数据库

mac-abeen:bin abeen$ sudo ./mongod -f mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37181
child process started successfully, parent exiting
mac-abeen:bin abeen$ sudo ./mongod -f mongodb2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37185
child process started successfully, parent exiting
mac-abeen:bin abeen$ sudo ./mongod -f mongodb3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37189
child process started successfully, parent exiting

  • 3 初始化副本集

mac-abeen:bin abeen$ ./mongo –nodb
MongoDB shell version: 3.2.8
> config = {}
{}
> config = {“_id”: “test_replica_set”, “members”: [
        {“_id”: 0, “host”: “192.168.0.10:27017”},
        {“_id”: 1, “host”: “192.168.0.20:27017”},
        {“_id”: 2, “host”: “192.168.0.30:27017”}]}

{
    “_id” : “test_replica_set”,
    “members” : [
        {
            “_id” : 0,
            “host” : “192.168.0.10:27017”
        },
        {
            “_id” : 1,
            “host” : “192.168.0.20:27017”
        },
        {
            “_id” : 2,
            “host” : “192.168.0.30:27017”
        }
    ]
}
> db = (new Mongo(“192.168.0.10:27017”)).getDB(“test”)
test
> rs.initiate(config)
{“ok” : 1}
test_replica_set:OTHER>

test_replica_set:PRIMARY> rs.config()
{
    “_id” : “test_replica_set”,
    “version” : 1,
    “protocolVersion” : NumberLong(1),
    “members” : [
        {
            “_id” : 0,
            “host” : “192.168.0.10:27017”,
            “arbiterOnly” : false,
            “buildIndexes” : true,
            “hidden” : false,
            “priority” : 1,
            “tags” : {
               
            },
            “slaveDelay” : NumberLong(0),
            “votes” : 1
        },
        {
            “_id” : 1,
            “host” : “192.168.0.20:27017”,
            “arbiterOnly” : false,
            “buildIndexes” : true,
            “hidden” : false,
            “priority” : 1,
            “tags” : {
               
            },
            “slaveDelay” : NumberLong(0),
            “votes” : 1
        },
        {
            “_id” : 2,
            “host” : “192.168.0.30:27017”,
            “arbiterOnly” : false,
            “buildIndexes” : true,
            “hidden” : false,
            “priority” : 1,
            “tags” : {
               
            },
            “slaveDelay” : NumberLong(0),
            “votes” : 1
        }
    ],
    “settings” : {
        “chainingAllowed” : true,
        “heartbeatIntervalMillis” : 2000,
        “heartbeatTimeoutSecs” : 10,
        “electionTimeoutMillis” : 10000,
        “getLastErrorModes” : {
           
        },
        “getLastErrorDefaults” : {
            “w” : 1,
            “wtimeout” : 0
        },
        “replicaSetId” : ObjectId(“5850f445c8cacd70496883b0”)
    }
}

  • 4 写入数据并查看副本集数据

test_replica_set:PRIMARY>
test_replica_set:PRIMARY> for (i=0; i < 100; i++){db.coll.insert({“count”: i})}
WriteResult({“nInserted” : 1})
test_replica_set:PRIMARY> db.coll.count()
100
test_replica_set:PRIMARY> db.coll.find().limit(5)
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b51”), “count” : 0 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b52”), “count” : 1 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b53”), “count” : 2 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b54”), “count” : 3 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b55”), “count” : 4 }

  • 5 查看副本集是否有数据

mac-abeen:bin abeen$ ./mongo –port 27017 –host 192.168.0.20
MongoDB shell version: 3.2.8
connecting to: 192.168.0.20:27017/test

test_replica_set:SECONDARY> db.coll.find().limit(5)
Error: error: {“ok” : 0, “errmsg” : “not master and slaveOk=false”, “code” : 13435}
设置连接可读取数据
test_replica_set:SECONDARY> db.setSlaveOk()
副本中已有数据
test_replica_set:SECONDARY> db.coll.find().limit(5)
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b53”), “count” : 2 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b55”), “count” : 4 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b52”), “count” : 1 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b54”), “count” : 3 }
{“_id” : ObjectId(“5850f5f35f1a7d82c0b45b51”), “count” : 0 }

修改副本集

rs.add(“server-4:27017”)    // 添加
rs.addArb(“server-4:27017”) // 添加选举仲裁者
rs.add({“_id”: 4, “host”: “server-4:27017”, “arbiterOnly”: true) // 添加选举仲裁者
rs.add({“_id”: 5, “host”: “server-5:27017”, “priority”: 0, “hidden”: true) // 添加隐藏成员
rs.remove(“server-4:27017”) // 移除

修改副本集, 通过 rs.reconfig

rs.reconfig 修改副本集成员时限制

  • 不能修改成员的_id 字段
  • 不能将接收 rs.reconfig 命令的成员 (通常是主节点) 的优先级设为 0
  • 不能仲裁者成员变为非仲裁者成员, 反之亦然.
  • 不能将 buildIndexes: false 的成员修改为 buildIndexes: true
    可以修改其他, 比如 host

var config = rs.config()
config.members[1].host = “newsserver:27017” // 修改 host
rs.reconfig(config)

更多 MongoDB 相关教程见以下内容

在 Azure 虚拟机上快速搭建 MongoDB 集群  https://www.linuxidc.com/Linux/2017-09/146778.htm
MongoDB 复制集原理  https://www.linuxidc.com/Linux/2017-09/146670.htm
MongoDB 3.4 远程连接认证失败  https://www.linuxidc.com/Linux/2017-06/145070.htm
Ubuntu 16.04 中安装 MongoDB3.4 数据库系统  https://www.linuxidc.com/Linux/2017-07/145526.htm
MongoDB 权威指南第 2 版 PDF 完整带书签目录 下载见 https://www.linuxidc.com/Linux/2016-12/138253.htm
如何在 Ubuntu 18.04 LTS 上安装和配置 MongoDB https://www.linuxidc.com/Linux/2018-05/152253.htm

MongoDB 日常运维操作命令集锦 https://www.linuxidc.com/Linux/2018-08/153631.htm

Linux 下 MongoDB 安装和配置详解  https://www.linuxidc.com/Linux/2018-08/153637.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计5168字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中