前言:
CDB容器数据库在部署Dataguard备库时,有时我们只想同步指定的PDB排除哪些不重要的或者数据量大的PDB,这种方式Dataguard是可以支持实现的,但需要通过参数设置以及专门的部署步骤。
本文接下来将主要介绍如何通过参数设置以及专门的部署步骤实现CDB容器数据库在配置DG备库时只同步指定的PDB。
数据库环境:
db_role | ip | uniq_name | root_db | pdb |
primary | 192.168.146.2 | orcl | CDB$ROOT | PDB$SEED pdb pdb1 pdb3 |
standby | 192.168.146.3 | orcldg | CDB$ROOT | pdb1 |
部署步骤:
1 检查数据库开启归档以及force_logging
- ---数据库开启归档模式
- archive log list
- ---对于cdb容器数据库,该参数在cdb容器设置即可
- select force_logging from v$database;
- alter database force logging;
2 检查并开启SYS远程登录
- ---确认参数remote_login_passwordfile为EXCLUSIVE,对于cdb容器数据库,该参数在cdb容器设置即可
- show parameter remote_login_passwordfile
3 配置主备TNS
- ---主库tns(每个节点都设置)
- orcl =
- (DESCRIPTION =
- (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.146.2)(PORT = 1521))
- (CONNECT_DATA =
- (SERVER = DEDICATED)
- (SERVICE_NAME = orcl)
- )
- )
- ---备库tns(每个节点都设置)
- orcldg=
- (DESCRIPTION =
- (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.146.3)(PORT = 1521))
- (CONNECT_DATA =
- (SERVER = DEDICATED)
- (SERVICE_NAME = orcldg)
- )
- )
4 主库参数配置
- ---备份spfile
- create pfile='/tmp/initorcl.bak' from spfile;
- ---设置当前DG主备库对应唯一名
- alter system set log_archive_config='dg_config=(orcl,orcldg)' scope=both sid='*';
- ---设置主库到备库的投递链路
- alter system set log_archive_dest_2=
- 'service=orcldg LGWR ASYNC NOAFFIRM delay=0 optional
- compression=disable max_failure=0 reopen=30
- net_timeout=300 valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE)
- db_unique_name=orcldg' scope=both sid='*';
- ---设置捕获归档日志的对应的主备客户端TNS
- alter system set fal_client=orcl scope=both sid='*';
- alter system set fal_server=orcldg scope=both sid='*';
- ---设置备库文件自动管理,以确保新增数据文件能同步到备库
- alter system set standby_file_management=AUTO scope=both sid='*';
- ---设置omf文件路径,可以确保主备数据数据文件存放路径不一致时,备库可通过omf自动在新路径生成数据文件
- alter system set db_create_file_dest='/u01/app/oracle/oradata' scope=both sid='*';
5 文件配置
- ---把主库的密码文件远程拷贝到备库的$ORACLE_HOME/dbs下面,再修改成orapw$SID的方式
- scp -rp $ORACLE_HOME/dbs/orapworcl oracle@192.168.146.3:$ORACLE_HOME/dbs/orapworcldg
- ---把主库新转出的pfile文件远程拷贝到备库的$ORACLE_HOME/dbs下面
- create pfile='/tmp/initorcldg.ora' from spfile;
- scp -rp /tmp/initorcldg.ora oracle@192.168.146.3:$ORACLE_HOME/dbs/initorcldg.ora
6 备库参数配置
- ---备库修改从主库拷贝过来的参数文件
- ---参数文件修改
- *.audit_file_dest=/u01/app/oracle/admin/orcldg/adump
- *.control_files='/u01/app/oracle/oradata/ORCLDG/controlfile/control01.ctl'
- *.dispatchers='(PROTOCOL=TCP) (SERVICE=orcldgXDB)'
- *.fal_client='ORCLDG'
- *.fal_server='ORCL'
- *.log_archive_dest_2='service=orcl LGWR ASYNC NOAFFIRM delay=0 optional
- compression=disable max_failure=0 reopen=30
- net_timeout=300 valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE)
- db_unique_name=orcl'
- *.standby_file_management='AUTO'
- *.log_archive_dest_1='/u01/app/oracle/archive'
- ---内存参数,进程数,根据实际需求按比例减
- *.sga_max_size
- *.shared_pool_size
- *.db_cache_size
- *.large_pool_size
- *.pga_aggregate_target
- *.processes
- *.sessions
- ---添加参数
- *.db_unique_name=orcldg
- #添加要DG同步的pdb名称
- *.ENABLED_PDBS_ON_STANDBY=pdb1
7 备库目录创建
- su - oracle
- mkdir -p /u01/app/oracle/admin/orcldg/adump
- mkdir -p /u01/app/oracle/oradata/ORCLDG/controlfile
- mkdir -p /u01/app/oracle/oradata/ORCLDG/datafile
- mkdir -p /u01/app/oracle/oradata/ORCLDG/onlinelog
- mkdir -p /u01/app/oracle/oradata/ORCLDG/tempfile
8 启动备库实例
- ---nomount启动备库实例
- export ORACLE_SID=orcldg
- sqlplus / as sysdba
- startup nomount
- ---生成spfile文件
- create spfile from pfile;
- shutdown immediate;
- startup nomount;
9 备份根容器root以及要同步的pdb
- backup format '/home/oracle/backup/root_%U' database root ;
- backup format '/home/oracle/backup/pdb_%U' pluggable database pdb1 plus archivelog;
- ---将备份拷贝到备库相同目录下
- scp -rp /home/oracle/backup oracle@192.168.146.3:/home/oracle/
10 备库控制文件standby controlfile
- alter database create standby controlfile as '/tmp/controlstd.ctl';
- ---把主库的控制文件远程拷贝到备库控制文件路径的下面
- scp -rp /tmp/controlstd.ctl oracle@192.168.146.3:/u01/app/oracle/oradata/ORCLDG/controlfile/control01.ctl
11 将备库启动到mount
alter database mount;
12 进行全备恢复
- select con_id,name from v$pdbs
-
- CON_ID NAME
- ---------- --------------------------------------------------------------------------------------------------------------------------------
- 2 PDB$SEED
- 3 PDB
- 4 PDB1
- 6 PDB3
- ---批量生成根容器root(con_id=1)以及pdb容器pdb1(con_id=4)的路径修改命令
- set pagesize 0
- set linesize 200
- select 'set newname for datafile '||file#||' to '''||replace(name,'ORCL','ORCLDG')||''';' from v$datafile where con_id in (1,4);
-
- set pagesize 0
- set linesize 200
- select 'set newname for tempfile '||file#||' to '''||replace(name,'ORCL','ORCLDG')||''';' from v$tempfile where con_id in (1,4);
-
- set newname for datafile 1 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_system_khpomwxx_.dbf';
- set newname for datafile 3 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_sysaux_khponp2n_.dbf';
- set newname for datafile 4 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_undotbs1_khpoo54z_.dbf';
- set newname for datafile 7 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_users_khpoo66v_.dbf';
- set newname for datafile 13 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_system_khppx4x7_.dbf';
- set newname for datafile 14 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_sysaux_khppx4xg_.dbf';
- set newname for datafile 15 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_undotbs1_khppx4xh_.dbf';
- set newname for datafile 16 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_users_khppxbgj_.dbf';
- set newname for tempfile 1 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_temp_khpoppd2_.tmp';
- set newname for tempfile 4 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_temp_khppx4xh_.dbf';
-
- ---语句批量生成跳过表空间
- set pagesize 400
- set linesize 400
- select listagg('"'||b.name||'"'||':'||a.name||',')
- from v$tablespace a,v$containers b
- where a.con_id=b.con_id and b.name not in ('CDB$ROOT','PDB1');
-
- "PDB$SEED":SYSTEM,"PDB$SEED":SYSAUX,"PDB$SEED":UNDOTBS1,"PDB$SEED":TEMP,"PDB":SYSAUX,"PDB":SYSTEM,"PDB":UNDOTBS1,"PDB":USERS,"PDB":TEMP,"PDB3":SYSTEM,"PDB3":UNDOTBS1,"PDB3":SYSAUX,"PDB3":TEMP,
- ---创建备份目录
- mkdir -p /u01/app/oracle/oradata/ORCLDG/datafile
- mkdir -p /u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile
- ---进行备份恢复
- rman target /
- run
- {
- set newname for datafile 1 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_system_khpomwxx_.dbf';
- set newname for datafile 3 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_sysaux_khponp2n_.dbf';
- set newname for datafile 4 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_undotbs1_khpoo54z_.dbf';
- set newname for datafile 7 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_users_khpoo66v_.dbf';
- set newname for datafile 13 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_system_khppx4x7_.dbf';
- set newname for datafile 14 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_sysaux_khppx4xg_.dbf';
- set newname for datafile 15 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_undotbs1_khppx4xh_.dbf';
- set newname for datafile 16 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_users_khppxbgj_.dbf';
- set newname for tempfile 1 to '/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_temp_khpoppd2_.tmp';
- set newname for tempfile 4 to '/u01/app/oracle/oradata/ORCLDG/E658EB1E89977B15E0536401A8C0D7D3/datafile/o1_mf_temp_khppx4xh_.dbf';
- restore database root;
- restore pluggable database PDB1;
- switch datafile all;
- switch tempfile all;
- recover database skip forever tablespace "PDB$SEED":SYSTEM,"PDB$SEED":SYSAUX,"PDB$SEED":UNDOTBS1,"PDB$SEED":TEMP,"PDB":SYSAUX,"PDB":SYSTEM,"PDB":UNDOTBS1,"PDB":USERS,"PDB":TEMP,"PDB3":SYSTEM,"PDB3":UNDOTBS1,"PDB3":SYSAUX,"PDB3":TEMP;
- }
13 添加standby log
- alter database ADD standby logfile thread 1 group 21 size 200M ;
- alter database ADD standby logfile thread 1 group 22 size 200M ;
- alter database ADD standby logfile thread 1 group 23 size 200M ;
14 打开根容器
alter database open;
15 禁用其他容器(除了root,pdb1)的恢复
- alter session set container=PDB$SEED;
- alter pluggable database PDB$SEED disable recovery;
- alter session set container=PDB;
- alter pluggable database PDB disable recovery;
- alter session set container=PDB3;
- alter pluggable database PDB3 disable recovery;
16 打开容器pdb1
alter pluggable database pdb1 open;
17 启动mrp进程
alter database recover managed standby database using current logfile disconnect from session parallel 8;
18 检查备库同步
- ---实时同步中
- select * from v$dataguard_stats
-
- SOURCE_DBID SOURCE_DB_UNIQUE_NAME NAME VALUE UNIT TIME_COMPUTED DATUM_TIME CON_ID
- ----------- -------------------------------- -------------------------------- ---------------------------------------------------------------- ------------------------------ ------------------------------ ------------------------------ ----------
- 1640672463 orcl transport lag +00 00:00:00 day(2) to second(0) interval 08/19/2022 22:51:10 08/19/2022 22:51:09 0
- 1640672463 orcl apply lag +00 00:00:00 day(2) to second(0) interval 08/19/2022 22:51:10 08/19/2022 22:51:09 0
- 1640672463 orcl apply finish time day(2) to second(3) interval 08/19/2022 22:51:10 0
- 0 estimated startup time 13 second 08/19/2022 22:51:10 0
19 测试数据同步
- ---在主库pdb1创建表
- SQL> select database_role from v$database;
-
- DATABASE_ROLE
- ----------------
- PRIMARY
- SQL> alter session set container=pdb1;
- Session altered.
-
-
- SQL> create table test as select * from dba_objects;
-
- Table created.
-
- ---在备库可以查到
- SQL> select database_role from v$database;
-
- DATABASE_ROLE
- ----------------
- PHYSICAL STANDBY
-
- SQL> alter session set container=pdb1;
-
- Session altered.
-
- SQL> select count(*) from test;
-
- COUNT(*)
- ----------
- 72356
-
- ---在主库pdb3创建表,备库没有同步,并且同步状态正常