侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MongoDB 主从复制实例讲解

数据库  /  管理员 发布于 5年前   235

主从复制可以用来做数据库的备份,故障恢复,读写分离。

本实验使用Mongodb 3.2版本,我们先查看一下mongod的帮助

[root@localhost mongodb]# mongod --help.....省略Master/slave options (old; use replica sets instead): --master   master mode --slave    slave mode --source arg when slave: specify master as         <server:port> --only arg  when slave: specify a single database         to replicate --slavedelay arg           specify delay (in seconds) to be used         when applying master ops to slave --autoresync automatically resync if slave data is         stale.....省略

主从复制已经是过期的功能,目前使用副本集代替。主从复制和副本集的区别,可以简单的理解成主从复制不能自动故障转移,副本集中的集群在主节点宕机后,可以使用选举的策略选择一个新的主节点出来。实现自动的故障转移。

从节点可以是一个,也可以是多个。

下面我们在一台机器上,使用两个实例的方式实现主从复制。

建立数据库目录

[root@localhost data]# mkdir -p /application/mongodb/data/{master,slave}

2. 启动master实例

[root@localhost data]# mongod --dbpath=/application/mongodb/data/master/ --port 27017 --master

--master 指定该实例是主服务器 。

3. 启动从实例

[root@localhost ~]# mongod --dbpath=/application/mongodb/data/slave/ --port 27018 --slave --source 127.0.0.1:27017

--slave 指定该实例为从服务器
--source 指定主服务器是谁?

从服务器启动后,即不断的想主服务器请求同步数据

2016-01-16T10:30:10.208+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:11.210+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:12.211+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:14.196+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:15.197+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:16.199+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:17.202+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:18.204+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:19.207+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:20.209+0800 I REPL   [replslave] syncing from host:127.0.0.1:27017

至此,主从复制已经配置完成,就是这么的简单。

对于从服务器,还有三个参数需要解释一下。

  --only arg

 从节点指定只复制某个特定的数据库(默认复制所有数据库)

  --slavedelay arg

指定从服务器延迟多久时间再同步,此选项在主服务器发生人为操作失误时,比较有用。发现错误时,从服务器还没有同步错误。这样可以避免错误的发生。

  --autoresync

如果从节点的数据与主节点发生断裂(某些oplog中的数据还未被同步,即被覆盖了),那么该选项将是从节点自动的从新从头开始同步数据库。

下面我们验证一下,数据的同步是否有效。
在主库中插入数据。

[root@localhost ~]# mongo 127.0.0.1:27017MongoDB shell version: 3.2.1connecting to: 127.0.0.1:27017/test> db.user.insert({"name":"jack","age":40,"job":"moive star"})WriteResult({ "nInserted" : 1 })> db.user.insert({"name":"vicent","age":25,"job":"teacher"})WriteResult({ "nInserted" : 1 })

登录从数据库,检查数据是否同步

[root@localhost ~]# mongo 127.0.0.1:27018MongoDB shell version: 3.2.1connecting to: 127.0.0.1:27018/test> > db.user.find(){ "_id" : ObjectId("5699af720102a61caffb76e8"), "name" : "jack", "age" : 40, "job" : "moive star" }{ "_id" : ObjectId("5699af920102a61caffb76e9"), "name" : "vicent", "age" : 25, "job" : "teacher" }

可以看到数据已经同步啦~

默认情况下,要想在从库开启查询功能,必须告知服务器,你接受从服务器的数据(有可能同步有延迟,数据不一致,你能够接受这种不一致)

> show collections2016-01-16T10:52:04.363+0800 E QUERY  [thread1] Error: listCollections failed: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :_getErrorWithCode@src/mongo/shell/utils.js:23:13DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:746:1DB.prototype.getCollectionInfos@src/mongo/shell/db.js:758:15DB.prototype.getCollectionNames@src/mongo/shell/db.js:769:12shellHelper.show@src/mongo/shell/utils.js:695:9shellHelper@src/mongo/shell/utils.js:594:15@(shellhelp2):1:1

执行rs.slaveOK

> rs.slaveOk()> show collectionsuser>

在从服务的local数据库中有个sources集合,记录了主服务的信息

> use localswitched to db local> show collectionsmesourcesstartup_log> db.sources.find().pretty(){  "_id" : ObjectId("5699aaafa33311c25ab793df"),  "host" : "127.0.0.1:27017",  "source" : "main",  "syncedTo" : Timestamp(1452913003, 1)}

我们再次启动从库时,就无需指定source参数啦。

[root@localhost ~]# mongod --dbpath=/application/mongodb/data/slave/ --port 27018 --slave2016-01-16T10:57:45.965+0800 I CONTROL [initandlisten] MongoDB starting : pid=21820 port=27018 dbpath=/application/mongodb/data/slave/ slave=1 64-bit host=localhost.localdomain2016-01-16T10:57:45.967+0800 I CONTROL [initandlisten] db version v3.2.12016-01-16T10:57:45.968+0800 I CONTROL [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b22016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] allocator: tcmalloc2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] modules: none2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] build environment:2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   distmod: rhel622016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   distarch: x86_642016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   target_arch: x86_642016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] options: { net: { port: 27018 }, slave: true, storage: { dbPath: "/application/mongodb/data/slave/" } }2016-01-16T10:57:46.010+0800 I -    [initandlisten] Detected data files in /application/mongodb/data/slave/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-16T10:57:46.011+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),2016-01-16T10:57:48.485+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2016-01-16T10:57:48.486+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.488+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.493+0800 I FTDC   [initandlisten] Initializing full-time diagnostic data capture with directory '/application/mongodb/data/slave/diagnostic.data'2016-01-16T10:57:48.494+0800 I NETWORK [initandlisten] waiting for connections on port 270182016-01-16T10:57:48.495+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker2016-01-16T10:57:49.497+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:50.503+0800 I REPL   [replslave] sleep 1 sec before next pass2016-01-16T10:57:51.504+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:52.505+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:54.295+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:55.296+0800 I REPL   [replslave] syncing from host:127.0.0.1:27017

主从库之间利用oplog日志进行同步。oplog存在于主库的local数据库,oplog.$main集合。

> use localswitched to db local> db.oplog.$main.find({"op":"i"}).sort({"ts":-1}).pretty(){  "ts" : Timestamp(1452916694, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699bfd6647c735cb3a50e0c"),    "name" : "zhangcong"  }}{  "ts" : Timestamp(1452913156, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699b204358c4672cad1cc6e"),    "name" : "zhangdd",    "age" : 30,    "job" : "teacher"  }}{  "ts" : Timestamp(1452912530, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699af920102a61caffb76e9"),    "name" : "vicent",    "age" : 25,    "job" : "teacher"  }}{  "ts" : Timestamp(1452912498, 2),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699af720102a61caffb76e8"),    "name" : "jack",    "age" : 40,    "job" : "moive star"  }}

该集合属于固定集合。在一定时间后,旧日志会被覆盖。如果日志已经被覆盖,从库还没有来的及同步。那么从库就无法再同步数据了。只有使用--autoresync让其重新同步数据。

备注:命令行参数指定的参数值,可以写到config文件中,启动时使用

mongod --config /path/to/file.conf

mongod 2.4以后的版本使用YAML的格式来编写配置文件。关于主从复制的配置如何在配置文件中声明,官方文件没有给出方法。试了几种写法都不正确。 因为mongodb使用副本集代替了主从复制,从而可能配置文件不再支持主从复制。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


  • 上一条:
    浅析Mongodb性能优化的相关问题
    下一条:
    mac下使用brew 安装mongodb的方法教程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客