一、oracle19c主备搭建
一、环境搭建
主机 | IP |
---|---|
p19cadg1 | 192.168.238.31 |
p19cadg2 | 192.168.238.32 |
内存 100g
1.修改主机名
root:
vi /etc/hostname
2.配置网络yum源
删除redhat7.0系统自带的yum软件包;
备份原信息
rpm -qa|grep yum >oldyum.pkg
不检查依赖,直接删除rpm包
rpm -qa|grep yum|xargs rpm -e --nodeps
自行下载所需要的软件包。包名会更新,根据当前最新的下载。如有有依赖问题,下载依赖包进行安装。
阿里云网络源地址:https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/
网易163网络源地址:http://mirrors.163.com/ CentOS
网络源地址:http://centos.ustc.edu.cn/centos/
打开网页,ctrl+f 搜索相关yum包:http://mirrors.163.com/centos/7/os/x86_64/Packages/
yum-3.4.3-168.el7.centos.noarch.rpm
yum-langpacks-0.4.2-7.el7.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
yum-rhn-plugin-2.0.1-10.el7.noarch.rpm
yum-utils-1.1.31-54.el7_8.noarch.rpm
根据依赖项安装;
rpm -ivh yum-*
下载repo配置文件。
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/centos/7/atomic/x86_64/repo/CentOS-Base.repo
将文件中所有的$releasever**更换为**7,否则无法使用。
sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
下载KEY文件:
wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 http://mirrors.163.com/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7
清理yum缓存。将服务器软件包信息缓存至本地,提高搜索安装效率
yum -y clean all && yum -y makecache
3.静态ip配置
root:
vi /etc/sysconfig/network-scripts/ifcfg-ens33
添加:
IPADDR=192.168.238.31/32
NETMASK=255.255.255.0
GATEWAY=192.168.238.2
PEERDNS=no
DNS1=114.114.114.114
DNS2=8.8.8.8
PREFIX=24
IPADDR=192.168.238.31/32(自己ip)
GATEWAY=192.168.238.2 (自己网关)
sudo systemctl restart network (重启网络状态)
4.关闭防火墙
使用root权限登录到Red Hat系统的终端。
停止当前运行的防火墙服务。可以使用以下命令:
systemctl stop firewalld
禁止防火墙服务在系统启动时自动启动。可以使用以下命令:
systemctl disable firewalld
确认防火墙服务已停止并禁用。可以使用以下命令验证:
systemctl status firewalld
如果输出显示 “inactive”,则表示防火墙服务已成功停止并禁用。
重新启动系统,以使更改生效。
二、主备库搭建
主库配置:
hostname配置
hostnamectl set-hostname p19cadg1
hosts文件配置
cat <<EOF>>/etc/hosts
#Public IP
192.168.238.31 p19cadg1
192.168.238.32 p19cadg2
EOF
配置目录(so用来存放oracle安装包)
mkdir /opt/oracle
mkdir /soft
chown -R oracle:oinstall /opt/oracle
chmod -R 775 /opt/oracle
下载安装包:
oracle-database-ee-19c-1.0-1.x86_64.rpm
oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
安装
rpm -ivh oracle-database-preinstall-19c-1.0-1.el7.x86 64.rpm
注意:(这里会出现问题) 根据提示安装所需要的包:(如)
yum -y install compat-libcap1 compat-libstdc++-33 libaio-devel libstdc++-devel ksh
安装oracle-database-ee-19c-1.0-1.x86_64.rpm
rpm -ivh oracle-database-ee-19c-1.0-1.x86_64.rpm
准备配置文件:
cd /etc/sysconfig
cp oracledb_ORCLCDB-19c.conf oracledb_itpuxdb-19c.conf
cat /etc/sysconfig/oracledb_itpuxdb-19c.conf
cd /etc/init.d
cp oracledb_ORCLCDB-19c oracledb_itpuxdb-19c
下面是在vi里面执行(全局替换)
vi /etc/init.d/oracledb_itpuxdb-19c
:%s/ORCLCDB/itpuxdb/g
:%s/ORCLPDB1/itpuxpdb/g
然后在修改下面的内容:
创建数据库:
/etc/init.d/oracledb_itpuxdb-19c configure
检查环境:
su - oracle
ps -ef | grep smon
echo $ORACLE_HOME
lsnrctl status
su - oracle
echo "export LANG=en_US" >>~/.bash_profile
echo "export ORACLE_BASE=/opt/oracle">>~/.bash_profile
echo "export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1">> ~/.bash_profile
echo "export ORACLE_UNQNAME=itpuxdb">> ~/.bash_profile
echo "export ORACLE_SID=itpuxdb">>~/.bash profile
echo "NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;export NLS_LANG" >>~/.bash_profile
echo "export PATH=$PATH:/opt/oracle/product/19c/dbhome_1/bin" >> ~/.bash_profilesource ~/.bash_profile
env | grep ORACLE
一定要kanyix
2.备库的搭建
(前面一点都差不多,但是没有了**准备配置文件:创建数据库)
hostname配置
hostnamectl set-hostname p19cadg2
hosts文件配置
cat <<EOF>>/etc/hosts
#Public IP
192.168.134.239 p19cadg1
192.168.134.240 p19cadg2
EOF
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
配置目录
mkdir /opt/oracle
这个是配置目录,如果有的话就不用创建啦
准备预先安装的包
先把下载的包传到soft目录下
mkdir /soft
rpm -ivh oracle-database-preinstall-19c-1.0-1.el7.x86 64.rpm
注意:(这里会出现问题)
根据提示安装所需要的包:(如)
yum -y install compat-libcap1 ksh
手工目录的授权
chown -R oralce:oinstall /opt/oracle
chmod -R 775 /opt/oracle
安装oracle-database-ee:
rpm -ivh oracle-database-ee-19c-1.0-1.x86_64.rpm
检查环境:
su - oracle
ps -ef|grep smon
echo $ORACLE HOME
lsnrctl status(这个会出错误没关系,下面会配好)
环境变量配置
su - oracle
echo "export LANG=en_US">>~/.bash_profile
echo "export ORACLE_BASE=/opt/oracle">>~/.bash_profile
echo "export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1">> ~/.bash_profile
echo "export ORACLE_UNQNAME=itpuxdg">> ~/.bash_profile
echo "export ORACLE_SID=itpuxdg">>~/.bash_profile
echo "NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;export NLS_LANG">>~/.bash_profile
echo "export PATH=$PATH:/opt/oracle/product/19c/dbhome_1/bin">> ~/.bash_profilesource ~/.bash_profile
env |grep ORACLE
Ok完成
三、主备库监听
1.准备备库监听配置(主库在安装数据库的时候就安装了监听)
vi /opt/oracle/product/19c/dbhome_1/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS =(PROTOCOL =TCP)(HOST = p19cadg2)(PORT = 1521))
(ADDRESS =(PROTOCOL=IPC)(KEY=EXTPROC1521))
)
)SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=itpuxdg)
(SID_NAME=itpuxdg)
(ORACLE_HOME=/opt/oracle/product/19c/dbhome_1)
)
)
然后检查监听是否配置成功
lsnrctl start
lsnrctl status
主库配置
1.打开强制日志
su - oracle
sqlplus "/as sysdba"
alter database force logging;
2.设置为归档模式
alter system set db_recovery_file_dest_size=5g;
alter system set db_recovery_file_dest='/opt/oracle';
startup mount;
alter database archivelog;
alter database open;
alter system switch logfile;
可以出查看一下是否有归档文件
cd /opt/oracle/ITPUXDB/archivelog/
3.创建日志文件 如果主库有三个了(那就要创建四个)
select * from v$log;
select * from v$logfile;alter database add standby logfile group 4 '/opt/oracle/oradata/ITPUXDB/stredo04.10g' size 200m;
alter database add standby logfile group 5 '/opt/oracle/oradata/ITPUXDB/stredo05.log' size 200m;
alter database add standby logfile group 6 '/opt/oracle/oradata/ITPUXDB/stredo06.log' size 200m;
alter database add standby logfile group 7 '/opt/oracle/oradata/ITPUXDB/stredo07.log' size 200m;
主备库监听tnsnames.ora:
vi /opt/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
ITPUXDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = p19cadg1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = itpuxdb)
)
)ITPUXDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = p19cadg2)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = itpuxdg)
)
)
(主库只要加ITPUXDG,备库全加上)
测试
tnsping itpuxdb
tnsping itpuxdg
准备数据库密码文件
主:
删除
opt/oracle/product/19c/dbhome_1/dbs/orapwitpuxdb
orapwd file=/opt/oracle/product/19c/dbhome_1/dbs/orapwitpuxdb password=ITPUX-123
备:
cd /opt/oracle/product/19c/dbhome_1/dbs
scp 192.168.238.31:/opt/oracle/product/19c/dbhome_1/dbs/orapwitpuxdb
mv orapwitpuxdb orapwitpuxdg
登录(测试一下)
主库:
sqlplus "sys/ITPUX-123@itpuxdg as sysdba"
准备参数文件
主:
sqlplus登录
create pfile='/opt/oracle/pfile.ora' from spfile;
(等会有改参数,这个是备份一份,然后传到备库里面在改参数)
然后在把pfile传给备库
scp /opt/oracle/pfile.ora 192.168.238.32:/opt/oracle/
主库参数(重启生效)
startup打开数据库
alter system set db_unique_name='itpuxdb' scope=spfile;
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(itpuxdb,itpuxdg)' scope=both;
alter system set LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=itpuxdb' scope=both;
alter system set LOG_ARCHIVE_DEST_2='SERVICE=itpuxdg LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=itpuxdg' scope=both;
alter system set fal_client='itpuxdb' scope=both;
alter system set FAL_SERVER='itpuxdg' scope=both;
alter system set DB_FILE_NAME_CONVERT='ITPUXDG','ITPUXDB' scope=spfile;
alter system set LOG_FILE_NAME_CONVERT='ITPUXDG','ITPUXDB' scope=spfile;
alter system set standby_file_management=AUTO scope=both;
#重启生效:
shutdown immediate
startup
alter system set LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=itpuxdb' scope=both;
备库参数
cd /opt/oracle
vi pfile.ora
#先把"*."前面的都删了,把itpuxdb改成itpuxdg,在添加下面的。
db_unique_name='itpuxdg'
LOG_ARCHIVE_CONFIG='DG_CONFIG=(itpuxdg,itpuxdb)'
LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=itpuxdg'
LOG_ARCHIVE_DEST_2='SERVICE=itpuxdb LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=itpuxdb' fal_client='itpuxdg'
FAL_SERVER='itpuxdb'
DB_FILE_NAME_CONVERT='ITPUXDB','ITPUXDG'
LOG_FILE_NAME_CONVERT='ITPUXDB','ITPUXDG'
standby_file_management=AUTO
创建目录
mkdir -p /opt/oracle/admin/itpuxdg/adump
mkdir -p /opt/oracle/oradata/ITPUXDG
备库:
sqlplus "sys/ITPUX-123@itpuxdg as sysdba"
startup pfile='/opt/oracle/pfile.ora' nomount;
create spfile from pfile='/opt/oracle/pfile.ora';
shutdown immediate;
startup nomount;
打开时出现了
ORA-00845: MEMORY_TARGET not supported on this system
那就是 memory_target大于了/dev/shm,重新调整数据库参数MEMORY_TARGET的大小,要下于/dev/shm的大小
查看/dev/shm的大小
df -h | grep shm
创建dataguard数据库(主库备库都可以)
rman target sys/ITPUX-123@itpuxdb auxiliary sys/ITPUX-123@itpuxdg
duplicate target database for standby from active database;
检查测试
检查dataguard状态:
select NAME,OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE,SWITCHOVER_STATUS from v$database;
启动dataguard数据同步
sqlplus "/as sysdba"
alter database recover managed standby database disconnect from session; 开启同步
关闭dataguard数据同步
alter database recover managed standby database cancel;
以上步骤知识了解,不要执行,从下开始再执行。
启动adg模式的备库:
上述备库的数据库是nomount状态,再执行完rman后
alter database open;
alter database recover managed standby database disconnect from session;
主库测试
alter system switch logfile;
主库检查dataguard状态:
select NAME,OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE,SWITCHOVER_STATUS from v$database;
dataguard数据同步测试
主库:
create tablespace cgedu66 datafile '/opt/oracle/oradata/ITPUXDB/cgedu66 .dbf' size 10m autoextend off;
create user cgedu66 identified by cgedu66 default tablespace cgedu66 ;
grant dba to cgedu66 ;conn cgedu66 /cgedu66 ;
create table cgedu66 .itpux01(c1 varchar2(10),c2 number);
insert into itpux01 values('itpux01','1');
insert into itpux01 values('itpux02','2');commit;
select * from cgedu66.itpux01;
alter system switch logfile;
备库检查:
select * from cgedu66.itpux01;
查看dataguard状态:
select NAME,OPEN_MODE,PROTECTION_MODE,DATABASE_ROLE,SWITCHOVER_STATUS from v$database;
检查dataguard日志
主
tail -100f /opt/oracle/diag/rdbms/itpuxdb/itpuxdb/trace/alert_itpuxdg.log
备
tail -100f /opt/oracle/diag/rdbms/itpuxdg/itpuxdg/trace/alert_itpuxdg.log
二、oracle主备补丁升级和回退
一、备库升级
1.备库备份$ORALCE_HOME
查看Oracle目录大小
du -sh $ORACLE_HOME
备份目录
cd $ORACLE_HOME
cd ..
ls
将dbhome_1压缩
tar -cvzf db_home.tar.gz db_home_1 /opt/oracle/product/19c
ls 查看
再看一下dbhome_1.tar.gz大小
du -sh dbhome_1.tar.gz
关闭监听
lsnrctl stop
lsnrctl status
关闭数据库
sqlplus / as sysdba
startup
shutdown immediate
exit
查看sqlplus进程
ps -ef | grep sqlplus
2.升级Opatch
拷贝当前Opatch目录
su - oracle
cd $ORACLE_HOME
cp OPatch -r OPatch1
下载解压包目录
cd /soft
解压到ORACLE_HOME目录里
unzip -d $ORACLE_HOME p6880880_190000_Linux-x86-64.zip
A 全部替换
查看OPATCH版本
cd $ORALCE_HOME/OPatch
./opatch version
3.应用补丁
cd /soft
ls
unzip p35943157_190000_Linux-x86-64.zip
进入补丁文件夹
cd 35943157
给出Opatch的一些用法
$ORACLE_HOME/OPatch/opatch prereq -help
4.补丁冲突检测
$ORACLE_HOME/OPatch/opatch prereq CheckConflictAgainstOHWithDetail
应用补丁
$ORACLE_HOME/OPatch/opatch apply
过程中 输入 y
5.检查
补丁应用情况
$ORACLE_HOME/OPatch/opatch lsinv
二、主库升级
su - root
chmod -R 755 /soft
chown oracle:oinstall /soft
cd /soft
ls
su - oracle
cd $ORALCE_HOEM
cd ..
ls
备份oraclehome目录
tar -cvzf dbhome_1.tar.gz dbhome_1
关闭监听
lsnrctl start
lsnrctl stop
lsnrctl status
关闭数据库
sqlplus / as sysdba
startup
shutdown immediate
exit
查看sqlplusjc
ps -ef | grep sqlplus
主库升级Opatch和应用bud
1升级Opatch
cd $ORACLE_HOME
cp -R OPatch OPatch1
cd /soft
unzip -d $ORACLE_HOME p6880880_190000_Linux-x86-64.zip
输入 A 全部替换
查看OPatch版本
cd $ORACLE_HOME
cd OPatch
./opatch version
主库打补丁
cd /soft
ls
unzip p35943157_190000_Linux-x86-64.zip
cd 35943157
$ORACLE_HOME/OPatch/opatch prereq -help
检查补丁冲突
$ORACLE_HOME/OPatch/opatch prereq CheckConflictAgainstOHWithDetail
应用补丁
$ORACLE_HOME/OPatch/opatch apply
输入y
检查补丁应用情况
$ORACLE_HOME/OPatch/opatch lsinv
主库注册前检测
cd $ORACLE_HOME/OPatch
lsnrctl start
sqlplus / as sysdba
startup upgrade
最后使用 startup upgrade 命令启动数据库并在升级模式下运行。这将启动数据库实例并将其升级到新版本,但不会打开数据库以供用户连接。
!
./datapatch -prereq
检查完成 注册补丁
./datapatch -verbose
编译无效对象
cd $ORACLE_HOME//rdbms/admin
sqlplus / as sysdba
SQL> @utlrp.sql
查看主库的补丁注册情况
select VERSION,VERSION_FULL from dba_registry;
完成
三、主备库切换,完成补丁注册
1.主库操作p19cadg1
SQL>select name,open_mode,switchover_status from v$database;
SQL>alter system switch logfile;
SQL>alter system archive log current;
SQL>alter database commit to switchover to physical standby with session shutdown;
SQL>shutdown abort
SQL>startup mount
SQL>select switchover_status from v$database;
SQL>alter database open;
SQL>select switchover_status from v$database;
2.备库操作mount状态p19cadg2
在主库启动时,备库在mount状态
sqlplus / as sysdba
SQL>startup mount
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
SQL> alter database commit to switchover to primary;
SQL> select name,DATABASE_ROLE from v$database;
SQL> alter database open;
SQL> select name,open_mode,switchover_status from v$database;
3.新备库(原主库启动实时日志)(p19cadg1)
SQL> alter database recover managed standby database using current logfile disconnect from session;
SQL> select name,open_mode,switchover_status from v$database;
4.新主库(原备库切换日志)(p19cadg2)
SQL> alter system switch logfile;
分别查看当前主备库切换后当前日志序列号
主库:
SQL> archive log list
备库:
SQL> archive log list
5.查看当前主备库状态
主库:
SQL> select name,open_mode,switchover_status from v$database;
备库:
SQL> select name,open_mode,switchover_status from v$database;
6.检查新主库(原备库)补丁注册
SQL> select VERSION,VERSION_FULL from dba_registry;
四、补丁回退
1.关闭数据库和监听
现备库(原主库p19cadg1)
现主库(原备库p19cadg2)
查看进程(主备)
ps -ef | grep sqlplus
只能有一个
补丁回退
先备库p19cadg1
cd $ORACLE_HOME/OPatch
./opatch lspatches
./opatch rollback -id 35943157
编译无效对象
cd $ORACLE_HOME//rdbms/admin
$ sqlplus /nolog
SQL> CONNECT / AS SYSDBA
Connected.
SQL> @utlrp.sql
主库补丁回退
cd $ORACLE_HOME/OPatch
./opatch lspatches
./opatch rollback -id 35943157
主库回退注册补丁p19cadg2
注册前检查
cd $ORACLE_HOME/OPatch
lsnrctl start
sqlplus / as sysdba
startup upgrade
!
./datapatch -prereq
主库注册补丁p19cadg2
./datapatch -verbose
主库切换备库p19cadg2(主->备)
主p19cadg2 open状态
备p19cadg2 mount状态
SQL> select name,open_mode,switchover_status from v$database;
SQL> alter system switch logfile;
SQL> alter system archive log current;
SQL> alter database commit to switchover to physical standby with session shutdown;
SQL> shutdown abort
SQL> startup mount
SQL> select switchover_status from v$database;
SQL> alter database open;
SQL> select switchover_status from v$database;
备库切换主库p19cadg1(备->主)
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
SQL> alter database commit to switchover to primary;
SQL> select name,DATABASE_ROLE from v$database;
SQL> alter database open;
SQL> select name,open_mode,switchover_status from v$database;
备库启用实时日历p19cadg2
SQL> alter database recover managed standby database using current logfile disconnect from session;
SQL> select name,open_mode,switchover_status from v$database;
新主库切换日志
SQL> alter system switch logfile;
SQL> archive log list
主
备
查看数据库状态
select name,open_mode,switchover_status from v$database;
主
备
检查补丁注册情况
select VERSION,VERSION_FULL from dba_registry;
主
备
完成