Mongodb 通过一致性备份搭建SECONDARY.

该方法应用面比较窄,

适用于 : 一主 一备 一投票节点,数据库较大,oplog 比较小,备库需要修复而且主库不能停机的情况.

该方法仅限于学习测试,线上环境慎用.

集群结构:
opsdba-vbj01-1:27018 ARBITE
opsdba-vbj01-1:27019 PRIMARY
opsdba-vbj01-1:27016 SECONDARY

模拟opsdba-vbj01-1:27016 crash ,使用一致性备份搭建备库.

1.在主库,自建role,赋予restore oplog权限.
use admin
db.runCommand({ createRole: "restoreoplog",
privileges:
        [
        { resource: { anyResource: true }, actions: [ "anyAction" ] }
        ],
roles:
       []
});
db.grantRolesToUser( "root", ["restoreoplog"] );

2.主库一致性备份.
[root@opsdba-vbj01-1 dump]# mongodump -uroot -proot123 --port=27019 --oplog --authenticationDatabase=admin -o all_backup
2016-10-11T15:45:58.019+0800    writing admin.system.users to
2016-10-11T15:45:58.019+0800    done dumping admin.system.users (1 document)
2016-10-11T15:45:58.019+0800    writing admin.system.roles to
2016-10-11T15:45:58.020+0800    done dumping admin.system.roles (1 document)
2016-10-11T15:45:58.020+0800    writing admin.system.version to
2016-10-11T15:45:58.020+0800    done dumping admin.system.version (1 document)
2016-10-11T15:45:58.021+0800    writing test.testData to
2016-10-11T15:45:58.021+0800    writing test.tab to
2016-10-11T15:45:58.190+0800    done dumping test.tab (34056 documents)
2016-10-11T15:46:01.022+0800    [###########.............]  test.testData  451185/909000  (49.6%)
2016-10-11T15:46:04.022+0800    [####################....]  test.testData  774771/909000  (85.2%)
2016-10-11T15:46:05.024+0800    [########################]  test.testData  913877/909000  (100.5%)
2016-10-11T15:46:05.024+0800    done dumping test.testData (913877 documents)
2016-10-11T15:46:05.025+0800    writing captured oplog to
2016-10-11T15:46:05.781+0800            dumped 6470 oplog entries

3.把opsdba-vbj01-1:27016 从SECONDARY 转为单节点空库.

/data/mongodb/mongodb/bin/mongod -f /data/mongodb/mongodb1/conf/mongod.cnf  --shutdown
清空data目录,单节点启动:
修改port = 27015,
注释掉replSet
/data/mongodb/mongodb/bin/mongod -f /data/mongodb/mongodb1/conf/mongod.cnf
 
创建管理员用户:
[root@opsdba-vbj01-1 mongodb1]# mongo --port=27015 admin
use admin
db.createUser(
  {
    user: "root",
    pwd: "root123",
    roles:
    [
      {
        role: "root",
        db: "admin"
      }
    ]
  }
);
自建role,赋予restore oplog权限:
[root@opsdba-vbj01-1 mongodb1]# mongo -uroot -proot123 --port=27015 admin
use admin
db.runCommand({ createRole: "restoreoplog",
privileges:
        [
        { resource: { anyResource: true }, actions: [ "anyAction" ] }
        ],
roles:
       []
});
db.grantRolesToUser( "root", ["restoreoplog"] );

4.导入
[root@opsdba-vbj01-1 dump]# mongorestore -uroot -proot123 --port=27015 --authenticationDatabase=admin --oplogReplay --dir=all_backup
2016-10-11T15:59:48.505+0800    building a list of dbs and collections to restore from all_backup dir
2016-10-11T15:59:48.507+0800    reading metadata for test.testData from all_backup/test/testData.metadata.json
2016-10-11T15:59:48.508+0800    reading metadata for test.tab from all_backup/test/tab.metadata.json
2016-10-11T15:59:48.556+0800    restoring test.tab from all_backup/test/tab.bson
2016-10-11T15:59:48.604+0800    restoring test.testData from all_backup/test/testData.bson
2016-10-11T15:59:49.147+0800    restoring indexes for collection test.tab from metadata
2016-10-11T15:59:49.147+0800    finished restoring test.tab (34056 documents)
2016-10-11T15:59:51.507+0800    [####....................]  test.testData  18.5 MB/94.1 MB  (19.7%)
2016-10-11T15:59:54.507+0800    [##########..............]  test.testData  39.8 MB/94.1 MB  (42.3%)
2016-10-11T15:59:57.507+0800    [###############.........]  test.testData  59.7 MB/94.1 MB  (63.5%)
2016-10-11T16:00:00.507+0800    [####################....]  test.testData  80.1 MB/94.1 MB  (85.1%)
2016-10-11T16:00:03.288+0800    [########################]  test.testData  94.1 MB/94.1 MB  (100.0%)
2016-10-11T16:00:03.288+0800    restoring indexes for collection test.testData from metadata
2016-10-11T16:00:03.289+0800    finished restoring test.testData (913877 documents)
2016-10-11T16:00:03.289+0800    restoring users from all_backup/admin/system.users.bson
2016-10-11T16:00:03.416+0800    restoring roles from all_backup/admin/system.roles.bson
2016-10-11T16:00:03.466+0800    replaying oplog
2016-10-11T16:00:03.808+0800    done

5.获取最后一个oplog的时间戳
[root@opsdba-vbj01-1 dump]# cd all_backup/
[root@opsdba-vbj01-1 all_backup]# bsondump oplog.bson >oplog.txt
[root@opsdba-vbj01-1 all_backup]# tail -1 oplog.txt
{"ts":{"$timestamp":{"t":1476171965,"i":805}},"t":{"$numberLong":"2"},"h":{"$numberLong":"6906152948185446623"},"v":2,"op":"i","ns":"test.testData","o":{"_id":{"$oid":"57fc98bddfa99af76706f721"},"x":6470.0,"name":"MACLEAN","name1":"MACLEAN","name2":"MACLEAN","name3":"MACLEAN"}}

6.初始化local库的相关表
[root@opsdba-vbj01-1 mongodb1]# mongo -uroot -proot123 --port=27015 admin
use local
db.runCommand( { create: "oplog.rs", capped: true, size: (1* 1024 * 1024 * 1024) } );

#数据来自oplog.txt
db.oplog.rs.save({"ts" : Timestamp(1476171965, 805),"h" : NumberLong("6906152948185446623")});
db.db.replset.minvalid.save({"ts" : Timestamp(1476171965,805), "t" : NumberLong(2)});

#数据来自主库的数据查询
db.replset.election.save({ "_id" : ObjectId("57fc5ea0cfa6486e03e975d0"), "term" : NumberLong(2), "candidateIndex" : NumberLong(2) });
db.system.replset.save({ "_id" : "myrelset", "version" : 5, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 1, "host" : "opsdba-vbj01-1:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "opsdba-vbj01-1:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 3, "host" : "opsdba-vbj01-1:27016", "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("57bfdcdcd40cbe4bf173396a") } });

7.重启
use admin
db.shutdownServer();

修改为原始值.
port=27016
replSet 取消注释
启动
/data/mongodb/mongodb/bin/mongod -f /data/mongodb/mongodb1/conf/mongod.cnf

8.检测:
比对行数.
db.collection.count()

9.启动日志
2016-10-11T16:42:51.520+0800 I CONTROL  [initandlisten] MongoDB starting : pid=633 port=27016 dbpath=/data/mongodb/mongodb1/data 64-bit host=opsdba-vbj01-1
2016-10-11T16:42:51.520+0800 I CONTROL  [initandlisten] db version v3.2.8
2016-10-11T16:42:51.520+0800 I CONTROL  [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-10-11T16:42:51.520+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten] modules: none
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten] build environment:
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten]     distmod: rhel62
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-10-11T16:42:51.521+0800 I CONTROL  [initandlisten] options: { config: "/data/mongodb/mongodb1/conf/mongod.cnf", net: { http: { enabled: false }, maxIncomingConnections: 3000, port: 27016, unixDomainSocket: { pathPrefix: "/data/mongodb/mongodb1/data" } }, operationProfiling: { mode: "slowOp", slowOpThresholdMs: 800 }, processManagement: { fork: true, pidFilePath: "/data/mongodb/mongodb1/data/mongod.pid" }, replication: { replSet: "myrelset" }, security: { clusterAuthMode: "keyFile", keyFile: "/data/mongodb/mongodb1/conf/myrelset.keyfile" }, storage: { dbPath: "/data/mongodb/mongodb1/data", directoryPerDB: true, engine: "wiredTiger", journal: { commitIntervalMs: 300, enabled: true }, mmapv1: { nsSize: 32 }, repairPath: "/data/mongodb/mongodb1/data", syncPeriodSecs: 60.0, wiredTiger: { engineConfig: { cacheSizeGB: 1 } } }, systemLog: { destination: "file", path: "/data/mongodb/mongodb1/log/mongod.log", quiet: true, timeStampFormat: "iso8601-local" } }
2016-10-11T16:42:51.521+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-10-11T16:42:51.944+0800 I STORAGE  [initandlisten] Starting WiredTigerRecordStoreThread local.oplog.rs
2016-10-11T16:42:51.944+0800 I STORAGE  [initandlisten] The size storer reports that the oplog contains 1 records totaling to 45 bytes
2016-10-11T16:42:51.944+0800 I STORAGE  [initandlisten] Scanning the oplog to determine where to place markers for truncation
2016-10-11T16:42:51.982+0800 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2016-10-11T16:42:51.982+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-10-11T16:42:51.982+0800 I CONTROL  [initandlisten]
2016-10-11T16:42:51.982+0800 I CONTROL  [initandlisten]
2016-10-11T16:42:51.982+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2016-10-11T16:42:52.030+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/mongodb/mongodb1/data/diagnostic.data'
2016-10-11T16:42:52.030+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-10-11T16:42:52.031+0800 I NETWORK  [initandlisten] waiting for connections on port 27016
2016-10-11T16:42:52.066+0800 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "myrelset", version: 5, protocolVersion: 1, members: [ { _id: 1, host: "opsdba-vbj01-1:27018", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 2, host: "opsdba-vbj01-1:27019", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 3, host: "opsdba-vbj01-1:27016", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 }, replicaSetId: ObjectId('57bfdcdcd40cbe4bf173396a') } }
2016-10-11T16:42:52.067+0800 I REPL     [ReplicationExecutor] This node is opsdba-vbj01-1:27016 in the config
2016-10-11T16:42:52.067+0800 I REPL     [ReplicationExecutor] transition to STARTUP2
2016-10-11T16:42:52.067+0800 I REPL     [ReplicationExecutor] Starting replication applier threads
2016-10-11T16:42:52.067+0800 I REPL     [ReplicationExecutor] transition to RECOVERING
2016-10-11T16:42:52.072+0800 I REPL     [ReplicationExecutor] transition to SECONDARY
2016-10-11T16:42:52.102+0800 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to opsdba-vbj01-1:27018
2016-10-11T16:42:52.102+0800 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to opsdba-vbj01-1:27019
2016-10-11T16:42:52.102+0800 I REPL     [ReplicationExecutor] Member opsdba-vbj01-1:27018 is now in state SECONDARY
2016-10-11T16:42:52.103+0800 I REPL     [ReplicationExecutor] Member opsdba-vbj01-1:27019 is now in state PRIMARY
2016-10-11T16:42:58.069+0800 I REPL     [ReplicationExecutor] syncing from: opsdba-vbj01-1:27018
2016-10-11T16:42:58.086+0800 I REPL     [SyncSourceFeedback] setting syncSourceFeedback to opsdba-vbj01-1:27018
2016-10-11T16:42:58.104+0800 I ASIO     [NetworkInterfaceASIO-BGSync-0] Successfully connected to opsdba-vbj01-1:27018

时间: 2024-10-30 12:41:23

Mongodb 通过一致性备份搭建SECONDARY.的相关文章

MONGODB使用MONGDODUMP备份来搭建备份集

准备加入一个备分节点,如下操作: 使用MONGODUMP进行一致性备份 ./mongodump -o /ora11g/monbak --oplog -u moadm1 -p gelc123 同样的首先进行恢复  ./mongorestore --oplogReplay --port 27030 /ora11g/monbak/  加入OPLOG集合 use local db.createCollection("oplog.rs",{"capped":true,&quo

从炉石传说数据库故障谈谈MongoDB的数据库备份和恢复手段

看到这个消息,我的第一反应是重新翻出尘封已久的ipad,装上炉石准备上线领补偿.等等,作为一个数据库行业从业人员,是不是还应该干点什么?恩,很有必要再重新审视一下我们的数据库有没有做好容灾,否则,今天你看别人热闹,明天可能就别人看你热闹了.借此机会我想给大家普及一下MongoDB数据库的备份和恢复手段(当然炉石传说应该不一定是使用MongoDB作为数据库),以帮助大家做好容灾,过个好年.同时,我也为我们阿里云MongoDB服务做下广告,我们的MongoDB服务拥有完善的自动备份/恢复功能,灵活的

pg_dump一致性备份以及cache lookup failed错误的原因分析

背景 PostgreSQL逻辑备份,如何保证备份数据的一致性呢,例如备份的同时,数据被纂改或者有新增的数据,如何保证在全库视角,备份出来的数据是在备份开始时看到的一致数据. 一致性逻辑备份分析 可以追溯到1999年的代码,早期PostgreSQL通过serializable隔离级别来保证备份的一致性.https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=bcb5aac81dec14d892fae18b446315367563

探索ORACLE之RMAN_03一致性备份

探索ORACLE之RMAN_03一致性备份 作者:吴伟龙         在之前的已经提到过备份分为一致性备份和非一致性备份,它们的区别就在于数据库所处的备份状态不同,并且一致性备份不能做增量备份恢复,需停业务,一致性备份可以做到增量的备份恢复,从而不影响业务的正常流转.   创建一致性备份  创建一致性备份可以是在非归档模式下创建,并且数据库必须处于mount状态下,而且恢复的时候值能恢复到最后一次备份的状态.也就说从备份到发生故障的这段时间都将丢失.操作步骤如下:   1.1检查归档状态:

探索ORACLE之RMAN_04非一致性备份

探索ORACLE之RMAN_04非一致性备份 作者:吴伟龙   在上一篇博文中提到了数据库的一致性备份,操作非常的简单.只需要一条很短的命令即可完成,但是如果要创建一正式库的备份,一般不建议用一致性备份,也不建议用很简单的名来完成.而是更多的采用脚本实现非一致性备份,这样将可通过backup+archive log+redo有效的将数据恢复到最近一次改变的状态,可以达到数据的丢失最小化. 创建非一致性备份  创建非一致性备份数据库必须处于归档(archivelog)模式,因为非一致性备份的数据库

用户管理的备份(一致性备份、非一致性备份、脱机备份、联机备份)_数据库其它

1.备份数据库概念:指备份数据库的所有数据文件和控制文件,另外还应该备份参数文件和口令文件注意:当备份数据库时,不要备份重做日志.1.1一致性备份概念:数据库一致性备份是指关闭了数据库后备份所有数据文件和控制文件的方法.当使用SHUTDOWN 命令正常关闭了数据库之后,所有数据库文件的当前SCN 值完全一致,所以关闭后的数据库备份被称为数据库一致性备份或者冷备份.适用:ARCHIVELOG.NOARCHIVELOGselect name from v$datafile union select

数据库一致性备份

本文主要讲解数据库一致性备份的执行步骤,备份数据库是指备份数据库的所有数据文件和控制文件,另外还有参数文件和口令文件.注意:备份数据库时 不需备份重做日志文件. 数据库的一致性备份是指:数据库一致性备份是指关闭了数据库后备份所有数据文件和控制文件的方法.当使用SHUTDOWN 命令正常关闭了数据库之后,所有数据库文件的当前SCN 值完全一致,所以关闭后的数据库备份被称为数据库一致性备份或者冷备份.适用:ARCHIVELOG.NOARCHIVELOG 一 列出要备份的数据文件和控制文件. SQL>

数据库非一致性备份

概念:指在OPEN 状态下备份数据库所有数据文件和控制文件的方法.因为在OPEN 状态下数据库内容随时都会改变,从而导致不同数据库文件的当前SCN值完全不同,所以打开时的数据库备份被称为数据库非一致性备份.注意:非一致性备份只适用:ARCHIVELOG模式,而不适用与NOARCHIVELOG模式. 具体步骤: 一  列出要备份的数据文件和控制文件. SQL> select name from v$datafile; NAME                                    

PostgreSQL 最佳实践 - 块级增量备份(ZFS篇)多zfs卷场景一致性备份

背景 当我们使用了多个ZFS卷或者文件系统时,如果一个实例的多个部分,如表空间,放在了不同的zfs上,再使用基于ZFS快照的备份时,可能出现多个文件系统不一致的情况. 例如控制文件是新的,但是数据是旧的. 保物理备份的一致性检查 基于文件的物理备份,为了保证备份的一致性,在备份开始时,需要做一个检查点,同时打开FULL PAGE WRTIE,同时还会生成backup_label文件记录备份开始时的WAL文件,检查点位置等信息. backup_label文件内容示例 START WAL LOCAT