在数据库管理领域,面对突发故障,确保数据安全和业务连续性是至关重要的。本文讲解了在达梦数据库如何利用归档日志机制,在数据库发生故障后,最大程度减少数据损失,并提供一套完整的实操指南。
归档日志是数据库管理系统在运行期间创建的一系列记录,详细记载了对数据库的所有修改操作,包括但不限于插入、更新、删除等。这一机制在达梦数据库中非常核心,特别是在归档模式下。
为何需要归档日志:
如何配置:可通过编辑配置文件、使用控制台工具或SQL方式开启归档模式,并调整相关参数,在第六点中,会介绍如何使用SQL的方式开启和配置归档
出于性能的考虑:
出于数据安全的考虑:
当完全备份与现有归档日志间存在未被覆盖的区间时,需要采取措施,确保归档日志的连续性和完整性,具体的方法如下:
完全备份+归档备份(假设归档与完全备份不存在空窗期)
-- 将数据库置为mount
ALTER DATABASE MOUNT;
-- 配置归档
ALTER DATABASE ADD ARCHIVELOG 'DEST = /dmdata/5236/arch, TYPE = local, FILE_SIZE = 1024, SPACE_LIMIT = 2048';
-- 开启归档
ALTER DATABASE ARCHIVELOG;
-- 将数据库置为open
ALTER DATABASE open;
-- 检查归档文件
select arch_mode from v$database;
-- 初始化测试表空间
create tablespace "TESTTSB" datafile '/dmdata/5236/DAMENG1/TESTTSB.dbf' size 128 autoextend off CACHE = NORMAL;
CREATE USER "TEST" IDENTIFIED BY Dameng123 HASH WITH SHA512 NO SALT PASSWORD_POLICY 2 ENCRYPT BY Dameng123
LIMIT FAILED_LOGIN_ATTEMPS 3, PASSWORD_LOCK_TIME 1, PASSWORD_GRACE_TIME 10 DEFAULT TABLESPACE "TESTTSB";
grant "DBA","RESOURCE","PUBLIC" to "TEST";
grant CREATE SESSION to "TEST";
-- 在disql中进行完全热备
backup database full backupset '/dmdata/5236/bak/dbfull_202405221731' COMPRESSED LEVEL 5 TASK THREAD 4 PARALLEL 8;
-- 建表,循环插入数据,每秒插入一条
create table TEST.test_tab(id int, createtime datetime);
begin
for i in 1..1000000 loop
insert TEST.test_tab values(i, sysdate);
commit;
sleep(1);
end loop;
end
-- 重开一个会话进行查询
select * from TEST.test_tab order by createtime desc;
ps -ef|grep dmserver
kill 2360488
rm -rf /dmdata/5236/DAMENG1/TESTTSB.DBF
-- 使用dmrman工具生成归档备份
./dmrman CTLSTMT="BACKUP ARCHIVE LOG ALL DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221932'"
-- 基于全备还原
./dmrman CTLSTMT="RESTORE DATABASE '/dmdata/5236/DAMENG1/dm.ini' FROM BACKUPSET '/dmdata/5236/bak/dbfull_202405221731'"
-- 基于全备恢复
./dmrman CTLSTMT="RECOVER DATABASE '/dmdata/5236/DAMENG1/dm.ini' FROM BACKUPSET '/dmdata/5236/bak/dbfull_202405221731'"
-- 还原归档日志
-- OVERWRITE参数为:1表示认为归档文件完好,不再还原该归档文件,添加一条日志记录;2表示存在同名归档立即报错返回,终止还原;3表示强制删除归档,重新还原同名归档
./dmrman CTLSTMT="RESTORE ARCHIVE LOG FROM BACKUPSET '/dmdata/5236/bak/arch_bak_202405221932' TO DATABASE '/dmdata/5236/DAMENG1/dm.ini' OVERWRITE 3"
-- 查看归档备份的日志范围(查看LSN范围)
./dmrman CTLSTMT="SHOW BACKUPSET '/dmdata/5236/bak/arch_bak_202405221932' INFO meta"
-- 基于被还原的归档日志恢复到指定的LSN(通过归档备份的日志范围,确定恢复到哪个LSN节点)
./dmrman CTLSTMT="recover database '/dmdata/5236/DAMENG1/dm.ini' with archivedir '/dmdata/5236/arch' until lsn 46160 "
-- 更新数据库魔术
./dmrman CTLSTMT="RECOVER DATABASE '/dmdata/5236/DAMENG1/dm.ini' UPDATE DB_MAGIC"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG ALL DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221932'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG FROM lsn 50414 DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221933'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG UNTIL lsn 50478 DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221934'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG lsn between 50414 and 50478 DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221935'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG FROM TIME '2024-05-22 20:16:29' DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221936'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG UNTIL TIME '2024-05-22 20:17:57' DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221937'"
./dmrman CTLSTMT="BACKUP ARCHIVE LOG TIME between '2024-05-22 20:16:29' and '2024-05-22 20:17:57' DATABASE '/dmdata/5236/DAMENG1/dm.ini' backupset '/dmdata/5236/bak/arch_bak_202405221938'"
文章
阅读量
获赞