一、什么是redo日志?redo日志有什么用?
redo日志即重做日志,重做日志是在DM数据库中添加、删除、修改对象,或者改变数据,DM都会按照特定的格式,将这些操作执行的结果写入到当前的重做日志文件中。重做日志文件以log为扩展名。每个DM数据库实例必须至少有2个重做日志文件,默认两个日志文件为DAMENG01.log、DAMENG02.log,这两个文件循环使用。 重做日志文件因为是数据库正在使用的日志文件,因此被称为联机日志文件。 重做日志文件主要用于数据库的备份与恢复。理想情况下,数据库系统不会用到重做日志文件中的信息。然而现实世界总是充满了各种意外,比如电源故障、系统故障、介质故障,或者数据库实例进程被强制终止等,数据库缓冲区中的数据页会来不及写入数据文件。这样,在重启DM实例时,通过重做日志文件中的信息,就可以将数据库的状态恢复到发生意外时的状态。
重做日志文件对于数据库是至关重要的。它们用于存储数据库的事务日志,以便系统在出现系统故障和介质故障时能够进行故障恢复。在 DM数据库运行过程中,任何修改数据库的操作都会产生重做日志,例如,当一条元组插入到一个表中的时候,插入的结果写入了重做日志,当删除一条元组时,删除该元组的事实也被写了进去,这样,当系统出现故障时,通过分析日志可以知道在故障发生前系统做了哪些动作,并可以重做这些动作使系统恢复到故障之前的状态。
二、redo日志损坏了怎么办?
既然redo日志对于数据库来说如此重要,那么当redo日志损坏了,该如何修复呢,下面就让我们来通过实验回答一下这个问题。
故障模拟:
数据库为单机,通过人工向redo日志追加内容,来模拟数据库的redo日志文件损坏。
普通情况下如果数据库重启后会报错
针对redo损坏有如下解决办法:
方法一:
(1)修改dm.ini参数PSEG_RECV为0,跳过回滚活动事务和 PURGE 已经提交事务的步骤。
关于PSEG_RECV参数:
该参数是系统级的动态参数,默认为1。系统故障重启时,对活动事务和已提交事务的处理方式。0:跳过回滚活动事务和 PURGE 已经提交事务的步骤。在回滚表空间出现异常、损坏、系统无法正常启动时,可将 PSEG_RECV设置为 0,让系统启动;但存在一定风险,未提交事务的修改将无法回滚,破坏事务的原子性;另外,已提交未 PURGE 的事务,将导致部分存储空间无法回收;1:回滚活动事务并PURGE 已经提交事务;2:延迟 PURGE 已提交事务,延迟回滚活动事务;3:回滚活动事务,延迟 PURGE 已提交事务。
该参数可以在线修改。
[dmdba@CentOS7 bin]$ ./disql SYSDBA/SYSDBA:5236
Server[LOCALHOST:5236]:mode is normal, state is open
login used time : 2.928(ms)
disql V8
SQL> SP_SET_PARA_VALUE(1,'PSEG_RECV',0);
DMSQL executed successfully
used time: 4.968(ms). Execute id is 800.
SQL>
(2)根据数据库文件夹下dminit+日期时间.log(例如:dminit20201224153001.log)的文件查看数据库的各个参数,重新初始化数据库;
[dmdba@CentOS7 DAMENG]$ cat dminit20201224153001.log
start init database: V8, 2020-12-24 15:30:01
init params:
db path: /opt/dmdbms/data/DAMENG
db name: DAMENG
auto overwrite: 0
page size: 32768
extent size: 32
time zone: +08:00
string case sensitive: 1
charset: 1
length in char: 0
page check mode: 0
priv flag: 0
rlog enc flag: 0
use new hash: 1
blank pad mode: 0
sec priv mode: 0
huge with delta: 1
rlog gen for huge: 0
pseg_mgr_flag: 0
log file path: /opt/dmdbms/data/DAMENG/DAMENG01.log
log file path: /opt/dmdbms/data/DAMENG/DAMENG02.log
create ini file /opt/dmdbms/data/DAMENG/dm.ini success.
create rlog file /opt/dmdbms/data/DAMENG/DAMENG01.log success.
create rlog file /opt/dmdbms/data/DAMENG/DAMENG02.log success.
SYSTEM file : /opt/dmdbms/data/DAMENG/SYSTEM.DBF
MAIN file : /opt/dmdbms/data/DAMENG/MAIN.DBF
ROLL file : /opt/dmdbms/data/DAMENG/ROLL.DBF
create dm database success. 2020-12-24 15:30:04
从上图可以看到,初始化参数路径为:/opt/dmdbms/data/DAMENG,数据库名为:DAMENG(默认),page size:32 ,extent size:32,charset:1。其他均为默认参数
使用该参数重新初始化数据库:
[dmdba@CentOS7 bin]$ ./dminit path=/opt/dmdbms/data CHARSET=1 EXTENT_SIZE=32 PAGE_SIZE=32
initdb V8
db version: 0x7000b
log file path: /opt/dmdbms/data/DAMENG/DAMENG01.log
log file path: /opt/dmdbms/data/DAMENG/DAMENG02.log
write to dir [/opt/dmdbms/data/DAMENG].
create dm database success. 2020-12-24 16:25:05
(3)将新初始化的数据库的redo日志拷贝到要修复数据库文件夹下(拷贝前对要修复数据库的原redo日志做好备份)
注意:由于数据库名不能相同,需要在其他机器上初始化,或者将有问题的库挪走到其他目录或者改名,初始化完新库后,拿到了redo日志文件再把有问题的库改回来。此外,本次使用的redo日志为默认大小256M,如果是其他大小,需要将新初始化的实例redo改为对应的大小,然后再使用该redo日志。同时,新是初始化的实例要先启动一次,再将redo日志拷贝要修复数据库文件夹下。
(4)替换后,利用dmmdf 修改redo日志的db_magic的值,否则启动会报如下错:
[dmdba@CentOS7 bin]$ ./DmServiceDMSERVER start
Starting DmServiceDMSERVER: ./DmServiceDMSERVER: 行 406: 6090 浮点数例外 (吐核)eval exec "\"$EXEC_PROG_FULL_PATH\"" "$STARTUP_PARAMS" -noconsole $TMP_START_MODE > "$SERVICE_LOG_FILE" 2>&1
[ FAILED ]
version info: enterprise
Use normal os_malloc instead of HugeTLB
Use normal os_malloc instead of HugeTLB
DM Database Server x64 V8 1-1-144-20.11.06-129436-ENT startup...
Database mode = 0, oguid = 0
file lsn: 0
ndct db load finished
ndct fill fast pool finished
iid page's trxid[3006]
NEXT TRX ID = 3007
pseg recv finished
nsvr_startup end.
aud sys init success.
aud rt sys init success.
dmmdf的使用方法如下:
[dmdba@CentOS7 bin]$ ./dmmdf help
Format: ./dmmdf KEYWORD=value
Example: ./dmmdf TYPE=1 FILE=/opt/data/DAMENG/SYSTEM.DBF
Keyword Explanation
--------------------------------------------------------------------------------
TYPE type
1: for dbf
2: for rlog
3: for original bak
4: for bakset meta
5: for bakset bkp
6: for bakset
7: for database degrades
scope=1: Only supports from 0x0007000A to 0x00070009, file is dm.ctl path
scope=2: Only supports from 0x0007000B to 0x0007000A, file is dm.ini path
8: for rlog degrades (Only supports from 0x7006 to 0x7005)
9: for specified database upgrade(Only supports from V8.1.1.88(V7.1.7.88) to V8.1.1.100(V7.1.7.100)
FILE file path
DCR_INI dmdcr.ini path
DFS_INI dmdfs.ini path
SCOPE scope for database degrades when type=7
HELP show this help info
--------------------------------------------------
Example: ./dmmdf TYPE=1 FILE=/opt/data/DAMENG/SYSTEM.dbf
./dmmdf TYPE=2 FILE=/opt/data/DAMENG/DAMENG01.log
./dmmdf TYPE=3 FILE=/opt/data/DAMENG/bak/dmdb.bak
./dmmdf TYPE=4 FILE=/opt/data/DAMENG/bak/bset/nbak.meta
./dmmdf TYPE=5 FILE=/opt/data/DAMENG/bak/bset/nbak.bak
./dmmdf TYPE=6 FILE=/opt/data/DAMENG/bak/bset
./dmmdf TYPE=7 SCOPE=1 FILE=/opt/data/DAMENG/dm.ctl
./dmmdf TYPE=7 SCOPE=2 FILE=/opt/data/DAMENG/dm.ini
./dmmdf TYPE=8 FILE=/opt/data/DAMENG/dm.ini
./dmmdf TYPE=9 FILE=/opt/data/DAMENG/dm.ini
通过dmmdf查看原库的db_magic值,并记录:
[dmdba@CentOS7 bin]$ ./dmmdf TYPE=1 FILE=/opt/dmdbms/data/DAMENG/SYSTEM.DBF
dmmdf V8
**********************************************************
1 db_magic=16379365
2 next_trxid=2005
3 pemnt_magic=1135665216
**********************************************************
Please input which parameter you want to change(1-3), q to quit: q
通过dmmdf修改DAMENG01.log的db_magic为16379365:
[dmdba@CentOS7 bin]$ ./dmmdf TYPE=2 FILE=/opt/dmdbms/data/DAMENG/DAMENG01.log
dmmdf V8
**********************************************************
1 sig = DMRLOG
2 ver = 7006
3 chksum = -266079413
4 sta = 0
5 n_magic = 7
6 db_magic = 2096250135
7 len = 268435456
8 free = 4096
9 clsn = 0
10 clsn_fil = 0
11 clsn_off = 4096
12 pemnt_magic = 216910885
13 fil_id = 0
15 next_seq = 2457
16 g_next_seq = 2457
17 arch_lsn = 0
18 arch_seq = 0
19 dbversion = 0x7000b
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
You can only reset sta(4) or db_magic (6) or clsn (9) or clsn_fil(10) or clsn_off(11) or pemnt_magic(12) or fil_id(13) or next_seq(15) or g_next_seq(16) or p_db_magic(20) or n_apply_ep(21).
Please input the num which one you want to change, q to quit: 6
Input the new value: 16379365
**********************************************************
1 sig = DMRLOG
2 ver = 7006
3 chksum = -1943526471
4 sta = 0
5 n_magic = 7
6 db_magic = 16379365
7 len = 268435456
8 free = 4096
9 clsn = 0
10 clsn_fil = 0
11 clsn_off = 4096
12 pemnt_magic = 216910885
13 fil_id = 0
15 next_seq = 2457
16 g_next_seq = 2457
17 arch_lsn = 0
18 arch_seq = 0
19 dbversion = 0x7000b
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
Do you want to quit and save the change to file (y/n): y
Save to file success!
[dmdba@CentOS7 bin]$
(5)正常重启数据库。
(6)此情况重启的数据库可能无法进行正常的写操作,所以必须重新建新库,尽快将数据迁移到新库中再使用。
方法二:
利用dbmdf将redo日志的状态由active改为inactive,启动时数据库将根据0号日志文件的状态来决定是否重做redo日志。
[dmdba@CentOS7 bin]$ ./dmmdf TYPE=2 FILE=/opt/dmdbms/data/DAMENG/DAMENG01.log
dmmdf V8
**********************************************************
1 sig = DMRLOG
2 ver = 7006
3 chksum = 1878986077
4 sta = 0
5 n_magic = 7
6 db_magic = 16379365
7 len = 268435456
8 free = 11676672
9 clsn = 43190
10 clsn_fil = 0
11 clsn_off = 11676160
12 pemnt_magic = 1487976171
13 fil_id = 0
15 next_seq = 4048
16 g_next_seq = 4048
17 arch_lsn = 0
18 arch_seq = 0
19 dbversion = 0x7000b
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
You can only reset sta(4) or db_magic (6) or clsn (9) or clsn_fil(10) or clsn_off(11) or pemnt_magic(12) or fil_id(13) or next_seq(15) or g_next_seq(16) or p_db_magic(20) or n_apply_ep(21).
Please input the num which one you want to change, q to quit: 4
Input the new value: 1
**********************************************************
1 sig = DMRLOG
2 ver = 7006
3 chksum = 1878986076
4 sta = 1
5 n_magic = 7
6 db_magic = 16379365
7 len = 268435456
8 free = 11676672
9 clsn = 43190
10 clsn_fil = 0
11 clsn_off = 11676160
12 pemnt_magic = 1487976171
13 fil_id = 0
15 next_seq = 4048
16 g_next_seq = 4048
17 arch_lsn = 0
18 arch_seq = 0
19 dbversion = 0x7000b
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
Do you want to quit and save the change to file (y/n): y
Save to file success!
[dmdba@CentOS7 bin]$
正常重启数据库,但此情况下可能无法正常的写操作,必须重新建新库迁移数据再使用。
注:在实际生产库中如果碰到,以上两种方法在万不得已的时候,不要使用,尽可能的想办法修复redo日志。如果使用这两种办法启动了数据库,都需重新建库迁移数据,再去使用新库作为生产库。
欢迎关注我的博客《Jackin's Blog - 知识改变命运》学习分享更多知识