参考文档下载链接:链接: https://pan.baidu.com/s/1gcSw0qsb8sI4XY84ZGV5Xg 提取码: sn1p
前提:本文档基于已经创建了oracle数据库,两台服务器网络可以互相访问,
|
主库 |
备库 |
OS系统 |
Centos 7.5 x64 |
Centos 7.5 x64 |
数据库版本 |
12.2.0.1 |
12.2.0.1 |
IP |
192.168.33.107 |
192.168.33.108 |
实例名 |
fxdb |
sbdb |
静态监听 |
1521-动态监听 |
1522-静态监听 |
主库:
Database软件,dbca安装数据库,监听
备库:
Database软件,无需dbca安装数据库,静态监听
将这两ip配置上
alter database force logging;
select force_logging from v$database;(查询状态是否为YES)
alter system set log_archive_format='%t_%s_%r.arc' scope=spfile;
alter system set log_archive_dest_1='LOCATION=/u01/arch' scope=spfile;
sql> shutdown immediate;
sql> startup mount;
sql> alter database archivelog;
sql> alter database open;
退出来创建/u01/arch路径
alter system set log_archive_config='DG_CONFIG=(fxdb,sbdb)' scope=both; 是指的DB_UNIQUE_NAME值
alter system set log_archive_dest_1='LOCATION=/u01/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=fxdb' scope=both;
alter system set log_archive_dest_2='SERVICE=tnssbdb LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=sbdb' scope=both;
alter system set standby_file_management='AUTO' scope=both;
alter system set fal_client='tnsfxdb' scope=both; 是指tnsname
alter system set fal_server='tnssbdb' scope=both;
编辑tnsnames.ora文件,一般这个文件都是在这个路径下面的 $ORACLE_HOME/network/admin
LISTENER_FXDB =
(ADDRESS = (PROTOCOL = TCP)(HOST = fanxin01)(PORT = 1521))
FXDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = fanxin01)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = fxdb)
)
)
TNSFXDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = fanxin01)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = fxdb)
)
)
TNSSBDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = fanxin02)(PORT = 1522))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = fxdb)
)
)
主库配置完成
将这两ip配置上
主库和备库,SYS密码必须相同
命名规则:orapwSID
scp orapwfxdb fanxin02:/u01/app/oracle/product/12.2.0/db_1/dbs
mv orapwfxdb orapwsbdb
scp tnsnames.ora
fanxin02:/u01/app/oracle/product/12.2.0/db_1/network/admin/tnsnames.ora
配置如下
scp tnsnames.ora
fanxin02:/u01/app/oracle/product/12.2.0/db_1/network/admin/tnsnames.ora
配置如下
在$ORACLE_HOME/db_1/dbs目录下
touch initsbdb.ora
编辑这个文件的内容
db_name='fxdb'
sga_target=1520M
pga_aggregate_target=579M
db_unique_name='sbdb'
compatible='12.2.0' 配置为和主库一致
log_archive_config='DG_CONFIG=(fxdb,sbdb)'
audit_file_dest='/u01/app/oracle/admin/sbdb/adump'
control_files='/u01/app/oracle/oradata/sbdb/control01.ctl','/u01/app/oracle/oradata/sbdb/control02.ctl'
log_archive_dest_1='LOCATION=/u01/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=sbdb'
log_archive_dest_2='SERVICE=tnsfxdb LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=fxdb'
db_file_name_convert='/u01/app/oracle/oradata/fxdb','/u01/app/oracle/oradata/sbdb'
log_file_name_convert='/u01/app/oracle/oradata/fxdb','/u01/app/oracle/oradata/sbdb'
standby_file_management='AUTO'
remote_login_passwordfile='EXCLUSIVE'
fal_client='tnssbdb'
fal_server='tnsfxdb'
创建目录:
mkdir -p /u01/app/oracle/admin/sbdb/adump
mkdir -p /u01/app/oracle/oradata/sbdb
mkdir -p /u01/arch
登录oracle执行下面这条命令
create spfile from pfile;
可以看到对应生成了下面这个文件
登录oracle,启动nomount状态
startup nomount;
备库配置完成
早主备库中都执行下面四个命令,测试是否成功
tnsping tnsfxdb
tnsping tnssbdb
sqlplus sys/Oracle123@tnsfxdb as sysdba
sqlplus sys/Oracle123@tnssbdb as sysdba
以上如果都没问题,能够联的上对应oracle,才可以进行下面操作
rman备份集,备库此时处于nomount状态 --连接rman
rman target sys/123456789Wuw@tnsfxdb auxiliary sys/123456789Wuw@tnssbdb
--从主库创建备库
duplicate target database for standby from active database nofilenamecheck;
如果失败:
备库:
shutdown->startup nomount
数据文件都删除 --删除/u01/app/oracle/oradata/sbdb 路径的下的所有文件,然后在执行上面的操作
在备库中登录Oracle,添加Standby日志组并开启同步,Standby日志组=日志组+1
alter database add standby logfile group 4 ('/u01/app/oracle/oradata/sbdb/stredo04.rdo') size 200m;
alter database add standby logfile group 5 ('/u01/app/oracle/oradata/sbdb/stredo05.rdo') size 200m;
alter database add standby logfile group 6 ('/u01/app/oracle/oradata/sbdb/stredo06.rdo') size 200m;
alter database add standby logfile group 7 ('/u01/app/oracle/oradata/sbdb/stredo07.rdo') size 200m;
如果失败了,删除对应文件
alter database drop standby logfile group 4;
alter database drop standby logfile group 5;
alter database drop standby logfile group 6;
alter database drop standby logfile group 7;
打开数据库实例:
alter database open
--开启实时同步
alter database recover managed standby database disconnect;
--更改保护模式为最大可用
alter database set standby database to maximize availability;
查看状态:
select name,open_mode,database_role,protection_mode,protection_level from v$database;
主从同步开启完成,
PS:如果出现服务器宕机,停电等特殊情况,导致Oracle服务器中断了,需要重新启动oracle,这个时候oracle主从同步也会失效,需要重新开启同步,需要按照以下规则来启动
PS:注意,以下操作都是在切换到Oracle用户下进行的
操作1:启动备库
操作2:启动主库
1.启动oracle实例
2.启动oracle监听
操作3:在备库上重新开启主从同步
开启完成之后就可以自己测试一下了,
这边我测试了一种情况,就是主从同步未开启的情况,往主库的表中插入数据,这个时候是不能同步到备库中了,但是开启了主从同步之后,主库会自动将原先未开启主从同步之前的数据也一起同步到备库中去