--------------------------Oracle Data Guard----------------------
环境:
Primary
ech0 192.168.1.100
Standby
ech0192.168.1.101
Syandby节点
――创建相关目录
[oracle@standby ~]$ mkdir -p /oracle/oradata/standby/
[oracle@standby ~]$ mkdir -p /oracle/admin/standby/adump/
[oracle@standby ~]$ mkdir -p /oracle/admin/standby/dpdump/
[oracle@standby ~]$ mkdir -p /oracle/archive/
[oracle@standby ~]$ mkdir -p /oracle/flashback/
Primary节点
[oracle@primary ~]$ sqlplus / as sysdba
――查看数据库为force logging状态
SQL> select force_logging from v$database;
――强制修改数据库为force logging状态
SQL> alter database force logging;
――为primary库增加standby redo log
1)查询primary库当前日志组的数量
SQL> select group#,member from v$logfile;
2)查询primary库当前日志组成员文件大小
SQL> select group#,bytes/1024/1024 from v$log;
3)创建standby redo log
SQL> alter database add standby logfile
group 4 '/oracle/oradata/primary/std_redo04.log' size 50M;
SQL> alter database add standby logfile
group 5 '/oracle/oradata/primary/std_redo05.log' size 50M;
SQL> alter database add standby logfile
group 6 '/oracle/oradata/primary/std_redo06.log' size 50M;
SQL> alter database add standby logfile
group 7 '/oracle/oradata/primary/std_redo07.log' size 50M;
4)查询创建结果
SQL> select group#,member from v$logfile where type='STANDBY';
――修改主库参数
1)创建一个pfile文件
SQL> create pfile from spfile;
2)修改pfile初始化参数 [在最下面添加]
[oracle@primary ~]$ vi /oracle/db/dbs/initprimary.ora
db_unique_name=primary
LOG_ARCHIVE_CONFIG='DG_CONFIG=(primary,standby)'
LOG_ARCHIVE_DEST_1='LOCATION=/oracle/archive/
VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=primary'
LOG_ARCHIVE_DEST_2='SERVICE=standby LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
FAL_SERVER=standby
FAL_CLIENT=primary
DB_FILE_NAME_CONVERT='/oracle/oradata/standby/','/oracle/oradata/primary/'
LOG_FILE_NAME_CONVERT='/oracle/oradata/standby/','/oracle/oradata/primary/'
STANDBY_FILE_MANAGEMENT=AUTO
――修改监听
[oracle@primary ~]$ cd /oracle/db/network/admin/
[oracle@primary admin]$ vi listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = primary)(PORT = 1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = primary)
(SID_NAME = primary)
)
(SID_DESC =
(GLOBAL_DBNAME = standby)
(SID_NAME = standby)
)
)
ADR_BASE_LISTENER = /oracle
[oracle@primary admin]$ vi tnsnames.ora
PRIMARY =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = primary)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = primary)
)
)
STANDBY =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = standby)
)
)
――重新启动监听
[oracle@standby ~]$ lsnrctl stop
[oracle@standby ~]$ lsnrctl start
――创建控制文件controlfile
SQL> alter database create standby
controlfile as '/oracle/oradata/primary/std_control01.ctl';
――关闭数据库
SQL> shutdown immeidate;
――复制数据文件到standby节点
[oracle@primary ~]$ cd /oracle/oradata/primary/
[oracle@primary primary]$ scp *.dbf
[email protected]:/oracle/oradata/standby/
――复制日志文件到standby节点
[oracle@primary primary]$ scp *.log [email protected]:/oracle/oradata/standby/
――复制控制文件到standby节点
[oracle@primary primary]$ scp std_control01.ctl
[email protected]:/oracle/oradata/standby/
――复制初始化文件、密码文件到standby节点
[oracle@primary primary]$ cd /oracle/db/dbs/
[oracle@primary dbs]$ scp initprimary.ora orapwprimary
[email protected]:/oracle/db/dbs/
――复制监听文件到standby节点
[oracle@primary dbs]$ cd ../network/admin/
[oracle@primary admin]$ scp listener.ora tnsnames.ora [email protected]:/oracle/db/network/admin/
――启动primary数据库
SQL> startup pfile='/oracle/db/dbs/initprimary.ora';
――创建spfile
SQL> create spfile from pfile;
Standby节点
――修改监听
[oracle@standby ~]$ vi /oracle/db/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = primary)
(SID_NAME = primary)
)
(SID_DESC =
(GLOBAL_DBNAME = standby)
(SID_NAME = standby)
)
)
ADR_BASE_LISTENER = /oracle
――启动监听
[oracle@standby ~]$ lsnrctl start
――测试监听 [parmary、standby节点都执行]
[oracle@standby ~]$ tnsping standby
[oracle@standby ~]$ tnsping primary
――复制控制文件
[oracle@standby dbs]$ cd /oracle/oradata/standby/
[oracle@standby standby]$ cp std_control01.ctl std_control02.ctl
[oracle@standby standby]$ cp std_control01.ctl std_control03.ctl
――编辑初始化文件
[oracle@standby standby]$ cd /oracle/db/dbs/
[oracle@standby dbs]$ mv initprimary.ora initstandby.ora
[oracle@standby dbs]$ mv orapwprimary orapwstandby
[oracle@standby dbs]$ vi initstandby.ora
*.audit_file_dest='/oracle/admin/standby/adump'
*.control_files='/oracle/oradata/standby/std_control01.ctl',
'/oracle/oradata/standby/std_control02.ctl',
'/oracle/oradata/standby/std_control03.ctl'
最下面添加
db_unique_name=standby
LOG_ARCHIVE_CONFIG='DG_CONFIG=(primary,standby)'
DB_FILE_NAME_CONVERT='/oracle/oradata/primary/','/oracle/oradata/standby/'
LOG_FILE_NAME_CONVERT='/oracle/oradata/primary/','/oracle/oradata/standby/'
LOG_ARCHIVE_FORMAT=log%t_%s_%r.arc
LOG_ARCHIVE_DEST_1='LOCATION=/oracle/archive/
VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=syandby'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_2='SERVICE=primary LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary'
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
FAL_SERVER=primary
FAL_CLIENT=standby
STANDBY_FILE_MANAGEMENT=AUTO
――启动standby备库
SQL> startup mount
――启动redo apply
SQL> alter database recover managed standby database disconnect from
session;
――测试1
1)在primary库查看当前最大日志序列号
SQL> select max(sequence#) from v$archived_log;
2)在primary库切换日志
SQL> alter system switch logfile;
3)在primary库再次查看当前最大日志序列号
SQL> select max(sequence#) from v$archived_log;
3)在standby库查看是否同步
SQL> select max(sequence#) from v$archived_log;
――测试2
1)在primary库创建一个表空间、查看syandby库是否同步
SQL> create tablespace test datafile '/oracle/oradata/primary/test01.dbf' size 10M;
2)在primary库检查是否创建成功
SQL> select name from v$datafile;
3)在standby库上检查是否同步 [显示同步失败]
SQL> select name from v$datafile;
4)在primary库切换日志、促发日志传输
SQL> alter system switch logfile;
5)在standby库再次检查是否同步
SQL> select name from v$datafile;
――物理standby的switchover
1)在primary库查看是否支持switchover[TO STANDBY为支持]
SQL> select database_role,switchover_status from v$database;
2)切换primary库为standby库
SQL> alter database commit to switchover to physical standby withsession shutdown;
3)重新启动primary库为mount状态
SQL> shutdown immediate
SQL> startup mount
4)启动redo apply
SQL>alter database recover managed standby database disconnect from session;
5)在standby库查看是否支持switchover[TO PRIMARY为支持]
SQL> select database_role,switchover_status from v$database;
6)切换standby库为primary库
SQL> alter database commit to switchover to primary;
7)打开新的primary库
SQL> alter database open;
8)查看是否切换成功
SQL> select database_role,switchover_status from v$database;
9)检查是否同步
-在新的primary库看当前最大日志序列号
SQL> select max(sequence#) from v$archived_log;
-在新standby库查看是否同步
SQL> select max(sequence#) from v$archived_log;