首页 > 数据库 >db2主备部署hadr(单个数据库)

db2主备部署hadr(单个数据库)

时间:2024-02-01 10:57:11浏览次数:32  
标签:db2inst1 db2 HADR cfg db hxl 主备 hadr

主库:192.168.1.135 host135
从库:192.168.1.134 host134

 

说明:
a.主库已经运行并有数据库DB_HXL
可以使用如下命令查看:
db2 list database directory

 

b.主数据库已经处于归档模式,做了主备后,备库也会是归档模式,归档路径与主库配置的是一样的,这就需要备库提前有相应的目录.
db2 get db cfg for db_hxl |grep -i LOGARCHMETH1
具体配置请参考如下链接:
https://www.cnblogs.com/hxlasky/p/17986410


c.从库已经创建了实例,但是不建库
实例名与主库保持一致,并启动实例
[db2inst1@host134 ~]$ ss -nlp|grep 50000

 

1.主库配置HADR同步参数
db2inst1用户下执行

db2 update db cfg for db_hxl using logretain on ##去掉
db2 update db cfg for db_hxl using trackmod on  ##好像需要重启动
db2 update db cfg for db_hxl using logindexbuild on
db2 update db cfg for db_hxl using indexrec restart
db2 update db cfg for db_hxl using HADR_LOCAL_HOST 192.168.1.135 ##好像需要重启动
db2 update db cfg for db_hxl using HADR_LOCAL_SVC 60006 (当启动HADR后,这个端口就会打开,负责复制的通信,端口可自定义)
db2 update db cfg for db_hxl using HADR_REMOTE_HOST 192.168.1.134
db2 update db cfg for db_hxl using HADR_REMOTE_SVC 60006
db2 update db cfg for db_hxl using HADR_REMOTE_INST db2inst1
db2 update db cfg for db_hxl using HADR_TIMEOUT 120
db2 update db cfg for db_hxl using HADR_PEER_WINDOW 10
db2 update db cfg for db_hxl using HADR_SYNCMODE sync

 

尝试重启数据库
db2 restart db db_hxl


[db2inst1@host135 ~]$ db2 restart db db_hxl
DB20000I The RESTART DATABASE command completed successfully.


查看配置:
db2 get db cfg for db_hxl|grep HADR_LOCAL_HOST
db2 get db cfg for db_hxl|grep HADR_LOCAL_SVC

 

2.备份数据库

在主库的db2inst1用户下执行
db2 force applications all
db2 backup database db_hxl to /home/db2inst1/backup

[db2inst1@host135 backup]$ db2 backup database db_hxl to /home/db2inst1/backup
SQL1433N The application is already connected to "DB_HXL01" while the command
issued requires a connection to "DB_HXL" for successful execution.
解决办法:
退出后重新登录执行

 

3.备份文件拷贝到备库
拷贝到目的库相应的目录
scp DB_HXL.0.db2inst1.DBPART000.20240131164653.001 [email protected]:/home/db2inst1/backup/

 

4.备库创建数据库库并恢复
在备库上执行

db2 create db db_hxl using codeset utf-8 territory CN
db2 restore db db_hxl from /home/db2inst1/backup

[db2inst1@host134 backup]$ db2 restore db db_hxl from /home/db2inst1/backup
SQL2523W  Warning!  Restoring to an existing database that is different from 
the database on the backup image, but have matching names. The target database 
will be overwritten by the backup version.  The Roll-forward recovery logs 
associated with the target database will be deleted.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.


这个时候好像链接不上备库的
db2 => connect to db_hxl
SQL1117N  A connection to or activation of database "DB_HXL" cannot be made 
because of ROLL-FORWARD PENDING.  SQLSTATE=57019

 

5.开启备库可读
主备库都执行,切换后原来的主库变成备库,可提供读服务.

db2set DB2_HADR_ROS=ON
db2set DB2_STANDBY_ISO=UR
[db2inst1@host134 backup]$ db2set -all
[i] DB2_STANDBY_ISO=UR
[i] DB2_HADR_ROS=ON
[i] DB2COMM=TCPIP
[g] DB2SYSTEM=host134

 

6.修改备库参数
配置备库参数

db2 update db cfg for db_hxl using logretain on ##取消
db2 update db cfg for db_hxl using trackmod on
db2 update db cfg for db_hxl using logindexbuild on
db2 update db cfg for db_hxl using indexrec restart
db2 update db cfg for db_hxl using HADR_LOCAL_HOST 192.168.1.134
db2 update db cfg for db_hxl using HADR_LOCAL_SVC 60006
db2 update db cfg for db_hxl using HADR_REMOTE_HOST 192.168.1.135
db2 update db cfg for db_hxl using HADR_REMOTE_SVC 60006
db2 update db cfg for db_hxl using HADR_REMOTE_INST db2inst1
db2 update db cfg for db_hxl using HADR_TIMEOUT 120
db2 update db cfg for db_hxl using HADR_PEER_WINDOW 10
db2 update db cfg for db_hxl using HADR_SYNCMODE sync

 

7.启动备库,必须先启动备库,再启动主库
db2stop
db2start
若实例已经启动的情况下,不需要重启动
db2 "deactivate database db_hxl"
db2 start hadr on db db_hxl as standby

8.启动主库
db2 "deactivate database db_hxl"
db2 start hadr on db db_hxl as primary

 

9.监控主备数据库状态:
在主备库上执行
db2pd -hadr -db db_hxl
找到如下项目,红色部分,说明主从同步正常

[db2inst1@host135 backup]$ db2pd -hadr -db db_hxl

Database Member 0 -- Database DB_HXL -- Active -- Up 0 days 00:16:47 -- Date 2024-01-31-17.26.34.151110

                            HADR_ROLE = PRIMARY
                          REPLAY_TYPE = PHYSICAL
                        HADR_SYNCMODE = SYNC
                           STANDBY_ID = 1
                        LOG_STREAM_ID = 0
                           HADR_STATE = PEER
                           HADR_FLAGS = TCP_PROTOCOL
                  PRIMARY_MEMBER_HOST = 192.168.1.135
                     PRIMARY_INSTANCE = db2inst1
                       PRIMARY_MEMBER = 0
                  STANDBY_MEMBER_HOST = 192.168.1.134
                     STANDBY_INSTANCE = db2inst1
                       STANDBY_MEMBER = 0
                  HADR_CONNECT_STATUS = CONNECTED

 

从库执行

[db2inst1@host134 ~]$ db2pd -hadr -db db_hxl
Database Member 0 -- Database DB_HXL -- Active Standby -- Up 0 days 00:17:51 -- Date 2024-01-31-17.27.23.616711

                            HADR_ROLE = STANDBY
                          REPLAY_TYPE = PHYSICAL
                        HADR_SYNCMODE = SYNC
                           STANDBY_ID = 0
                        LOG_STREAM_ID = 0
                           HADR_STATE = PEER
                           HADR_FLAGS = TCP_PROTOCOL
                  PRIMARY_MEMBER_HOST = 192.168.1.135
                     PRIMARY_INSTANCE = db2inst1
                       PRIMARY_MEMBER = 0
                  STANDBY_MEMBER_HOST = 192.168.1.134
                     STANDBY_INSTANCE = db2inst1
                       STANDBY_MEMBER = 0
                  HADR_CONNECT_STATUS = CONNECTED

 

10.登录从库

[db2inst1@host134 backup]$ db2
db2 => connect to db_hxl
db2 => select * from tb_test;

ID                   NAME                 USER_AGES  
-------------------- -------------------- -----------
                   1 hxl                           20
                   2 hxl01                         30
                   
可以查到刚才恢复的数据

 

11.数据验证
主库建表,并写入数据

[db2inst1@host135 bin]$ db2
db2 => connect to db_hxl
db2 => create table tb_test01(id bigint not null generated by default as identity (start with 1,increment by 1),name varchar(20),user_ages int)
db2 => insert into tb_test01(name,user_ages) values('hxl',20);
DB20000I  The SQL command completed successfully.
db2 => insert into tb_test01(name,user_ages) values('hxl01',30);
DB20000I  The SQL command completed successfully.

 

11.登录备库查看

db2 => select * from tb_test01;

ID                   NAME                 USER_AGES  
-------------------- -------------------- -----------
                   1 hxl                           20
                   2 hxl01                         30

  2 record(s) selected.
  
可以看到主从同步完成

尝试在从库上写入数据会报如下错误:
db2 => insert into tb_test01(name,user_ages) values('hxl01',30);
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL1773N  The statement or command failed because it requires functionality 
that is not supported on a read-enabled HADR standby database. Reason code = "5".

 

12.主从库的正常停止和启动
#########停HADR,先停主在停从
主:
db2 deactivate database db_hxl
db2 stop hadr on database db_hxl

备:
db2 deactivate database db_hxl
db2 stop hadr on database db_hxl

 

########起HADR,先启从再启主
备:
db2 deactivate database db_hxl
db2 start hadr on database db_hxl as standby

主:
db2 deactivate database db_hxl
db2 start hadr on database db_hxl as primary

 

13.主从切换
主备库切换
备库接管主库,下面命令只能在备库执行,我这里是在192.168.1.134机器上执行.
正常接管,相当于Oracle的standby的switchover,接管之后,主变备,备变主,重新在原主库(也就是现在的备库执行此命令),hadr重新回到原来状态.
[db2inst1@host134 ~]$ db2 takeover hadr on database db_hxl
DB20000I The TAKEOVER HADR ON DATABASE command completed successfully.

这个时候查看主从库的状态
目前的主库(原从库),192.168.1.134

[db2inst1@host134 ~]$ db2pd -hadr -db db_hxl

Database Member 0 -- Database DB_HXL -- Active -- Up 0 days 16:52:55 -- Date 2024-02-01-10.28.46.650953

                            HADR_ROLE = PRIMARY
                          REPLAY_TYPE = PHYSICAL
                        HADR_SYNCMODE = SYNC
                           STANDBY_ID = 1
                        LOG_STREAM_ID = 0
                           HADR_STATE = PEER
                           HADR_FLAGS = TCP_PROTOCOL
                  PRIMARY_MEMBER_HOST = 192.168.1.134
                     PRIMARY_INSTANCE = db2inst1
                       PRIMARY_MEMBER = 0
                  STANDBY_MEMBER_HOST = 192.168.1.135
                     STANDBY_INSTANCE = db2inst1
                       STANDBY_MEMBER = 0
                  HADR_CONNECT_STATUS = CONNECTED

 

目前的从库(原主库),192.168.1.135

[db2inst1@host135 ~]$ db2pd -hadr -db db_hxl

Database Member 0 -- Database DB_HXL -- Active Standby -- Up 0 days 16:53:32 -- Date 2024-02-01-10.29.35.314130

                            HADR_ROLE = STANDBY
                          REPLAY_TYPE = PHYSICAL
                        HADR_SYNCMODE = SYNC
                           STANDBY_ID = 0
                        LOG_STREAM_ID = 0
                           HADR_STATE = PEER
                           HADR_FLAGS = TCP_PROTOCOL
                  PRIMARY_MEMBER_HOST = 192.168.1.134
                     PRIMARY_INSTANCE = db2inst1
                       PRIMARY_MEMBER = 0
                  STANDBY_MEMBER_HOST = 192.168.1.135
                     STANDBY_INSTANCE = db2inst1
                       STANDBY_MEMBER = 0
                  HADR_CONNECT_STATUS = CONNECTED

 

强制切换
db2 takeover hadr on db db_hxl by force
一定要在primary为down的状态下执行.

 

14.强制关机

 

标签:db2inst1,db2,HADR,cfg,db,hxl,主备,hadr
From: https://www.cnblogs.com/hxlasky/p/18000735

相关文章

  • 达梦数据库配置-主备
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>spring:datasource:driver-class-name:dm.jdbc.driver.DmD......
  • db2设置归档模式
    环境:Os:Centos7DB:V11.5.61.查看数据库是否处于归档模式[db2inst1@host135SQL00001]$db2getdbcfgfordb_hxl|grepLOGARCHFirstlogarchivemethod(LOGARCHMETH1)=OFFArchivecompressionforlogarchmeth1(LOGARCHCOMPR1)=OFFOptio......
  • db2安装部署
    环境:OS:Centos7DB:v11.5.61.解压[root@host135db2]#tar-xvfv11.5.6_linuxx64_server_dec.tar.gz[root@host135db2]#mvserver_dec/opt/db2 2.检查安装环境[root@host135db2]#./db2prereqcheck 3.安装依赖包yuminstalllibstdc++*yuminstallpam*yumgroupinst......
  • DM8备份和还原以及实时主备搭建
    一、备份和还原1.1联机备份数据库处于运行状态、并正常提供数据库服务情况下进行的备份操作,我们称为联机备份。数据库处于关闭状态时进行的备份操作,被称为脱机备份。一般生产环境下多数会选择联机备份,即在数据库运行的情况下进行数据库的备份。另备份还可以区分为库备份、表空......
  • postgresql 14 主备切换
    主从切换1.停止主节点$pg_ctlstopwaitingforservertoshutdown....doneserverstopped2.从节点提升为主节点$pg_ctlpromotewaitingforservertopromote....doneserverpromoted验证是否切换成功$psql-h127.0.0.1-p5432-Upostgres-W-c"select......
  • openGauss学习笔记-176 openGauss 数据库运维-实例主备切换
    openGauss学习笔记-176openGauss数据库运维-实例主备切换176.1操作场景openGauss在运行过程中,数据库管理员可能需要手工对数据库节点做主备切换。例如发现数据库节点主备failover后需要恢复原有的主备角色,或怀疑硬件故障需要手动进行主备切换。级联备机不能直接转换为主机,只能......
  • DB207S-ASEMI迷你贴片整流桥DB207S
    编辑:llDB207S-ASEMI迷你贴片整流桥DB207S型号:DB207S品牌:ASEMI封装:DBS-4最大平均正向电流:2A最大重复峰值反向电压:1000V产品引线数量:4产品内部芯片个数:4产品内部芯片尺寸:55MIL峰值正向漏电流:<10ua恢复时间:>2000ns浪涌电流:50A芯片材质:光阻GPP最大正向电压:1.10V工作结温:-55℃~150℃包装......
  • DB207S-ASEMI迷你贴片整流桥DB207S
    编辑:llDB207S-ASEMI迷你贴片整流桥DB207S型号:DB207S品牌:ASEMI封装:DBS-4最大平均正向电流:2A最大重复峰值反向电压:1000V产品引线数量:4产品内部芯片个数:4产品内部芯片尺寸:55MIL峰值正向漏电流:<10ua恢复时间:>2000ns浪涌电流:50A芯片材质:光阻GPP最大正向电压:1.10V工作结温......
  • DB2链接工具
    前提,电脑配置了java环境; 安装完后,打开这个文件夹 打开下面的文件 执行步骤234,其中第4步如果没有管理员权限,可以把文档复制出来,编辑保存后,再覆盖原文档; 链接数据库,找到响应的库表即可; ......
  • 麒麟ARM系统 - 双机达梦8主备搭建
    环境说明1.1硬件需求至少需要三台服务器,1主、1从、1监视器。由于资源有限,将监视器安装在主库机器上,本次实验使用2台虚拟机。操作系统:麒麟arm64服务器版V10uname-r4.19.90-17.ky10.aarch64master节点:192.168.1.175backup节点:192.168.1.1761.2网络需求心跳I......