标签(空格分隔): 知识库 Oracle 系统集成 dataguard
- 查看数据库状态:
select NAME,OPEN_MODE,LOG_MODE,PROTECTION_MODE,ACTIVATION#,SWITCHOVER#,SWITCHOVER_STATUS,database_role from v$database;
- 切归档:
alter system switch logfile;
- 查看当前归档号:
select max(sequence#) from v$archived_log;
- 直接重启至某个状态
startup [nomount/mount] force;
select status from v$instance; --查看数据库状态
select member from v$logfile; --联机重做日志
select name from v$datafile; --日志文件
select name from v$tempfile; --临时数据文件
show parameter control_files;(select status,name from v$controlfile)--查看控制文件
建议在部署过程中一直开着方便随时查看问题
tail -f /home/oracle/app/diag/rdbms/itpuxdb/itpuxdb/trace/alert_itpuxdb.log
tail -f /home/oracle/app/diag/rdbms/stitpux/stitpux/trace/alert_stitpux.log
10.0.0.51 db01 –主库
10.0.0.52 itpuxdg –备库
将两个参数配置到两个 /etc/hosts 里并确保两边都能用该解析ping到对方
alter database force logging;
show parameter recover; 使用db_recovery_file_dest
1)
SQL> show parameter recover; --查看当前归档日志路径
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /home/oracle/app/flash_recover
y_area
db_recovery_file_dest_size big integer 3882M
recovery_parallelism integer 0
SQL> archive log list; --查看当前归档是否开启
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 88
Current log sequence 90
SQL> alter database force logging;启日志
SQL> alter system set db_recovery_file_dest_size=4G scope=both; 设归档大小
SQL> !mkdir /home/oracle/arch;
SQL> alter system set db_recovery_file_dest='/home/oracle/arch' scope=both; 设归档路径
SQL> shutdown immediate;
SQL> startup mount; 重启至mount状态
SQL> alter database archivelog; 启用归档
SQL> alter database open; 打开数据库
SQL> archive log list; 再次查看归档配置是否生效
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 88
Next log sequence to archive 90
Current log sequence 90
SQL> alter system switch logfile; 此处注意要切一次归档日志,否则新的目录不会立即投入到当前环境
SQL> !ls /home/oracle/arch/ORCL/archivelog/2017_01_06/
此时验证新的归档日志有没有在目标目录生成
o1_mf_1_90_d6yfropj_.arc
SQL> alter system set db_create_file_dest='';
一般来说数据库默认使用了一部分omf自动命名规则,该规则在主备库中不方便确认文件的具体类型,因此会通过该命令取消OMF功能.不过该功能只影响数据库dbf文件,不能影响arc文件命名.
2)创建备机日志文件 standby redolog
SQL> set lin 200 --准备开启联机重做日志
SQL> col MEMNER format a60;
SQL> select * from v$logfile;--一般在这里可以看到冗余group#字段可看
GROUP# STATUS TYPE MEMBER IS_
---------- ------- ------- ------------------------------------------------------------ ---
3 ONLINE /home/oracle/app/oradata/orcl/redo03.log NO
2 ONLINE /home/oracle/app/oradata/orcl/redo02.log NO
1 ONLINE /home/oracle/app/oradata/orcl/redo01.log NO
SQL> select group#,sequence#,bytes from v$log;--在此可看主库日志文件大小
GROUP# SEQUENCE# BYTES
---------- ---------- ----------
1 91 52428800
2 89 52428800
3 90 52428800
目前要创建比上边多1组和大小比上边日志大小大的的备机日志文件.
SQL> show parameter standby; 查看备机日志生成方式
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
standby_archive_dest string ?/dbs/arch
standby_file_management string MANUAL
此处MANUAL为手工方式,建议改为自动方式
SQL> alter database add standby logfile group 4 '/home/oracle/app/oradata/orcl/redo04.log' size 52M;
SQL> alter database add standby logfile group 5 '/home/oracle/app/oradata/orcl/redo05.log' size 52M;
SQL> alter database add standby logfile group 6 '/home/oracle/app/oradata/orcl/redo06.log' size 52M;
SQL> alter database add standby logfile group 7 '/home/oracle/app/oradata/orcl/redo07.log' size 52M;
SQL> alter system set standby_file_management=auto scope=both;
SQL> select * from v$logfile; 查看刚刚添加的数据文件是否在内
SQL> select * from v$standby_log; 可查看备机日志文件是否加入及状态,目前一般为不可用
3) 修改实例名 SID
先修改环境变量里的SID为itpuxdb并source生效
[oracle@localhost ~]$ cd $ORACLE_HOME/dbs
[oracle@localhost dbs]$ ls
hc_DBUA0.dat hc_orcl.dat init.ora lkORCL orapworcl spfileorcl.ora
[oracle@localhost dbs]$ mv hc_orcl.dat hc_itpuxdb.dat
[oracle@localhost dbs]$ mv init.ora inititpuxdb.ora
[oracle@localhost dbs]$ mv lkORCL lkITPUXDB
[oracle@localhost dbs]$ mv orapworcl orapwitpuxdb
[oracle@localhost dbs]$ mv spfileorcl.ora spfileitpuxdb.ora ;
使用dba startup 后可用 sqlplus 普通用户/普户密码@itpuxdb 连接成功
SQL> select INSTANCE_NAME from v$instance;
INSTANCE_NAME
----------------
itpuxdb
--设置主库名(一般不用改)
alter system set db_unique_name='itpuxdb' scope=spfile sid='*';
alter system set service_names='itpuxdb' scope=spfile sid='*';
(X)db_name 要求两边数据库一致,如此现在就不改了
alter system set db_name='itpuxdb' scope=spfile sid='*';
shutdown immediate;
startup;
show parameter name;
startup mount pfile='/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs/inititpuxdb.ora';
4) 配监听
先拷一份生产的tns.ora,并把备库tns连接配置到里边,最终里边有两个监听地址,一个是主库一个是备库
此处注意两个都需要IP地址,否则复制到备库时会出错
ITPUXDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.220)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = itpuxdb)
)
)
STITPUX =
(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.39)(PORT=1521))
(CONNECT_DATA=
(SERVER=DEDICATED)
(SERVICE_NAME=stitpux)
)
)
5) 配静态监听 listen.ora
最终里边有两个监听项,一个是SID_LIST_LISTENER,一个是LISTENER;
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = itpuxdb)
(SID_NAME = itpuxdb)
(ORACLE_HOME = /home/oracle/app/oracle/product/11.2.0/dbhome_1)
(PRESPAWN_MAX = 20)
(PRESPAWN_LIST =
(PRESPAWN_DESC =
(PROTOCOL = tcp)
(POOL_SIZE = 2)
(TIMEOUT = 1)
)
)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /home/oracle/app
--重新加载监听
lsnrctl reload
tnsping 本机,成功则ok
6) 增加参数
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(itpuxdb,stitpux)' scope=both sid='*';
--识别本地归档路径
alter system set LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=itpuxdb' scope=both sid='*';
alter system set LOG_ARCHIVE_DEST_2='SERVICE=stitpux LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=stitpux' scope=both sid='*';
alter system set log_archive_format='%t_%s_%r.arc' scope=spfile sid='*';
--设置角色,设置数据来源方和数据接收方
alter system set fal_client='itpuxdb' scope=both sid='*';
alter system set FAL_SERVER='stitpux' scope=both sid='*';
--数据库文件转换和日志文件转换,(远程路径 ,本地路径) 指明本地生成的日志和数据库文件在远程的同步路径
注意下边两个参数,一个是db文件,一个是repo联机重做日志,目前由于建库两个放在了一个目录
alter system set DB_FILE_NAME_CONVERT='/home/oracle/app/oradata/orcl','/home/oracle/app/oradata/orcl' scope=spfile sid='*';
alter system set LOG_FILE_NAME_CONVERT='/home/oracle/app/oradata/orcl','/home/oracle/app/oradata/orcl' scope=spfile sid='*';
alter system set standby_file_management=AUTO scope=both sid='*';
7)关闭并重启数据库
startup force;
show parameter conver 看一下远程参数是否生效
8) 查看静态监听是否生效
sqlplus "sys/oracle@itpuxdb as sysdba"
1) 配监听
从生产复制静态监听lisnter.ora,注意修改其中的实例名。
重启监听后,在备库机上tnsping本机和主库,并在主库上tnsping备库.
scp oracle@10.0.0.220:/home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/tnsnames.ora /home/oracle/app/product/11.2.0/dbhome_1/network/admin
scp oracle@10.0.0.220:/home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora /home/oracle/app/product/11.2.0/dbhome_1/network/admin
2) 配置参数文件
在主库复制参数文件到备库参数文件
导出一个pfile参数文件
(主库)
mkdir /home/oracle/tmpfiles
create pfile='/home/oracle/tmpfiles/standby.pfile' from spfile;
(备库)
mkdir /home/oracle/tmpfiles
scp oracle@10.0.0.220:/home/oracle/tmpfiles/standby.pfile /home/oracle/tmpfiles
注意还要修改其中*.db_unique_name='stitpux'
将这个文件复制到备用机,并修改相应主库信息为备库信息,并修改远程路径参数,删除其余dataguard参数
创建相应的路径
mkdir -p /home/oracle/app/admin/stitpux/adump
mkdir -p /home/oracle/arch
mkdir -p /home/oracle/app/oradata/STITPUX
mkdir -p /home/oracle/app/flash_recovery_area/STITPUX
3) 复制密码文件
scp oracle@10.0.0.220:/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs/\*pw\* .
mv orapwitpuxdb orapwstitpux
4) 启动数据库
sqlplus "sys/oracle@stitpux as sysdba"
连接成功则表示静态监听和密码服务均正常
startup pfile='/home/oracle/tmpfiles/standby.pfile' nomount;
5) 创建 pfile 文件
create spfile from pfile=’/home/oracle/tmpfiles/standby.pfile’;
shutdown immediate;
留下这些
*.audit_file_dest='/home/oracle/app/admin/stitpux/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/home/oracle/app/oradata/STITPUX/control01.ctl','/home/oracle/app/flash_recovery_area/STITPUX/control02.ctl'
*.db_block_size=8192
*.db_create_file_dest=''
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest_size=4294967296
*.db_recovery_file_dest='/home/oracle/arch'
*.db_unique_name='stitpux'
*.diagnostic_dest='/home/oracle/app'
*.job_queue_processes=0
*.memory_target=780140544
*.nls_language='SIMPLIFIED CHINESE'
*.nls_territory='CHINA'
*.open_cursors=300
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.service_names='stitpux'
*.sessions=335
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
6) 重新启动后修改数据库参数
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(stitpux,itpuxdb)' scope=both sid='*';
alter system set LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=stitpux' scope=both sid='*';
server 均为数据库另一端 tns.ora,db_unique_name均为数据库另一端的sid
alter system set LOG_ARCHIVE_DEST_2='SERVICE=itpuxdb LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=itpuxdb' scope=both sid='*';
alter system set log_archive_format='%t_%s_%r.arc' scope=spfile sid='*';
alter system set fal_client='stitpux' scope=both sid='*';
alter system set FAL_SERVER='itpuxdb' scope=both sid='*';
数据库文件转换和日志文件转换,(远程路径 ,本地路径)
指明本地生成的日志和数据库文件在远程的同步路径,注意下边两个参数,一个是db文件,一个是repo联机重做日志,目前由于建库两个放在了一个目录(由于两边一致则不改)
alter system set DB_FILE_NAME_CONVERT='/home/oracle/app/oradata/orcl','/home/oracle/app/oradata/orcl','/home/oracle/ora_dbf','/home/oracle/ora_dbf' scope=spfile sid='*';
alter system set LOG_FILE_NAME_CONVERT='/home/oracle/app/oradata/orcl','/home/oracle/app/oradata/orcl' scope=spfile sid='*';
alter system set standby_file_management=AUTO scope=both sid='*';
1) 备份生产库,拷贝到备库
(主)
rman target / --oracle组件命令,直接在bashshell执行
crosscheck archivelog all;--由于主库我们改了一些数据库信息,需刷新数据库文件列表
backup format '/home/oracle/tmpfiles/itpuxdb_%u_%s_%p'database plus archivelog;
> 切归档
2) 创建standby控制文件到备库
(主)
>切归档;
alter database create standby controlfile as '/home/oracle/tmpfiles/standby.ctl';
(备)
scp oracle@10.0.0.220:/home/oracle/tmpfiles/* /home/oracle/tmpfiles
3) 创建dataguard
(主)
11g:
rman
(X)connect target "sys/oracle@itpuxdb as sysdba" 11G上会报tns错误,只能用下边的方法
(X)connect auxiliary "sys/oracle@stitpux as sysdba"
rman target sys/oracle@itpuxdb auxiliary sys/oracle@stitpux;
(X)duplicate target database for standby; --两边如果目录结构一致需要加一个参
duplicate target database for standby nofilenamecheck;
12c:
先解锁sysbackup/sysdg账户
alter user sysbackup identified by oracle account unlock;
alter user sysdg identified by oracle account unlock;
scp db01:/oracle/app/oracle/product/12.1.0.2/db_1/dbs/orapw* .
mv orapwitpuxdb orapwstitpux
rman
--连接主库
connect target "sysbackup/oracle@itpuxdb as sysbackup"
--连接备库
connect auxiliary "sysbackup/oracle@stipux as sysbackup"
--向备库recover库控制文件
duplicate target database for standby;
4) 检查同步状况
(备)
备库需在此时重启至mount状态
show parameter control;
select * from v$database;
select open_mode,protection_mode,activation#,switchover#,switchover_status from v$database;
如果是RECOVERY NEEDED 则执行下方命令
启动后台数据库恢复
alter database recover managed standby database disconnect from session;
取消恢复(恢复模式下无法open数据库)
alter database recover managed standby database cancel;
alter database open;--在备库执行意味着启动mrp进程
再次查看数据库情况发现备库已进入READ ONLY 归档模式则证明安装成功.
create tablespace test1 datafile '/home/oracle/ora_dbf/testkts.dbf' size 2m autoextend off;
alter system switch logfile;
create user testsp identified by testsp default tablespace test1;
alter system switch logfile;
删除数据
delete from test1.test1_tab;
commit;
删除表
drop table test1.test1_tab;
删除用户
drop user test1 cascade;
删除表空间
drop tablespace test includeing contents and datafile;
状态 | 含义 |
---|---|
NOT ALLOWED | 当前的数据库不是带有备用数据库的主数据库 |
PREPARING DICTIONARY | 该逻辑备用数据库正在向一个主数据库和其他备用数据库发送它的重做数据,以便为切换做准备 |
PREPARING SWITCHOVER | 接受用于切换的重做数据时,逻辑备用配置会使用它 |
RECOVERY NEEDED | 备用数据库还没有接收到切换请求 |
SESSIONS ACTIVE | 在主数据库中存在活动的SQL会话;在继续执行之前必须断开这些会话 |
SWITCHOVER PENDING | 适用于那些已收到主数据库切换请求但是还没有处理该请求的备用数据库 |
SWITCHOVER LATENT | 切换没有完成并返回到主数据库 |
TO LOGICAL STANDBY | 主数据库已经收到了来自逻辑备用数据库的完整的字典 |
TO PRIMARY | 该备用数据库可以转换为主数据库 |
TO STANDBY | 该主数据库可以转换为备用数据库 |
错误:
SQL> select switchover_status from v$database;
SWITCHOVER_STATUS
--------------------
FAILED DESTINATION
该错误是由于主机arch进程无法连接到备机造成
在双方机器上分别tnsping两个tnsname,一般来说,应有一方监听没起,启动后连接失败方应会很快会被arch进程检测到,从而连接成功。