前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB复制集部署

MongoDB复制集部署

作者头像
十毛
发布2019-05-10 17:32:08
1.4K0
发布2019-05-10 17:32:08
举报

MongoDB的高可用特使是用复制集实现的,本文介绍如何在CentOS7快速搭建一个复制集

部署单节点版本


yum安装mongo程序

  • 添加yum服务器 /etc/yum.repos.d/mongodb-org-4.0.repo
代码语言:javascript
复制
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
  • 安装mongodb
代码语言:javascript
复制
sudo yum install -y mongodb-org

安装后会创建两个默认的文件夹 /var/lib/mongo(数据目录) /var/log/mongodb (日志目录)

运行mongod

代码语言:javascript
复制
# 启动
sudo systemctl start mongod.service
# 开机启动
sudo systemctl enable mongod.service

使用

代码语言:javascript
复制
mongo

部署复制集


  • 环境说明:部署在一台服务器上,目录分别是/home/tenmao/mongo_repl/mongo{1,2,3},端口分别是27017, 27027, 27037

配置文件

home/tenmao/mongo_repl/mongo1/mongod.conf

其他两个节点的配置,就是端口和目录地址不一样

代码语言:javascript
复制
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/tenmao/mongo_repl/mongo1/mongod.log

# Where and how to store data.
storage:
  dbPath: /home/tenmao/mongo_repl/mongo1/db
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /home/tenmao/mongo_repl/mongo1/mongod.pid  # location of pidfile
  tenmaoeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: 0.0.0.0  # 绑定到所有IP地址,支持其他服务器上的客户端访问

replication:
  replSetName: "tenmao_mongo" #集群的名字

初始化复制集

  • 启动3个实例
代码语言:javascript
复制
# 启动服务器
mongod -config mongo1/mongod.conf
mongod -config mongo2/mongod.conf
mongod -config mongo3/mongod.conf
# 连接到一台服务器
mongo --port 27017
> rs.status()
{
    "operationtenmaoe" : tenmaoestamp(0, 0),
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized",
    "$clustertenmaoe" : {
        "clustertenmaoe" : tenmaoestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
  • 复制集初始化
代码语言:javascript
复制
# 错误的初始化
> rs.initiate()
{
    "operationtenmaoe" : tenmaoestamp(0, 0),
    "ok" : 0,
    "errmsg" : "No host described in new configuration 1 for replica set tenmao_mongo maps to this node",
    "code" : 93,
    "codeName" : "InvalidReplicaSetConfig",
    "$clustertenmaoe" : {
        "clustertenmaoe" : tenmaoestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

# 正确的初始化
> rs.initiate( {
...    _id : "tenmao_mongo",
...    members: [
...       { _id: 0, host: "localhost:27017" },
...       { _id: 1, host: "localhost:27027" },
...       { _id: 2, host: "localhost:27037" }
...    ]
... })
{
    "ok" : 1,
    "operationtenmaoe" : tenmaoestamp(1556284500, 1),
    "$clustertenmaoe" : {
        "clustertenmaoe" : tenmaoestamp(1556284500, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

使用

  • 主节点插入数据
代码语言:javascript
复制
tenmao_mongo:PRIMARY> use blog
switched to db blog
tenmao_mongo:PRIMARY> db.article.insert({"title": "hello world", "content": "this the first mongo record"})
WriteResult({ "nInserted" : 1 })
  • 从节点读取
代码语言:javascript
复制
tenmao_mongo:SECONDARY> use blog
switched to db blog
tenmao_mongo:SECONDARY> rs.slaveOk()
tenmao_mongo:SECONDARY> db.article.find()
{ "_id" : ObjectId("5cc30509b507e7303e2d8230"), "title" : "hello world", "content" : "this the first mongo record" }

复制集管理常用命令

代码语言:javascript
复制
tenmao_mongo:PRIMARY> rs.help()
    rs.status()                                { replSetGetStatus : 1 } checks repl set status
    rs.initiate()                              { replSetInitiate : null } initiates set with default settings
    rs.initiate(cfg)                           { replSetInitiate : cfg } initiates set with configuration cfg
    rs.conf()                                  get the current configuration object from local.system.replset
    rs.reconfig(cfg)                           updates the configuration of a running replica set with cfg (disconnects)
    rs.add(hostportstr)                        add a new member to the set with default attributes (disconnects)
    rs.add(membercfgobj)                       add a new member to the set with extra attributes (disconnects)
    rs.addArb(hostportstr)                     add a new member which is arbiterOnly:true (disconnects)
    rs.stepDown([stepdownSecs, catchUpSecs])   step down as primary (disconnects)
    rs.syncFrom(hostportstr)                   make a secondary sync from the given member
    rs.freeze(secs)                            make a node ineligible to become primary for the tenmaoe specified
    rs.remove(hostportstr)                     remove a host from the replica set (disconnects)
    rs.slaveOk()                               allow queries on secondary nodes

    rs.printReplicationInfo()                  check oplog size and tenmaoe range
    rs.printSlaveReplicationInfo()             check replica set members and replication lag
    db.isMaster()                              check who is primary

常见错误

  • ERROR: child process failed, exited with error number 100:查看mongo日志,可以找到问题,一般是数据目录不存在,需要手工创建
  • not master and slaveOk=false: 因为默认情况只能从主节点读取数据,因为从节点数据可能过时。可以通过rs.slaveOk()

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 部署单节点版本
    • yum安装mongo程序
      • 运行mongod
        • 使用
        • 部署复制集
          • 配置文件
            • 初始化复制集
              • 使用
                • 复制集管理常用命令
                • 常见错误
                • 参考
                相关产品与服务
                数据库
                云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档