1. Create the directory for log archives on both node01:node02:
[root@node01 ~]# mkdir -p /data/db2/db2inst2/logarchives
[root@node02 ~]# mkdir -p /data/db2/db2inst2/logarchives
2. Create the directory for database backup on both nodes:
[root@node01 ~]# mkdir -p /data/db2/db2inst2/backup
[root@node02 ~]# mkdir -p /data/db2/db2inst2/backup
3. Create the directory for mirror logs on both nodes
[root@node01 ~]# mkdir -p /data/db2/db2inst2/mirrorlogs
[root@node02 ~]# mkdir -p /data/db2/db2inst2/mirrorlogs
[root@node01 ~]# chown -R db2inst2:db2iadm2 /data/db2/db2inst2/
[root@node02 ~]# chown -R db2inst2:db2iadm2 /data/db2/db2inst2/
4、Configure log archiving and mirror logging:
[db2inst2@db2-node01 ~]$ db2 create db ghan
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING logarchmeth1 "DISK:/data/db2/db2inst2/logarchives" "
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING LOGPRIMARY 20 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING LOGSECOND 5 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING LOGFILSIZ 2048 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION USING MIRRORLOGPATH /data/db2/db2inst2/mirrorlogs DEFERRED"
[db2inst1@node01 ~]$ db2 "UNQUIESCE DATABASE"
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING LOGBUFSZ 1024 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DB CFG FOR GHAN USING LOGBUFSZ 1024 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING LOGINDEXBUILD ON DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING INDEXREC RESTART DEFERRED"
[db2inst1@node01 ~]$ db2 terminate
[db2inst1@node01 ~]$ db2 backup db ghan to /tmp
5、Configgure Server file node01、ondoe02
修改:vi /etc/services 添加如下
DB2_HADR_GHAN1 55006/tcp
DB2_HADR_GHAN2 55007/tcp
6、Configgure Node01
[db2inst1@node01 ~] db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_LOCAL_HOST 10.0.0.1 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_HOST 10.0.0.2 DEFERRED"
[db2inst1@node01 ~] db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_LOCAL_SVC DB2_HADR_GHAN1 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_SVC DB2_HADR_GHAN2 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_INST db2inst2 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_PEER_WINDOW 120 DEFERRED"
[db2inst1@node01 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_SYNCMODE SYNC DEFERRED"
[db2inst1@node01 ~]$ db2 "DEACTIVATE DATABASE GHAN"
[db2inst1@node01 ~]$ db2 "ACTIVATE DATABASE GHAN"
7、Configgure Node02 Start Standby
[root@db2-node01 ~]# scp /tmp/GHAN.0.db2inst2.NODE0000.CATN0000.20151213103803.001 108.88.3.221:/tmp/
[db2inst1@node02 ~]$ db2 "RESTORE DATABASE GHAN FROM /tmp TAKEN AT 20151213103803 REPLACE HISTORY FILE"
[db2inst1@node02 ~] db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_LOCAL_HOST 10.0.0.2 DEFERRED"
[db2inst1@node02 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_HOST 10.0.0.1 DEFERRED"
[db2inst1@node02 ~] db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_LOCAL_SVC DB2_HADR_GHAN2 DEFERRED"
[db2inst1@node02 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_SVC DB2_HADR_GHAN1 DEFERRED"
[db2inst1@nodedb02 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_REMOTE_INST db2inst2 DEFERRED"
[db2inst1@nodedb02 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_SYNCMODE SYNC DEFERRED"
[db2inst1@nodedb02 ~]$ db2 "UPDATE DATABASE CONFIGURATION FOR GHAN USING HADR_PEER_WINDOW 120 DEFERRED"
[db2inst1@nodedb02 ~]$ db2 "START HADR ON DATABASE GHAN AS STANDBY"
[db2inst1@nodedb02 ~] db2 "ACTIVATE DATABASE GHAN"
[db2inst1@nodedb02 ~] db2 "GET DB CFG FOR GHAN"
[db2inst2@db2-node02 db2dump]$ db2 get db cfg for ghan | grep HADR
8、configgure node01 Start PRIMARY
[db2inst1@node01 ~] db2 "START HADR ON DATABASE GHAN AS PRIMARY"
[db2inst1@nodedb01 ~] db2 "GET DB CFG FOR GHAN |grep HADR"
[db2inst1@nodedb01 ~] db2 "get snapshot for database on GHAN"
9、HADR功能主备互相测试:
9.1 standby 提升为PRIMARY
[db2inst2@db2-node02 ~]$ db2 get db cfg for ghan | grep HADR
HADR 数据库角色
= STANDBY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.2
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN2
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.1
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN1
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node02 ~]$ db2 "takeover hadr on db ghan "
DB20000I TAKEOVER HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node02 ~]$ db2 get db cfg for ghan | grep HADR
HADR 数据库角色
= PRIMARY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.2
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN2
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.1
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN1
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node02 ~]$ db2 connect to ghan
数据库连接信息
数据库服务器 = DB2/LINUXX8664 9.7.5
SQL 授权标识 = DB2INST2
本地数据库别名 = GHAN
[db2inst2@db2-node02 ~]$ db2 "insert into tank values(123)"
DB20000I SQL 命令成功完成。
[db2inst2@db2-node02 ~]$ db2 "insert into tank values(123)"
DB20000I SQL 命令成功完成。
[db2inst2@db2-node02 ~]$ db2 "insert into tank values(123)"
DB20000I SQL 命令成功完成。
[db2inst2@db2-node02 ~]$ db2 "select count(*) from tank"
1
-----------
30
1 条记录已选择。
9.2 旧主节点(现standby)提升也活动节点
[db2inst2@db2-node01 ~]$ db2 GET DB CFG FOR GHAN |grep HADR
HADR 数据库角色
= STANDBY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.1
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN1
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.2
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN2
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node01 ~]$ db2 "takeover hadr on db ghan "
DB20000I TAKEOVER HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$ db2 connect to ghan
数据库连接信息
数据库服务器 = DB2/LINUXX8664 9.7.5
SQL 授权标识 = DB2INST2
本地数据库别名 = GHAN
[db2inst2@db2-node01 ~]$ db2 "insert into tank values(123)"
DB20000I SQL 命令成功完成。
[db2inst2@db2-node01 ~]$ db2 "insert into tank values(123)"
DB20000I SQL 命令成功完成。
[db2inst2@db2-node01 ~]$ db2 "select count(*) from tank"
1
-----------
32
1 条记录已选择。
[db2inst2@db2-node01 ~]$ db2 GET DB CFG FOR GHAN |grep HADR
HADR 数据库角色
= PRIMARY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.1
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN1
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.2
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN2
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node01 ~]$
10、双节点关闭,只启动活动节点启动,关闭备用节点
[root@db2-node01 ~]# su - db2inst2
[db2inst2@db2-node01 ~]$ db2 GET DB CFG FOR GHAN |grep HADR
HADR 数据库角色
= PRIMARY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.1
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN1
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.2
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN2
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node01 ~]$ db2 connect to ghan
SQL1768N 未能启动 HADR。原因码 = "7"
[
db2inst2@db2-node01 ~]$ db2 "START HADR ON DATABASE GHAN AS STANDBY"
DB20000I START HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$
[db2inst2@db2-node01 ~]$ db2 GET DB CFG FOR GHAN |grep HADR
HADR 数据库角色 =
STANDBY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.1
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN1
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.2
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN2
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node01 ~]$
db2 "DEACTIVATE DATABASE GHAN"
DB20000I DEACTIVATE DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$
db2 "ACTIVATE DATABASE GHAN"
DB20000I ACTIVATE DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$ db2 "takeover hadr on db ghan "
SQL1770N 不能完成接管 HADR。原因码:"1"。
[db2inst2@db2-node01 ~]$ db2 "takeover hadr on db ghan by force"
DB20000I TAKEOVER HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$ db2 connect to ghan
数据库连接信息
数据库服务器 = DB2/LINUXX8664 9.7.5
SQL 授权标识 = DB2INST2
本地数据库别名 = GHAN
[db2inst2@db2-node01 ~]$
db2 "select count(*) from tank"
1
-----------
32
1 条记录已选择。
11、双节点关闭,只启动备点节点启动,关闭主节点
[root@db2-node01 ~]# su - db2inst2
[db2inst2@db2-node01 ~]$ db2 connect to ghan
SQL1776N HADR 备用数据库或者处于当前配置或状态的 HADR
备用数据库不支持此命令。原因码:"1"。
[db2inst2@db2-node01 ~]$
[db2inst2@db2-node01 ~]$ db2 GET DB CFG FOR GHAN |grep HADR
HADR 数据库角色 =
STANDBY
HADR 本地主机名 (HADR_LOCAL_HOST) = 10.0.0.1
HADR 本地服务名称 (HADR_LOCAL_SVC) = DB2_HADR_GHAN1
HADR 远程主机名 (HADR_REMOTE_HOST) = 10.0.0.2
HADR 远程服务名称 (HADR_REMOTE_SVC) = DB2_HADR_GHAN2
远程服务器的 HADR 实例名 (HADR_REMOTE_INST) = db2inst2
HADR 超时值 (HADR_TIMEOUT) = 120
HADR 日志写同步方式 (HADR_SYNCMODE) = SYNC
HADR 对等窗口持续时间(秒) (HADR_PEER_WINDOW) = 120
[db2inst2@db2-node01 ~]$ db2 "takeover hadr on db ghan by force"
SQL1770N 不能完成接管 HADR。原因码:"5"。
[db2inst2@db2-node01 ~]$
db2 "START HADR ON DATABASE GHAN AS STANDBY"
DB20000I START HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$
db2 "takeover hadr on db ghan by force"
DB20000I TAKEOVER HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node01 ~]$
db2 connect to ghan
数据库连接信息
数据库服务器 = DB2/LINUXX8664 9.7.5
SQL 授权标识 = DB2INST2
本地数据库别名 = GHAN
[db2inst2@db2-node01 ~]$ db2 list tables
表/视图 模式 类型 创建时间
------------------------------- --------------- ----- --------------------------
GHAN DB2INST2 T 2015-12-17-02.38.27.732961
GHAN123 DB2INST2 T 2015-12-19-22.57.00.134986
TANK DB2INST2 T 2015-12-18-02.11.31.661655
3 条记录已选择。
[db2inst2@db2-node01 ~]$
db2 "select count(*) from tank"
1
-----------
27
1 条记录已选择。
[db2inst2@db2-node01 ~]$
此时启动另一节点
[db2inst2@db2-node02 ~]$
db2 "START HADR ON DATABASE GHAN AS STANDBY"
DB20000I START HADR ON DATABASE 命令成功完成。
[db2inst2@db2-node02 ~]$
db2 "get snapshot for database on GHAN"
HADR 状态
角色 = 主
状态 = 对等
同步方式 = 同步
连接状态 = 已连接, 2015-12-23 08:35:11.457230
对等窗口结束 = 2015-12-23 08:37:12.000000 (1450877832)
对等窗口(秒) = 120
丢失的脉动信号 = 0
本地主机 = 10.0.0.1
本地服务 = DB2_HADR_GHAN1
远程主机 = 10.0.0.2
远程服务 = DB2_HADR_GHAN2
远程实例 = db2inst2
超时(秒) = 120
主日志位置(文件,页,LSN) = S0000012.LOG, 0, 0000000008728010
备用日志位置(文件,页,LSN) = S0000011.LOG, 0, 0000000007F28010
日志间隔运行平均值(以字节计)= 0