搭建简单的DB2 HADR

简单的HADR,只用一台虚拟机,两个实例间搭建。工作量不大,一般5分钟左右能够完成。
步骤:
1.设定归档模式
2.使用备份建立standby数据库
3.设定hadr相关的参数
4.启动并测试


环境:
Server: 127.0.0.1
Primary instance: db2inst4
Primary service/port: 42099
Standby instance: db2inst5
Standby service/port: 41099
DB name: SAMPLE
--注意,切勿使用与DBM SVCENAME 太接近的端口,因为实例会默认使用那端口之后的连续几个端口,所以应尝试更远一些的端口


1.设定归档模式

--在Primary:
--启用归档模式
[db2inst4@localhost instance]$ db2 update db cfg for SAMPLE using LOGRETAIN on
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.

--启用LOGINDEXBUILD,以便日志有关索引的操作
[db2inst4@localhost instance]$ db2 update db cfg for SAMPLE using LOGINDEXBUILD on
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.


2.使用备份建立standby数据库

--备份primary
[db2inst4@localhost arch]$ db2 list db directory

System Database Directory

Number of entries in the directory = 1

Database 1 entry:

Database alias = SAMPLE
Database name = SAMPLE
Local database directory = /home/db2inst4
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =

[db2inst4@localhost arch]$ db2 backup db sample to /arch

Backup successful. The timestamp for this backup image is : 20110430101950
[db2inst4@localhost arch]$ ls -atrl
total 135208
drwxr-x--- 3 db2inst1 db2iadm1 4096 Dec 8 12:12 db2inst1
drwxr-xr-x 27 root root 4096 Apr 22 23:45 ..
drwxrwxrwx 3 root root 4096 Apr 30 10:19 .
-rw------- 1 db2inst4 db2iadm1 138297344 Apr 30 10:20 SAMPLE.0.db2inst4.NODE0000.CATN0000.20110430101950.001

--注意修改备份文件属性,以便standby实例能够访问
[db2inst4@localhost arch]$ chmod 777 SAMPLE.0.db2inst4.NODE0000.CATN0000.20110430101950.001

--在standby:
[db2inst5@localhost ~]$ db2 restore db sample from /arch/ on /home/db2inst5/
DB20000I The RESTORE DATABASE command completed successfully.
[db2inst5@localhost ~]$ db2 list db directory

System Database Directory

Number of entries in the directory = 1

Database 1 entry:

Database alias = SAMPLE
Database name = SAMPLE
Local database directory = /home/db2inst5
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =

--这时候standby的数据库应该是roll-forward pedning的状态,切勿手动roll-forward
[db2inst5@localhost ~]$ db2 connect to SAMPLE
SQL1117N A connection to or activation of database "SAMPLE" cannot be made
because of ROLL-FORWARD PENDING. SQLSTATE=57019


3.设定hadr相关的参数

--在Primary:
db2 update db cfg for sample using HADR_LOCAL_HOST 127.0.0.1
db2 update db cfg for sample using HADR_LOCAL_SVC 42099
db2 update db cfg for sample using HADR_REMOTE_HOST 127.0.0.1
db2 update db cfg for sample using HADR_REMOTE_SVC 41099
db2 update db cfg for sample using HADR_REMOTE_INST db2inst5
db2 update db cfg for sample using HADR_SYNCMODE SYNC
db2 update db cfg for sample using HADR_TIMEOUT 3
db2 update db cfg for sample using HADR_PEER_WINDOW 120
db2 connect to sample
db2 quiesce database immediate force connections
db2 unquiesce database
db2 connect reset

--在Standby:
db2 update db cfg for sample using HADR_LOCAL_HOST 127.0.0.1
db2 update db cfg for sample using HADR_LOCAL_SVC 41099
db2 update db cfg for sample using HADR_REMOTE_HOST 127.0.0.1
db2 update db cfg for sample using HADR_REMOTE_SVC 42099
db2 update db cfg for sample using HADR_REMOTE_INST db2inst4
db2 update db cfg for sample using HADR_SYNCMODE SYNC
db2 update db cfg for sample using HADR_TIMEOUT 3
db2 update db cfg for sample using HADR_PEER_WINDOW 120



4.启动并测试

--先启动standby
--在Standby:
[db2inst5@localhost ~]$ db2 start hadr on db sample as standby
DB20000I The START HADR ON DATABASE command completed successfully.

--这时候应该是remote catchup pending的状态:
[db2inst5@localhost ~]$ db2pd -d sample -hadr

Database Partition 0 -- Database SAMPLE -- Standby -- Up 0 days 00:03:44

HADR Information:
Role State SyncMode HeartBeatsMissed LogGapRunAvg (bytes)
Standby RemoteCatchupPending Sync 0 1

ConnectStatus ConnectTime Timeout
Disconnected Sat Apr 30 14:09:54 2011 (1304143794) 3

PeerWindowEnd PeerWindow
Null (0) 120

LocalHost LocalService
127.0.0.1 41099

RemoteHost RemoteService RemoteInstance
127.0.0.1 42099 db2inst4

PrimaryFile PrimaryPg PrimaryLSN
S0000000.LOG 0 0x0000000002AC24A1

StandByFile StandByPg StandByLSN StandByRcvBufUsed
S0000000.LOG 0 0x0000000002AC24A1 0%

--再启动Primary
--在Primary:
[db2inst4@localhost ~]$ db2 start hadr on database sample as primary
DB20000I The START HADR ON DATABASE command completed successfully.
[db2inst4@localhost ~]$ db2pd -d sample -hadr

Database Partition 0 -- Database SAMPLE -- Active -- Up 0 days 00:00:15

HADR Information:
Role State SyncMode HeartBeatsMissed LogGapRunAvg (bytes)
Primary Peer Sync 0 0

ConnectStatus ConnectTime Timeout
Connected Sat Apr 30 14:14:14 2011 (1304144054) 3

PeerWindowEnd PeerWindow
Sat Apr 30 14:16:26 2011 (1304144186) 120

LocalHost LocalService
127.0.0.1 42099

RemoteHost RemoteService RemoteInstance
127.0.0.1 41099 db2inst5

PrimaryFile PrimaryPg PrimaryLSN
S0000002.LOG 0 0x0000000003288861

StandByFile StandByPg StandByLSN
S0000002.LOG 0 0x0000000003288861

--可以看到一旦Primary也起来了,hadr的状态就会变成peer

--这时候尝试手动归档,看日志是否能够顺利传递到standby
[db2inst4@localhost ~]$ db2 archive log for DB sample
DB20000I The ARCHIVE LOG command completed successfully.

--在Standby观察
[db2inst5@localhost ~]$ db2pd -d sample -hadr

Database Partition 0 -- Database SAMPLE -- Standby -- Up 0 days 00:05:25

HADR Information:
Role State SyncMode HeartBeatsMissed LogGapRunAvg (bytes)
Standby Peer Sync 0 0

ConnectStatus ConnectTime Timeout
Connected Sat Apr 30 14:14:14 2011 (1304144054) 3

PeerWindowEnd PeerWindow
Sat Apr 30 14:17:18 2011 (1304144238) 120

LocalHost LocalService
127.0.0.1 41099

RemoteHost RemoteService RemoteInstance
127.0.0.1 42099 db2inst4

PrimaryFile PrimaryPg PrimaryLSN
S0000003.LOG 0 0x000000000366BA41

StandByFile StandByPg StandByLSN StandByRcvBufUsed
S0000003.LOG 0 0x000000000366BA41 0%

--两边当前日志都是S0000003.LOG,测试成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值