系统环境:centos 6.x
数据库版本:11.2.0.3
主从IP:192.168.1.100(主库)
192.168.1.101(备库)
搭建方式:冷备份方式(这种方式需要停机,所以一般我们都选择rman 来搭建物理备库)
一:主库操作:
1.设置为归档模式
SQL> archive log list;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> archive log list;
SQL> alter database open;
2. Primary 设置force logging
SQL> alter database force logging;
SQL> select force_logging from v$database;
FORCE_LOG
---------
YES
3.配置监听并启动监听(listener.ora文件):
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ora)
(ORACLE_HOME = /opt/oracle/product/11.2.0/db_64)
(SID_NAME = ora)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /opt/oracle
配置静态注册(tnsnames.ora文件)
PRIM=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ora)
)
)
STDBY=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ora)
)
)
启动监听:
prim@oracle /home/oracle$ lsnrctl start
二:备库操作:
配置监听并启动监听(listener.ora文件):
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ora)
(ORACLE_HOME = /opt/oracle/product/11.2.0/db_64)
(SID_NAME = ora)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /opt/oracle
配置静态注册(tnsnames.ora文件)
PRIM=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ora)
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ora)
)
)
启动监听:
stdby@oracle /home/oracle$ lsnrctl start
三:主库操作,配置dataguard 参数:
创建PFIEL文件:
SQL>create pfile='/home/oracle/init.ora' from spfile;
编辑init.ora文件,添加主库所需参数:
*.db_unique_name='prim'
*.log_archive_config='dg_config=(prim,stdby)'
*.log_archive_dest_1='location=/opt/oracle/oradata/ora/archive valid_for=(all_logfiles,all_roles) db_unique_name=prim'
*.log_archive_dest_2='service=stdby reopen=60 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=stdby'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.standby_file_management='auto'
*.fal_server='stdby'
*.fal_client='prim'
*.db_file_name_convert='/opt/oracle/oradata/stdby','/opt/oracle/oradata/ora'
*.log_file_name_convert='/opt/oracle/oradata/stdby','/opt/oracle/oradata/ora'
这里因为主库和备库的数据目录不一致,所以需要配置db_file_name_convert和log_file_name_convert参数,如果主库和备库目录都一致可以不用配置。
重启数据库到mount状态:
SQL>shutdown immediate
SQL>startup mount
创建standby 控制文件:
SQL>alter database create standby control file as '/home/oracle/control01.ctl’;
将主库的参数文件,密码文件,standby控制文件,日志文件和数据文件SCP到备库:
scp参数文件和控制文件:
prim@oracle /home/oracle$ scp init.ora control01.ctl oracle@192.168.1.101:/home/oracle
scp密码文件:
prim@oracle /opt/oracle/product/11.2.0/db_64/dbs$ scp orapwora oracle@192.168.1.101:/opt/oracle/product/11.2.0/db_64/dbs
scp日志文件和数据文件:
prim@oracle /opt/oracle/oradata/ora$ scp *.dbf *.log oracle@192.168.1.101/opt/oracle/oradata/stdby
四:备库操作:
编辑pfile参数文件init.ora(这个文件是从主库scp过来的,所以注意修改对应参数就好了),添加备库所需参数:
*.control_files='/opt/oracle/oradata/stdby/control01.ctl', '/opt/oracle/oradata/stdby/control02.ctl'
*.db_unique_name='stdby'
*.log_archive_config='dg_config=(stdby,prim)'
*.log_archive_dest_1='location=/opt/oracle/oradata/stdby/archive valid_for=(all_logfiles,all_roles) db_unique_name=stdby'
*.log_archive_dest_2='service=prim reopen=60 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=prim'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.standby_file_management='auto'
*.fal_server='prim'
*.fal_client='stdby'
*.db_file_name_convert='/opt/oracle/oradata/ora','/opt/oracle/oradata/stdby'
*.log_file_name_convert='/opt/oracle/oradata/ora','/opt/oracle/oradata/stdby'
mv 控制文件到数据目录下,并cp一份控制文件:
stdby@oracle /home/oracle$ mv control01.ctl /opt/oracle/oradata/stdby/
stdby@oracle /opt/oracle/oradata/stdby$ cp control01.ctl control02.ctl
检查主从网络是否正常(主库和备库都需要执行):
stdby@oracle /home/oracle$tnsping prim
stdby@oracle /home/oracle$tnsping stdby
stdby@oracle /home/oracle$sqlplus sys/oracle@prim as sysdba
stdby@oracle /home/oracle$sqlplus sys/oracle@stdby as sysdba
检查数据文件,日志文件,控制文件,密码文件是否在备库:
stdby@oracle /opt/oracle/oradata/stdby$ ll -lth *.dbf *.log *.ctl
-rw-r----- 1 oracle oinstall 9.3M Dec 2 10:41 control02.ctl
-rw-r----- 1 oracle oinstall 9.3M Dec 2 10:41 control01.ctl
-rw-r----- 1 oracle oinstall 5.1M Dec 2 09:01 users01.dbf
-rw-r----- 1 oracle oinstall 131M Dec 2 09:01 undotbs01.dbf
-rw-r----- 1 oracle oinstall 541M Dec 2 09:01 sysaux01.dbf
-rw-r----- 1 oracle oinstall 721M Dec 2 09:01 system01.dbf
-rw-r----- 1 oracle oinstall 51M Dec 1 23:18 redo03.log
-rw-r----- 1 oracle oinstall 51M Dec 1 23:18 redo02.log
-rw-r----- 1 oracle oinstall 51M Dec 1 23:18 redo01.log
-rw-r----- 1 oracle oinstall 30M Dec 1 22:48 temp01.dbf
stdby@oracle /opt/oracle/product/11.2.0/db_64/dbs$ ll -lt orapwora
-rw-r----- 1 oracle oinstall 1536 Dec 1 22:29 orapwora
进入SQLPLUS下启动数据库:
SQL>create spfile from pfile='/home/oracle/init.ora';
SQL> startup mount
检查数据库状态:
SQL> select database_role,open_mode from v$database;
DATABASE_ROLE OPEN_MODE
-------------------------------- ----------------------------------------
PHYSICAL STANDBY MOUNTED
五:在主从数据库上添加standby redo log,主要为了数据库切换使用
主库上添加:
SQL> alter database add standby logfile group 6 '/opt/oracle/oradata/ora/st_redo06.log' size 50M;
SQL> alter database add standby logfile group 7 '/opt/oracle/oradata/ora/st_redo07.log' size 50M;
SQL> alter database add standby logfile group 8 '/opt/oracle/oradata/ora/st_redo08.log' size 50M;
备库上添加:
SQL> alter database add standby logfile group 6 '/opt/oracle/oradata/stdby/st_redo06.log' size 50M;
SQL> alter database add standby logfile group 7 '/opt/oracle/oradata/stdby/st_redo07.log' size 50M;
SQL> alter database add standby logfile group 8 '/opt/oracle/oradata/stdby/st_redo08.log' size 50M;
这里需要注意standby 日志组必须等于redo log组或者多一组.
六:打开主库到“OPEN”状态和启动备库日志应用
主库:
SQL>alter database open;
SQL> select database_role,open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PRIMARY READ WRITE
备库:
SQL> recover managed standby database disconnect from session;
检查备库日志应用情况:
SQL> select sequence#,applied from v$archived_log;
SEQUENCE# APPLIED
---------- ------------------
9 YES
8 YES
7 YES
10 YES
11 YES
12 YES
6 rows selected.
OK,一套物理DATAGUARD环境搭建完毕。