目录
centos安装Oracle12c
windos远程连接Oracle
数据库备份
创建定时备份任务
ADG搭建(主从复制)
创建pfile文件
创建归档目录
修改pfile文件
3.RMAN复制数据
OGG搭建
ADG和OGG的搭配使用
DG
RAC
MAA (DG+RAC)
1.系统环境
1.1 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1.2 关闭selinux
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
1.3 配置内核参数
#添加以下内容
vim /etc/sysctl.conf
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
kernel.shmall = 2097152
kernel.shmmax = 2147483648
net.ipv4.ip_local_port_range = 9000 65500
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.rp_filter = 1
net.core.rmem_default = 262144
net.core.rmem_max= 4194304
net.core.wmem_default= 262144
net.core.wmem_max= 1048576
# 启用新的配置
sysctl -p
1.4 缓存swap检查以及配置
free -h
2.编译环境
yum install binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libXext libXtst libX11 libXau libxcb libXi make sysstat net-tools telnet java unzip -y
3.用户权限
3.1 创建2个 oracle用户组
groupadd oinstall
groupadd dba
3.2 创建oracle用户加入两个用户组
useradd -g oinstall -G dba oracle
passwd oracle
3.3 修改oracle用户的安全性能设置
vim /etc/security/limits.conf
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
3.4 设置oracle用户的环境变量
vim /home/oracle/.bash_profile
export PATH
export ORACLE_BASE=/usr/oracle
export ORACLE_HOME=$ORACLE_BASE/product/12c/db_1
export ORACLE_SID=orcl
export ORACLE_UNQNAME=orcl
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export LANG=en_US
export TNS_ADMIN=/usr/oracle/product/12c/db_1/network/admin
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
# 使其生效
source /home/oracle/.bash_profile
3.5 创建oracle安装目录
mkdir -p /usr/oracle
chown -R oracle:oinstall /usr/oracle
chmod -R 775 /usr/oracle
4.下载安装Oracle12c
下载地址 https://www.oracle.com/database/technologies/oracle-database-software-downloads.html
# 解压后在./database 目录下
unzip linuxx64_12201_database.zip -d /opt/.
# 更改当前oracle安装文件所在路径用户组和权限
chown -R oracle:oinstall /opt/database
chmod -R 755 /opt/database
4.1 静默安装Orcale
su - oracle
vim /opt/database/response/db_install.rsp
####################################################################
# 为下面列出的变量指定值以进行自定义
####################################################################
# 不要更改以下系统生成的值
oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v12.2.0
oracle.install.option=
# 指定要为清单目录设置的Unix组
UNIX_GROUP_NAME=oinstall
# 指定存放库存文件的位置
INVENTORY_LOCATION=/usr/oracle
# 指定Oracle主页的完整路径。
ORACLE_HOME=/usr/oracle/product/12c/db_1
# 指定Oracle Base的完整路径。
ORACLE_BASE=/usr/oracle
oracle.install.db.InstallEdition=
###############################################################################
# 特权操作系统组
###############################################################################
# OSDBA_GROUP是要授予SYSDBA权限的操作系统组。
oracle.install.db.OSDBA_GROUP=dba
# OSOPER_GROUP是要授予SYSOPER权限的操作系统组。
oracle.install.db.OSOPER_GROUP=oinstall
# OSBACKUPDBA_GROUP是要授予SYSBACKUP权限的操作系统组。
oracle.install.db.OSBACKUPDBA_GROUP=oinstall
# OSDGDBA_GROUP是要授予SYSDG权限的操作系统组。
oracle.install.db.OSDGDBA_GROUP=oinstall
# OSKMDBA_GROUP是要授予SYSKM权限的操作系统组。
oracle.install.db.OSKMDBA_GROUP=oinstall
# OSRACDBA_GROUP是要授予SYSRAC权限的操作系统组。
oracle.install.db.OSRACDBA_GROUP=oinstall
###############################################################################
#网格选项
###############################################################################
oracle.install.db.rac.configurationType=
oracle.install.db.CLUSTER_NODES=
oracle.install.db.isRACOneInstall=
oracle.install.db.racOneServiceName=
oracle.install.db.rac.serverpoolName=
oracle.install.db.rac.serverpoolCardinality=
###############################################################################
# 数据库配置选项
###############################################################################
oracle.install.db.config.starterdb.type=
# 指定Starter数据库全局数据库名称
oracle.install.db.config.starterdb.globalDBName=orcl
# 指定Starter数据库SID。
oracle.install.db.config.starterdb.SID=orcl
oracle.install.db.ConfigureAsContainerDB=
oracle.install.db.config.PDBName=
oracle.install.db.config.starterdb.characterSet=
oracle.install.db.config.starterdb.memoryOption=
# 指定数据库的总内存分配。值(MB)应至少为256 MB,并且不应超过可用的物理内存总量
oracle.install.db.config.starterdb.memoryLimit=800
oracle.install.db.config.starterdb.installExampleSchemas=
###############################################################################
# 可以为中的以下四个模式提供密码
###############################################################################
# 这个变量保存要用于启动数据库中所有模式的密码
oracle.install.db.config.starterdb.password.ALL=Oracle123
oracle.install.db.config.starterdb.password.SYS=
oracle.install.db.config.starterdb.password.SYSTEM=
oracle.install.db.config.starterdb.password.DBSNMP=
oracle.install.db.config.starterdb.password.PDBADMIN=
oracle.install.db.config.starterdb.managementOption=
oracle.install.db.config.starterdb.omsHost=
oracle.install.db.config.starterdb.omsPort=
oracle.install.db.config.starterdb.emAdminUser=
oracle.install.db.config.starterdb.emAdminPassword=
###############################################################################
# 指定恢复选项
###############################################################################
oracle.install.db.config.starterdb.enableRecovery=
# 指定要用于数据库的存储类型
oracle.install.db.config.starterdb.storageType=FILE_SYSTEM_STORAGE
# 指定数据库文件位置,该位置是数据文件、控制文件和重做日志的目录。
oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=/usr/oracle/data
# 指定恢复位置
oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=/usr/oracle/recovery
oracle.install.db.config.asm.diskGroup=
oracle.install.db.config.asm.ASMSNMPPassword=
MYORACLESUPPORT_USERNAME=
MYORACLESUPPORT_PASSWORD=
SECURITY_UPDATES_VIA_MYORACLESUPPORT=
DECLINE_SECURITY_UPDATES=
PROXY_HOST=
PROXY_PORT=
PROXY_USER=
PROXY_PWD=
COLLECTOR_SUPPORTHUB_URL=
# 如果有报错会在显示出来请仔细查看
[oracle@db02server ~]$ /opt/database/runInstaller -silent -ignoreSysPrereqs -ignorePrereq -responseFile /opt/database/response/db_install.rsp
Starting Oracle Universal Installer...
Checking Temp space: must be greater than 500 MB. Actual 18852 MB Passed
Checking swap space: must be greater than 150 MB. Actual 2047 MB Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2023-03-16_09-42-36PM. Please wait ...[oracle@db02server ~]$ You can find the log of this install session at:
/usr/oracle/logs/installActions2023-03-16_09-42-36PM.log
The installation of Oracle Database 12c was successful.
Please check '/usr/oracle/logs/silentInstall2023-03-16_09-42-36PM.log' for more details.
As a root user, execute the following script(s):
1. /usr/oracle/orainstRoot.sh
2. /usr/oracle/product/12c/db_1/root.sh
Successfully Setup Software.
As install user, execute the following command to complete the configuration.
/opt/database/runInstaller -executeConfigTools -responseFile /opt/database/response/db_install_210.rsp [-silent]
# 正常情况下安装完毕会提示用root用户运行两个shell脚本
sh /usr/oracle/orainstRoot.sh
sh /usr/oracle/product/12c/db_1/root.sh
4.2 静默安装数据库
vim /opt/database/response/dbca.rsp
##############################################################################
# DBCA响应文件
##############################################################################
# 不要更改以下系统生成的值
responseFileVersion=/oracle/assistants/rspfmt_dbca_response_schema_v12.2.0
# 数据库的全局数据库名称
gdbName=orcl
# 数据库的系统标识符(SID)
sid=orcl
databaseConfigType=
RACOneNodeServiceName=
policyManaged=
createServerPool=
serverPoolName=
cardinality=
force=
pqPoolName=
pqCardinality=
createAsContainerDatabase=
numberOfPDBs=
pdbName=
useLocalUndoForPDBs=
pdbAdminPassword=
nodelist=
# 模板的名称
templateName=General_Purpose.dbc
# SYS用户的密码
sysPassword=Oracle123
# SYSTEM用户的密码
systemPassword=Oracle123
# Windows服务用户的密码
oracleHomeUserPassword=Oracle123
emConfiguration=
# Enterprise Manager配置类型
emExpressPort=5500
runCVUChecks=
dbsnmpPassword=
omsHost=
omsPort=
emUser=
emPassword=
dvConfiguration=
dvUserName=
dvUserPassword=
dvAccountManagerName=
dvAccountManagerPassword=
olsConfiguration=
datafileJarLocation=
datafileDestination=
recoveryAreaDestination=
storageType=
diskGroupName=
asmsnmpPassword=
recoveryGroupName=
characterSet=
nationalCharacterSet=
registerWithDirService=
dirServiceUserName=
dirServicePassword=
walletPassword=
listeners=
variablesFile=
variables=
initParams=
# 指定是否将示例架构添加到数据库
sampleSchema=true
memoryPercentage=
databaseType=
automaticMemoryManagement=
totalMemory=
# 运行下面命令创建数据库,有会创建进度显示
[oracle@db02server ~]$ dbca -silent -createDatabase -responseFile /opt/database/response/dbca.rsp
Copying database files
1% complete
2% complete
18% complete
33% complete
Creating and starting Oracle instance
35% complete
40% complete
44% complete
49% complete
53% complete
54% complete
55% complete
58% complete
Completing Database Creation
59% complete
60% complete
61% complete
64% complete
68% complete
69% complete
Executing Post Configuration Actions
100% complete
Look at the log file "/usr/oracle/cfgtoollogs/dbca/orcl/orcl.log" for further details.
# 查看是否运行
ps -ef | grep ora_ | grep -v grep
4.3 静默配置监听
#netca.rsp 默认不需要修改
[oracle@db02server ~]$ netca -silent -responsefile /opt/database/response/netca.rsp
Parsing command line arguments:
Parameter "silent" = true
Parameter "responsefile" = /opt/database/response/netca.rsp
Done parsing command line arguments.
Oracle Net Services Configuration:
Profile configuration complete.
Oracle Net Listener Startup:
Running Listener Control:
/usr/oracle/product/12c/db_1/bin/lsnrctl start LISTENER
Listener Control complete.
Listener started successfully.
Listener configuration complete.
Oracle Net Services configuration successful. The exit code is 0
5.1修改启动和关闭实例的程序
vim /usr/oracle/product/12c/db_1/bin/dbstart
将ORACLE_HOME_LISTNER=$1修改为ORACLE_HOME_LISTNER=/usr/oracle/product/12c/db_1
vim /usr/oracle/product/12c/db_1/bin/dbshut
同上,将ORACLE_HOME_LISTNER=$1修改为ORACLE_HOME_LISTNER=/usr/oracle/product/12c/db_1
# 修改/etc/oratab
修改/etc/oratab
将orcl:/usr/oracle/product/12c:N修改为orcl:/usr/oracle/product/12c:Y
# 启动实例
/usr/oracle/product/12c/db_1/bin/dbstart
# 开机启动Oracle
chmod +x /etc/rc.d/rc.local
vim /etc/rc.d/rc.local
su oracle -lc "/usr/oracle/product/12c/db_1/bin/lsnrctl start"
su oracle -lc "/usr/oracle/product/12c/db_1/bin/dbstart"
# 进入数据库
sqlplus / as sysdba
搜索软件 (oracle.com)
[oracle@db02server ~]$ sqlplus / as sysdba
SQL*Plus: Release 12.2.0.1.0 Production on Thu Mar 16 23:39:54 2023
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
SQL> alter user system account unlock;
User altered.
SQL>
SQL> connect system/Oracle123 as sysdba
Connected.
SQL>
SQL>
EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。
EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用。
IMP只适用于EXP导出的文件,不适用于EXPDP导出文件;IMPDP只适用于EXPDP导出的文件,而不适用于EXP导出文件。
su - oracle
sqlplus / as sysdba
创建directory空间
创建一个备份的文件目录(oracle用户可读写的文件)
mkdir -p /usr/oracle/product/oracle_backup
chown -R oracle:oinstall /usr/oracle
chmod -R 775 /usr/oracle
创建逻辑目录,该命令不会在操作系统创建真正的目录,最好以sysdba等管理员身份创建。
SQL>create directory oracle_backup as '/usr/oracle/product/oracle_backup';
给用户oracle_backup赋予逻辑目录的读写权限
SQL>grant read,write on directory oracle_backup to PUBLIC;
查看管理员目录
SQL> select * from dba_directories;
OWNER
--------------------------------------------------------------------------------
DIRECTORY_NAME
--------------------------------------------------------------------------------
DIRECTORY_PATH
--------------------------------------------------------------------------------
ORIGIN_CON_ID
-------------
SYS
ORACLE_BACKUP
/usr/oracle/product/oracle_backup
备份数据:
1)按用户导出
expdp system/[email protected]/orcl directory=oracle_backup dumpfile=backup.dmp full=y
2)并行进程parallel
expdp system/[email protected]/orcl directory=oracle_backup dumpfile=backup.dmp parallel=40 full=y
3) 按表名导出
expdp system/[email protected]/orcl tables=help,test directory=oracle_backup dumpfile=backup.dmp full=y
4)导出整个数据库
expdp system/Oracle123 directory=oracle_backup dumpfile=backup.dmp full=y
恢复数据:
1)按用户导入
impdp system/[email protected]/orcl directory=oracle_backup dumpfile=backup.dmp
2)导入数据库
impdp system/Oracle123 directory=oracle_backup dumpfile=backup.dmp full=y
3)追加数据
impdp system/Oracle123 directory=oracle_backup dumpfile=backup.dmp TABLE_EXISTS_ACTION
[oracle@db01server ~]$ vim /home/oracle/backup_oracle.sh
[oracle@db01server ~]$ cat /home/oracle/backup_oracle.sh
#!/bin/bash
DATA_DIR=""
BAKUP_TIME=`date +%Y-%m-%d`
DAYS=15
echo "Starting backup..."
echo "Bakup file path $DATA_DIR/$BAKUP_TIME.dmp"
su - oracle <
[oracle@db01server ~]$ sqlplus / as sysdba
SQL*Plus: Release 12.2.0.1.0 Production on Fri Mar 24 11:44:23 2023
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
SQL>
# 查询Orcal密码的有效期设置,LIMIT字段是密码有效天数
SQL> SELECT * FROM dba_profiles WHERE profile='DEFAULT' AND resource_name='PASSWORD_LIFE_TIME';
PROFILE
--------------------------------------------------------------------------------
RESOURCE_NAME RESOURCE
-------------------------------- --------
LIMIT
--------------------------------------------------------------------------------
COM INH IMP
--- --- ---
DEFAULT
PASSWORD_LIFE_TIME PASSWORD
180
NO NO NO
# 设置Oracle密码有效期为UNLIMITED
SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
Profile altered.
SQL> SELECT * FROM dba_profiles WHERE profile='DEFAULT' AND resource_name='PASSWORD_LIFE_TIME';
PROFILE
--------------------------------------------------------------------------------
RESOURCE_NAME RESOURCE
-------------------------------- --------
LIMIT
--------------------------------------------------------------------------------
COM INH IMP
--- --- ---
DEFAULT
PASSWORD_LIFE_TIME PASSWORD
UNLIMITED
NO NO NO
# 修改Oracle用户密码
SQL> alter user system identified by Oracle123;
User altered.
监控参数:
oracledb_exporter_last_scrape_duration_seconds 最后一次抓取用时,单位:秒
oracledb_exporter_last_scrape_error 最后一次抓取错误
oracledb_exporter_scrapes_total 抓取总数
oracledb_up 数据库运行状态
oracledb_activity_execute_count 活动执行计数
oracledb_activity_parse_count_total 活动解析总数
oracledb_activity_user_commits 活动用户提交数
oracledb_activity_user_rollbacks 活动用户回滚数
oracledb_sessions_activity 会话活动数
oracledb_wait_time_application 应用等待时间
oracledb_wait_time_commit 提交等待时间
oracledb_wait_time_concurrency 并发等待时间
oracledb_wait_time_configuration 配置等待时间
oracledb_wait_time_network 网络等待时间
oracledb_wait_time_other 其他等待时间
oracledb_wait_time_scheduler 调度器等待时间
oracledb_wait_time_system_io 系统io等待时间
oracledb_wait_time_user_io 用户io等待时间
oracledb_tablespace_bytes 表空间大小,单位:字节
oracledb_tablespace_max_bytes 表空间最大字节数
oracledb_tablespace_free 剩余表空间
oracledb_tablespace_used_percent 表空间使用百分比
oracledb_process_count 进程计数
oracledb_resource_current_utilization 资源当前利用率
oracledb_resource_limit_value 资源限制值
下载地址:https://github.com/iamseth/oracledb_exporter/releases
tar -xvf oracledb_exporter.0.3.0.linux-amd64.tar.gz
mv oracledb_exporter /usr/local/bin/oracledb_exporter
mv default-metrics.toml /etc/oracledb_exporter/default-metrics.toml
修改权限
chown oracle:oinstall /usr/local/bin/oracledb_exporter
chmod 775 /usr/local/bin/oracledb_exporter
chown oracle:oinstall /etc/oracledb_exporter/default-metrics.toml
chmod 775 /etc/oracledb_exporter/default-metrics.toml
设置执行的环境变量
vim /home/oracle/.bashrc
# export DATA_SOURCE_NAME=用户名/密码@数据库服务名
export DATA_SOURCE_NAME=system/[email protected]/orcl
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
source /home/oracle/.bashrc
[root@db01server ~]# oracledb_exporter
oracledb_exporter: error while loading shared libraries: libclntsh.so.18.1: cannot open shared object file: No such file or directory
[root@db01server ~]#
[root@db01server ~]# su oracle
[oracle@db01server root]$ cd $ORACLE_HOME/lib
[oracle@db01server lib]$
[oracle@db01server lib]$ pwd
/usr/oracle/product/12c/db_1/lib
[oracle@db01server lib]$ ln -s libclntsh.so libclntsh.so.18.1
[oracle@db01server lib]$ exit
exit
[root@db01server ~]# vim /etc/ld.so.conf
[root@db01server ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/oracle/product/12c/db_1/lib/libclntsh.so.18.1
[root@db01server ~]# ldconfig
# 后台运行程序(nohub:切换窗口不退出程序)
[oracle@db01server ~]$ nohup oracledb_exporter --default.metrics "/etc/oracledb_exporter/default-metrics.toml"
INFO[0000] Starting oracledb_exporter 0.3.0rc1 source="main.go:540"
INFO[0000] Successfully loaded default metrics from: /etc/oracledb_exporter/default-metrics.toml source="main.go:515"
INFO[0000] No custom metrics defined. source="main.go:530"
INFO[0000] Listening on :9161 source="main.go:560"
[oracle@db01server ~] ss -tunlp | grep 91
tcp LISTEN 0 1024 [::]:9161 [::]:* users:(("oracledb_export",pid=28245,fd=5))
创建监控:
# 修改prometheus的配置文件
cat /home/vince/prometheus/prometheus.yml
# 监控oracle12c
- job_name: 'oracle12c'
static_configs:
- targets:
- 172.25.250.210:9161
浏览器访问:http://172.25.250.254:9090/targets
Grafana添加11121模板
浏览器访问http://172.25.250.254:3000
创建触发:
[root@bastion vince]# vim rules/oracle.rules
groups:
- name: oracle
rules:
- alert: Oracle Status
expr: oracledb_up == 0
for: 5s
labels:
severity: warning
annotations:
summary: '业务报警({{$labels.instance}}): Oracle服务关闭'
description: 'Oracle服务关闭,请检查'
[root@bastion vince]# systemctl restart prometheus.service
创建告警:
[root@bastion vince]# vim alertmanager/alertmanager.yml
global:
resolve_timeout: 5m
smtp_from: '发件人邮箱账号'
smtp_smarthost: '发件服务器:465'
smtp_auth_username: '发件人邮箱账号'
smtp_auth_password: '密码'
smtp_require_tls: false
templates:
- '/home/vince/alertmanager/email.tmpl'
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 1m
repeat_interval: 1h
receiver: 'default_mail'
receivers:
- name: 'default_mail'
email_configs:
- to: '收件人邮箱账号,收件人邮箱账号'
headers: { Subject: " 【告警信息】{{ .CommonLabels.instance }} {{ .CommonLabels.alertname }} " }
html: '{{ template "email.to.html" . }}'
send_resolved: true
创建告警模板:
[root@bastion vince]# vim alertmanager/email.tmpl
======== 异常告警 ========
告警名称:{{ $alert.Labels.alertname }}
告警级别:{{ $alert.Labels.severity }}
告警机器:{{ $alert.Labels.instance }}
告警网卡:{{ $alert.Labels.device }}
告警详情:{{ $alert.Annotations.summary }}
告警时间:{{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========== END ==========
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
======== 告警恢复 ========
告警名称:{{ $alert.Labels.alertname }}
告警级别:{{ $alert.Labels.severity }}
告警机器:{{ $alert.Labels.instance }}
告警网卡:{{ $alert.Labels.device }}
告警详情:{{ $alert.Annotations.summary }}
告警时间:{{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间:{{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========== END ==========
{{- end }}
{{- end }}
{{- end }}
[root@bastion vince]# systemctl restart alertmanger.service
[root@bastion vince]# systemctl restart prometheus.service
adg:主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;
主数据库一般是实时的业务数据库,从数据库的作用和使用场合一般有几个:一是作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作;二是可在从数据库作备份、数据统计等工作,这样不影响主数据库的性能,三、实现读写分离。
区别:1、dg指的是Data Guard数据卫士,拥有备份的功能,能够确保数据的高可用性和数据保护,adg是“Active Data Guard”,可以查询或导出数据,适用于只读性的应用;2、dg读写不能并行,adg的读写可以并行。
角色 | 操作系统 | IP | 数据库名称 | SID |
---|---|---|---|---|
主库(master) | centos7 | 172.25.250.210 | orcl | orcl |
备库(slave) | centos7 | 172.25.250.211 | orcl | orcl-slave |
1.两台数据库配置域名映射
cat >> /etc/hosts << EOF
172.25.250.210 db01server
172.25.250.211 db02server
EOF
2.ADG配置
#主库配置
#开启强制日志记录
# sqlplus 登录
sqlplus / as sysdba
# 先关闭数据库
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
# 启动到nomount状态
SQL> startup nomount;
ORACLE instance started.
Total System Global Area 763363328 bytes
Fixed Size 8625368 bytes
Variable Size 603980584 bytes
Database Buffers 142606336 bytes
Redo Buffers 8151040 bytes
# 在启动到mount状态;
SQL> alter database mount;
Database altered.
# 配置为强日志记录
#修改数据库为强制记日志,这是必须的操作,主库的每一步操作都得记录到日志中去。
SQL> alter database force logging;
Database altered.
#修改为归档模式
#修改数据库为归档模式,因为dg是通过传送归档日志到备库然后应用来保证主备库一致的
SQL> alter database archivelog;
Database altered.
#查询状态,Enabled为开启状态
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /usr/oracle/product/12c/db_1/dbs/arch
Oldest online log sequence 1
Next log sequence to archive 1
Current log sequence 1
#创建redo日志文件,必须为主库的redo文件数+1
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/usr/oracle/oradata/orcl/redo04.redo') size 200M;
Database altered.
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/usr/oracle/oradata/orcl/redo05.redo') size 200M;
Database altered.
SQL>
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/usr/oracle/oradata/orcl/redo06.redo') size 200M;
Database altered.
SQL>
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/usr/oracle/oradata/orcl/redo07.redo') size 200M;
Database altered.
#查询日志文件
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文件位于$ORACLE_HOME/dbs目录下,命名格式为init+数据库名.ora
#如数据库名为orcl,则pfile文件就是initorcl.ora文件
SQL> create pfile from spfile;
File created.
# 在目录/usr/oracle/oradata/orcl 下创建(自定义,不固定)
[root@db01server ~]# mkdir /usr/oracle/oradata/orcl/archivelog -p
[oracle@db01server ~]$ cd /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'
~
#主库
[oracle@db01server dbs]$ mkdir /usr/oracle/oradata/orcl/backup
将pfile文件和密码文件orapworcl(命名规则为orapw+数据库名)发送到备库的相同目录
[oracle@db01server dbs]$ scp /usr/oracle/product/12c/db_1/dbs/initorcl.ora oracle-standby:/usr/oracle/product/12c/db_1/dbs/.
[oracle@db01server dbs]$ scp /usr/oracle/product/12c/db_1/dbs/orapworcl oracle-standby:/usr/oracle/product/12c/db_1/dbs/.
#根据修改的pfile文件启动数据库(在主库操作)
[oracle@db01server dbs]$ sqlplus / as sysdba
SQL*Plus: Release 12.2.0.1.0 Production on Fri Mar 17 16:45:15 2023
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to an idle instance.
SQL>
SQL> shutdown immediate
ORA-01507: database not mounted
SQL>
SQL> startup nomount pfile='/usr/oracle/product/12c/db_1/dbs/initorcl.ora';
ORACLE instance started.
Total System Global Area 763363328 bytes
Fixed Size 8625368 bytes
Variable Size 482345768 bytes
Database Buffers 264241152 bytes
Redo Buffers 8151040 bytes
SQL>
SQL> create spfile from pfile;
File created.
SQL>
SQL> alter database mount;
Database altered.
SQL>
SQL> alter database open;
Database altered.
#配置主库的listener.ora文件和tnsnames.ora文件
[oracle@db01server dbs]$ cd $ORACLE_HOME/network/admin
[oracle@db01server admin]$ pwd
/usr/oracle/product/12c/db_1/network/admin
[oracle@db01server admin]$
[oracle@db01server admin]$ ls
listener.ora samples shrept.lst sqlnet.ora
[oracle@db01server admin]$
[oracle@db01server 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@db01server 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 = 172.25.250.211)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
ORCL_PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.250.210)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
(UR=A)
)
)
#重启监听
# 监听配置好之后重启监听
[oracle@db01server admin]$ lsnrctl stop
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-MAR-2023 17:01:27
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle-primary)(PORT=1521)))
The command completed successfully
[oracle@db01server admin]$
[oracle@db01server admin]$
[oracle@db01server admin]$ lsnrctl start
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-MAR-2023 17:01:34
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Starting /usr/oracle/product/12c/db_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 12.2.0.1.0 - Production
System parameter file is /usr/oracle/product/12c/db_1/network/admin/listener.ora
Log messages written to /usr/oracle/diag/tnslsnr/db01server/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db01server)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle-primary)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production
Start Date 17-MAR-2023 17:01:34
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /usr/oracle/product/12c/db_1/network/admin/listener.ora
Listener Log File /usr/oracle/diag/tnslsnr/db01server/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db01server)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "orcl" has 1 instance(s).
Instance "orcl-primary", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@db01server admin]$
#备库配置
#修改从主库发送过来的pfile文件
[oracle@db02server ~]$ cd $ORACLE_HOME/dbs
[oracle@db02server dbs]$
[oracle@db02server dbs]$ pwd
/usr/oracle/product/12c/db_1/dbs
[oracle@db02server dbs]$
[oracle@db02server dbs]$ vim initorcl.ora
orcl.__data_transfer_cache_size=0
orcl.__db_cache_size=260046848
orcl.__inmemory_ext_roarea=0
orcl.__inmemory_ext_rwarea=0
orcl.__java_pool_size=4194304
orcl.__large_pool_size=8388608
orcl.__oracle_base='/usr/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=306184192
orcl.__sga_target=457179136
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=167772160
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_standby'
*.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)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/usr/oracle/oradata/orcl/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl_primary'
*.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'
*.memory_target=726m
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
#备库
[oracle@db02server dbs]$ mkdir /usr/oracle/oradata/orcl/backup
#启动备库到nomount状态
[oracle@db02server dbs]$ sqlplus / as sysdba
SQL*Plus: Release 12.2.0.1.0 Production on Fri Mar 17 17:12:52 2023
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>
SQL> startup nomount pfile="/usr/oracle/product/12c/db_1/dbs/initorcl.ora";
ORACLE instance started.
Total System Global Area 763363328 bytes
Fixed Size 8625368 bytes
Variable Size 603980584 bytes
Database Buffers 142606336 bytes
Redo Buffers 8151040 bytes
#配置备库的listener.ora文件和tnsnames.ora文件
#配置监听
[oracle@db02server ~]$ cd $ORACLE_HOME/network/admin
[oracle@db02server admin]$
[oracle@db02server admin]$ pwd
/usr/oracle/product/12c/db_1/network/admin
[oracle@db02server admin]$
[oracle@db02server admin]$ ls
listener.ora samples shrept.lst sqlnet.ora
[oracle@db02server admin]$
[oracle@db02server admin]$ vim listener.ora
[oracle@db02server admin]$
[oracle@db02server admin]$ cat 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 = db02server)(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
[oracle@db02server admin]$
#配置TNS
[oracle@db02server admin]$ vim tnsnames.ora
[oracle@db02server admin]$
[oracle@db02server admin]$ cat tnsnames.ora
i
# 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)
)
)
#重启监听
[oracle@db02server admin]$
[oracle@db02server admin]$ lsnrctl stop
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 20-MAR-2023 10:33:43
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db02server)(PORT=1521)))
The command completed successfully
[oracle@db02server admin]$
[oracle@db02server admin]$ lsnrctl start
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 20-MAR-2023 10:33:52
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Starting /usr/oracle/product/12c/db_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 12.2.0.1.0 - Production
System parameter file is /usr/oracle/product/12c/db_1/network/admin/listener.ora
Log messages written to /usr/oracle/diag/tnslsnr/db02server/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db02server)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db02server)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production
Start Date 20-MAR-2023 10:33:52
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /usr/oracle/product/12c/db_1/network/admin/listener.ora
Listener Log File /usr/oracle/diag/tnslsnr/db02server/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db02server)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "orcl" has 1 instance(s).
Instance "orcl-standby", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
#使用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
4.ADG的维护
#启停顺序
启动:
先备库,后主库
关闭:
先主库,后备库
#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
OGG是Oracle GoldenGate的简写,它支持在Oracle数据库和其他受支持的异构数据库之间复制数据。
主要用途-数据迁移:OGG的标准用途是将数据从Oracle迁移到非Oracle数据库,或从非Oracle数据库迁移到Oracle数据库,支持的数据源有:
Oracle和非Oracle数据库,如MySQL,IBM DB2,MS SQL,Sybase等
OGG特性:
1)、对生产系统影响小:实时读取交易日志,以低资源占用实现大量的数据实时复制。
2)、以交易为单位,保证交易一致性:只同步已提交的数据。
3)、高性能,智能的交易重组和操作合并,使用数据库本地接口访问,并行处理体系,灵活的拓扑结构:支持一对一、一对多、多对一、多对多和双向复制等。
date +%Y年-%m月-%d日-%H时-%M分
Oracle Real Application Cluster (RAC,实时应用集群)用来在集群环境下实现多机共享数据库,以保证应用的高可用性;同时可以自动实现并行处理及负载均衡,并能实现数据库在故障时的容错和无断点恢复。它是oracle数据库支持网络计算环境的核心技术。
Oracle RAC和单个数据库的区别:主要区别还是RAC是集群式数据库,通过集群软件管理。