物理DataGard搭建、主备switchover、物理DataGard转逻辑DataGard
第一大步:基本信息配置:
配置主机名:
hostname dg01.cluster.com
vim /etc/hosts
10.1.1.36 dg01.cluster.com
10.1.1.37 dg02.cluster.com
vim /etc/sysconfig/network
主库操作:
SQL> alter database force logging;
DG两个库还会分为一些情况:
两个库的实例名是否一致?
两个库的db_name,instance_name,sid都可以一致
但是 db_unique_name 需要不一致
SQL> alter system set db_unique_name=pri scope=spfile;
--因为这个是静态参数,所以只能改到spfile里,然后再重启数据库,那就违背了不关库的原则,那么你可以在这里不改,就用orcl,只改备库就好了;
--此实验为了方便,还是在这里改了一下,并重启生效
SQL> show parameter db_unique_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_unique_name string PRI
SQL> alter system set log_archive_config='dg_config=(pri,std)';
SQL> ho mkdir /u01/oracle/arch
SQL> alter system set log_archive_dest_1='location=/u01/oracle/arch/ valid_for=(all_logfiles,all_roles) db_unique_name=pri';
SQL> alter system set log_archive_dest_2='service=std lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=std';
SQL> show parameter log_archive_dest_state_1
SQL> show parameter log_archive_dest_state_2 --确认这两个参数为enable的
SQL> show parameter log_archive_format
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_format string %t_%s_%r.dbf --格式默认就好
SQL> alter system set fal_server=std;
SQL> alter system set fal_client=pri; --注意这里反着写的,表示切换后的主和备
=================================================
DB_FILE_NAME_CONVERT='boston','chicago'
LOG_FILE_NAME_CONVERT=
'/arch1/boston/','/arch1/chicago/','/arch2/boston/','/arch2/chicago/'
--上面两个参数是官档上定义不同路径转换时用的,(也就是两个库路径不同,需要这样定义来转换)
--我们这里为了方便,做的两个库,路径是相同的,所以就不需要上面的两个参数
==============================================
SQL> alter system set standby_file_management=auto;
SQL> alter database add standby logfile group 5 '/u01/oracle/oradata/orcl/stredo5.log' size 50m;
SQL> alter database add standby logfile group 6 '/u01/oracle/oradata/orcl/stredo6.log' size 50m;
SQL> alter database add standby logfile group 7 '/u01/oracle/oradata/orcl/stredo7.log' size 50m;
SQL> alter database add standby logfile group 8 '/u01/oracle/oradata/orcl/stredo8.log' size 50m;
--至此,参数修改完毕,然后由修改后的spfile创建一个pfile文件,以便拷到备库修改使用
SQL> create pfile from spfile;
第二大步:
[oracle@dg01 ~]$ mkdir /u01/oracle/bak
RMAN> backup format '/u01/oracle/bak/full_%U' database plus archivelog;
--全备,注意加归档
RMAN> backup format '/u01/oracle/bak/con_%U' current controlfile for standby;
--注意控制文件要加一个for standby,因为主库两边是不同的
第三大步:
考虑拷贝什么到备库
nomount spfile
mount controlfile
open dbf
密码文件
所以要拷pfile (拷过去再修改),controlfile的备份(con_%U),全备(full_%U),密码文件
对备库的软件安装需求:
装oracle软件(路径保持一致),不需要装库
mkdir /u01/oracle/oradata/orcl
mkdir /u01/oracle/admin/orcl/{a,b,u,dp,c}dump -p
mkdir /u01/oracle/arch
mkdir /u01/oracle/bak
--创建这些目录,注意使用oracle用户去创建
[oracle@dg01 ~]$ scp /u01/oracle/10g/dbs/initorcl.ora dg02.cluster.com:/u01/oracle/10g/dbs/
--拷参数文件
[oracle@dg01 ~]$ scp /u01/oracle/bak/* dg02.cluster.com:/u01/oracle/bak/
--拷控制文件和全备的rman备份
[oracle@dg02 ~]$ orapwd file=/u01/oracle/10g/dbs/orapworcl password=oracle force=y
--在备库重新密码文件,或者从主库scp过来也是一样
--注意,scp拷的时候请用oracle用户,否则用root用户拷过来,要修改权限
第四大步:
监听的配置
--主库上的监听配置
[oracle@dg01 ~]$ vim /u01/oracle/10g/network/admin/listener.ora
--注意要为dg01的主机名,或者ip,端口1521
[oracle@dg01 ~]$ vim /u01/oracle/10g/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.36)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
--这一段是本来就有的,下面两段是加的
pri =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.36)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pri)
)
)
std =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.37)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = std)
)
)
--备库上的监听配置
[oracle@dg02 ~]$ vim /u01/oracle/10g/network/admin/listener.ora
--要为备库的主机名或者IP,端口也为1521
[oracle@dg02 ~]$ vim /u01/oracle/10g/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.37)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
--这一段默认就有,下面两段是加的
pri =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.36)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pri)
)
)
std =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.37)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = std)
)
)
两边配置好监听后使用lsnrctl start命令启动监听
然后两边互相tnsping orcl
tnsping pri
tnsping std
--上面都互相能tnsping通,最好在备库上远程连接一下主库,要求成功,才说明监听没问题
[oracle@dg02 ~]$ uniread sqlplus sys/oracle@pri as sysdba
第五大步:恢复备库,使之和主库同步
--先修改主库上拷过来的pfile,把std和pri互换,注意手动去做,不要去替换
[oracle@dg02 ~]$ vim /u01/oracle/10g/dbs/initorcl.ora
orcl.__db_cache_size=83886080
orcl.__java_pool_size=4194304
orcl.__large_pool_size=4194304
orcl.__shared_pool_size=71303168
orcl.__streams_pool_size=0
*.audit_file_dest='/u01/oracle/admin/orcl/adump'
*.background_dump_dest='/u01/oracle/admin/orcl/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/u01/oracle/oradata/orcl/control01.ctl','/u01/oracle/oradata/orcl/control02.ctl','/u01/oracle/oradata/orcl/control03.ctl'
*.core_dump_dest='/u01/oracle/admin/orcl/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='orcl'
*.db_recovery_file_dest='/u01/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.db_unique_name='std' --换
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.fal_client='std' --换
*.fal_server='pri' --换
*.job_queue_processes=10
*.log_archive_config='dg_config=(std,pri)' --可换可不换,这里我还是换了
*.log_archive_dest_1='location=/u01/oracle/arch/ valid_for=(all_logfiles,all_roles) db_unique_name=std' --换
*.log_archive_dest_2='service=pri lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=pri' --换,但是primary这是个单词不要换成stdmary
*.open_cursors=300
*.pga_aggregate_target=16777216
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=167772160
*.standby_file_management='AUTO'
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/u01/oracle/admin/orcl/udump'
[oracle@dg02 ~]$ uniread sqlplus / as sysdba
Connected to an idle instance.
SQL> create spfile from pfile; --备库是空闲实例状态时也可以创建spfile
SQL> startup nomount --启动到mount状态
--在备库上进行操作
[oracle@dg02 ~]$ uniread rman target sys/oracle@pri auxiliary /
connected to target database: ORCL (DBID=1271897179)
connected to auxiliary database (not mounted)
--可以从连接信息看到是同时连到主和备(注意此时主库为open状态,备库为nomount状态)
RMAN> duplicate target database for standby nofilenamecheck;
--把拷过来的备份(全备和控制文件备份)进行恢复,如果两个库路径一致,就可以加上nofilenamecheck
[oracle@dg02 ~]$ uniread sqlplus / as sysdba
--恢复完成后,备库自动会由nomount状态到mount状态;现在退出rman,连接sqlplus
SQL> recover managed standby database disconnect from session;
--这句话是打开日志传递应用功能
--至此,dg搭建完成;现在可以进行验证
验证方法:
主库上alter system switch logfile;
然后去备库archive log list;查看当前的归档日志序列号是否跟着变动,并且和主库一致;如果是,表明成功
也可以创建一个小表空间,再切换日志,在备库的物理上能看到这个dbf也表明成功
==============================================================================
关闭DG
先关主,再关备
开启DG
先开备,再开主
关闭的方法:
主
SQL> shutdown immediate
备:
SQL> recover managed standby database cancel; --取消日志应用功能
SQL> shutdown immediate
然后关闭监听
启动的方法:
先启监听
备:
SQL> startup nomount
SQL> alter database mount standby database;
SQL> recover managed standby database disconnect from session;
主:
SQL> startup
===========================================
备库现在是mount状态,不能查询数据
物理DG
那么可以把它open read only;如果read only,备库就只能查询,这种读写分离比较变相;
因为备库open后,就不应用日志,那么你这种查询或者做备份都是只能查和备open以前的数据
物理DG
主 备
mount 可以应用日志
open read only 不可以应用日志
备库操作:
SQL> recover managed standby database cancel;
SQL> alter database open read only; --以只读方式打开
--open read only后,可以在主库建一个表,并切换日志;备库进行查询,发现查不到,说明只要open了就不可以应用日志
--那么想要备库能查询这个刚建的表,需要切回到mount状态去应用日志,再关闭应用日志,打开到open read only状态
SQL> alter database close;
SQL> recover managed standby database disconnect from session;
SQL> recover managed standby database cancel;
SQL> alter database open read only;--再一次read only后,就可以查询到这张表了
--最好是不要把备库open read write;因为备库能写那么写的改变不能传回主库,就会造成两边不一致;
此时只能把备库闪回到以前的状态,再应用日志;或者是恢复到以前的状态再应用日志
==================================================================================
主备切换:
切换顺序,把主切换成从;再把从切换成主
--原来的主库上切成备的操作:
SQL> select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PRIMARY SESSIONS ACTIVE
SQL> alter database commit to switchover to physical standby with session shutdown;
SQL> shutdown immediate
SQL> startup nomount
SQL> alter database mount standby database;
SQL> recover managed standby database disconnect from session;
SQL> select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PHYSICAL STANDBY TO PRIMARY
--原来的备库切成主库的操作:
SQL> select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PHYSICAL STANDBY TO PRIMARY
SQL> alter database commit to switchover to primary with session shutdown;
=============================================================
--切的时候报了下面错误;发现是上面的实验把备库open read only了,所以提示需要介质恢复;那么(alter database close),
再应用日志(recover managed standby database disconnect from session;),再来切换就可以了
SQL> alter database commit to switchover to primary with session shutdown;
alter database commit to switchover to primary with session shutdown
*
ERROR at line 1:
ORA-16139: media recovery required
=============================================================
SQL> shutdown immediate
SQL> startup
SQL> select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PRIMARY SESSIONS ACTIVE
--按照上面的步骤再切换回来,也是OK的
================================================================================
逻辑DG:
物理DG:通过redo apply来恢复进行同步
逻辑DG:通过sql apply来恢复进行同步(因为有些sql操作是有限制的,不能实现100%的容灾)
逻辑DG可以在open状态下完成数据同步(物理DGopen状态下不能应用日志)
它的物理结构可以不一致(因为物理DG的恢复原理是需要物理上结构一致的,位置要相同,
而逻辑DG的sql应用,只关心逻辑上是否是同一个名称,物理结构可以不一致)
逻辑DG可以应用于读写分离的架构,或者是抽样查询等
逻辑DG的实现:通过物理DG转换成逻辑DG
1,备库关闭日志应用:
SQL> recover managed standby database cancel;
2,主库运行dbms_logstby.build
SQL> exec dbms_logstdby.build
SQL> alter system archive log current;
3,再来备库操作:
SQL> alter database recover to logical standby orcl;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database open resetlogs;
SQL> alter database start logical standby apply [immediate];
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
LOGICAL STANDBY