角色 | 操作系统 | IP | 数据库名称 | SID |
---|---|---|---|---|
主库(master) | centos7.5 | 192.168.0.20 | orcl | orcl-primary |
备库(slave) | centos7.5 | 192.168.0.21 | orcl | orcl-standby |
#两台数据库配置域名映射
vim /etc/hosts
192.168.0.20 oracle-primary
192.168.0.21 oracle-standby
#主库配置
#开启强制日志记录
# sqlplus 登录
sqlplus / as sysdba
# 先关闭数据库
SQL> shutdown immediate
# 启动到nomount状态
SQL> startup nomount ;
# 在启动到mount状态;
SQL> alter database mount;
# 配置为强日志记录
#修改数据库为强制记日志,这是必须的操作,主库的每一步操作都得记录到日志中去。
SQL> alter database force logging;
#修改为归档模式
#修改数据库为归档模式,因为dg是通过传送归档日志到备库然后应用来保证主备库一致的
SQL> alter database archivelog;
#查询状态,Enabled为开启状态
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /usr/oracle/oradata/orcl/archivelog
Oldest online log sequence 52
Next log sequence to archive 54
Current log sequence 54
#创建redo日志文件,必须为主库的redo文件数+1
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/usr/oracle/oradata/orcl/redo04.redo') size 200M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/usr/oracle/oradata/orcl/redo05.redo') size 200M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/usr/oracle/oradata/orcl/redo06.redo') size 200M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/usr/oracle/oradata/orcl/redo07.redo') size 200M;
#查询日志文件
SQL> select group#,type,member from v$logfile;
GROUP# TYPE
---------- -------
MEMBER
--------------------------------------------------------------------------------
3 ONLINE
/usr/oracle/oradata/orcl/redo03.log
2 ONLINE
/usr/oracle/oradata/orcl/redo02.log
1 ONLINE
/usr/oracle/oradata/orcl/redo01.log
GROUP# TYPE
---------- -------
MEMBER
--------------------------------------------------------------------------------
4 STANDBY
/usr/oracle/oradata/orcl/redo04.redo
5 STANDBY
/usr/oracle/oradata/orcl/redo05.redo
6 STANDBY
/usr/oracle/oradata/orcl/redo06.redo
GROUP# TYPE
---------- -------
MEMBER
--------------------------------------------------------------------------------
7 STANDBY
/usr/oracle/oradata/orcl/redo07.redo
7 rows selected.
#创建pfile文件
#这里创建pfile是为了做一些主库参数的配置,并且还得拷贝到备库再次修改成备库的配置。只要通过pfile才能在主备库之间建立联系(主库和备库中的pfile相似但有区别)
#pfile文件位于$ORACLE_HOME/dbs目录下,命名格式为init+数据库名.ora
#如数据库名为orcl,则pfile文件就是initorcl.ora文件
SQL> create pfile from spfile;
#创建归档目录
# 在目录/usr/oracle/oradata/orcl 下创建(自定义,不固定)
mkdir /usr/oracle/oradata/orcl/archivelog -p
#修改pfile文件
[oracle@oracle1 dbs]$ pwd
/usr/oracle/product/12c/db_1/dbs
[oracle@oracle1 dbs]$ vim initorcl.ora
orcl.__data_transfer_cache_size=0
orcl.__db_cache_size=4043309056
orcl.__inmemory_ext_roarea=0
orcl.__inmemory_ext_rwarea=0
orcl.__java_pool_size=16777216
orcl.__large_pool_size=83886080
orcl.__oracle_base='/usr/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=1677721600
orcl.__sga_target=4999610368
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=838860800
orcl.__streams_pool_size=0
*.audit_file_dest='/usr/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='12.2.0'
*.control_files='/usr/oracle/oradata/orcl/control01.ctl','/usr/oracle/oradata/orcl/control02.ctl'
*.db_block_size=8192
*.db_name='orcl'
*.DB_UNIQUE_NAME='orcl_primary'
*.diagnostic_dest='/usr/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.FAL_CLIENT='orcl_primary'
*.FAL_SERVER='orcl_standby'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl_primary,orcl_standby)'
#LOCATION为主库的归档地址
*.LOG_ARCHIVE_DEST_1='LOCATION=/usr/oracle/oradata/orcl/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl_primary'
#SERVICE为备库的tnsname
*.LOG_ARCHIVE_DEST_2='SERVICE=orcl_standby ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl_standby'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.LOG_ARCHIVE_FORMAT='%t_%s_%r.arc'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=1587m
*.processes=640
*.REMOTE_LOGIN_PASSWORDFILE='EXCLUSIVE'
*.sga_target=4760m
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.undo_tablespace='UNDOTBS1'
~
#主库
mkdir /usr/oracle/oradata/orcl/backup
#将pfile文件和密码文件orapworcl(命名规则为orapw+数据库名)发送到备库的相同目录
[oracle@oracle dbs]$ scp ./initorcl.ora oracle-standby:/usr/oracle/product/12c/db_1/dbs/
[oracle@oracle dbs]$ scp ./orapworcl oracle-standby:/usr/oracle/product/12c/db_1/dbs/
#根据修改的pfile文件启动数据库(在主库操作)
[oracle@oracle-tmp dbs]$ sqlplus / as sysdba
SQL> shutdown immediate
SQL> startup nomount pfile='/usr/oracle/product/12c/db_1/dbs/initorcl.ora';
SQL> create spfile from pfile;
SQL> alter database mount;
SQL> alter database open;
#配置主库的listener.ora文件和tnsnames.ora文件
[oracle@oracle1 dbs]$ cd $ORACLE_HOME/network/admin
[oracle@oracle1 admin]$ pwd
/usr/oracle/product/12c/db_1/network/admin
[oracle@oracle1 admin]$ ls
listener.ora samples shrept.lst sqlnet.ora tnsnames.ora
[oracle@oracle1 admin]$ vim listener.ora
# listener.ora Network Configuration File: /usr/oracle/product/12c/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-primary)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /usr/oracle/product/12c/db_1)
(SID_NAME = orcl-primary)
)
)
ADR_BASE_LISTENER = /usr/oracle
[oracle@oracle1 admin]$ vim tnsnames.ora
# tnsnames.ora Network Configuration File: /usr/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-primary)(PORT = 1521))
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-primary)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_STANDBY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.21)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
ORCL_PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.20)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
#重启监听
# 监听配置好之后重启监听
lsnrctl stop
lsnrctl start
#备库配置
#修改从主库发送过来的pfile文件
cd $ORACLE_HOME/dbs
vim initorcl.ora
orcl.__data_transfer_cache_size=0
orcl.__db_cache_size=4043309056
orcl.__inmemory_ext_roarea=0
orcl.__inmemory_ext_rwarea=0
orcl.__java_pool_size=16777216
orcl.__large_pool_size=83886080
orcl.__oracle_base='/usr/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=1677721600
orcl.__sga_target=4999610368
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=838860800
orcl.__streams_pool_size=0
*.audit_file_dest='/usr/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='12.2.0'
*.control_files='/usr/oracle/oradata/orcl/control01.ctl','/usr/oracle/oradata/orcl/control02.ctl'
*.db_block_size=8192
*.db_name='orcl'
*.diagnostic_dest='/usr/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=1587m
*.processes=640
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=4760m
*.undo_tablespace='UNDOTBS1'
*.log_file_name_convert='/usr/oracle/oradata/orcl','/usr/oracle/oradata/orcl'
*.db_file_name_convert='/usr/oracle/oradata/orcl','/usr/oracle/oradata/orcl'
DB_UNIQUE_NAME=orcl_standby
LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl_primary,orcl_standby)'
LOG_ARCHIVE_DEST_1='LOCATION=/usr/oracle/oradata/orcl/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl_standby'
LOG_ARCHIVE_DEST_2='SERVICE=orcl_primary ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl_primary'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
FAL_SERVER=orcl_primary
STANDBY_FILE_MANAGEMENT=AUTO
#备库
mkdir /usr/oracle/oradata/orcl/backup
#启动备库到nomount状态
sqlp / as sysdba
SQL> shutdown immediate;
SQL> startup nomount pfile="/usr/oracle/product/12c/db_1/dbs/initorcl.ora";
#配置备库的listener.ora文件和tnsnames.ora文件
#配置监听
vim listener.ora
# listener.ora Network Configuration File: /usr/oracle/product/12c/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-standby)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /usr/oracle/product/12c/db_1)
(SID_NAME = orcl-standby)
)
)
ADR_BASE_LISTENER = /usr/oracle
#配置TNS
vim tnsnames.ora
# tnsnames.ora Network Configuration File: /usr/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-standby)(PORT = 1521))
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle-standby)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_STANDBY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.21)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
ORCL_PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.20)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
#使用rman链接主库和备库
[oracle@oracle-1 dbs]$ rman target sys/Oracle123@orcl_primary auxiliary sys/Oracle123@orcl_standby
# 开始同步
RMAN> duplicate target database for standby from active database nofilenamecheck;
RMAN> exit;
#备库开启apply service
[oracle@oracle-1 dbs] sqlplus / as sysdba
# 打开数据库
SQL> alter database open;
#检查数据库状态
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY
SQL> alter database recover managed standby database disconnect;
SQL> alter database open;
#检查数据库状态
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY WITH APPLY
#启停顺序
启动:
先备库,后主库
关闭:
先主库,后备库
#ADG主备切换
#12c新语法:
#主库执行,备库不需要再执行命令:
#先验证切换是否有问题
alter database switchover to sbdb verify;
#切换
alter database switchover to sbdb;
#切换之后主备都要重启数据库
#主库
startup
#备库
startup nomount
alter database recover managed standby database disconnect;
查询数据库切换状态
SQL> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ---------------- --------------------
ORCL PRIMARY TO STANDBY