数据库升级、打补丁是我们经常面对的日常工作内容。在正常情况下,两个因素是我们必须要考虑的问题:停机时间窗和回退方案。就Oracle而言,即便是最简单的更新操作,都难以做到“零停机”。回退方案是在一旦发现新版本存在问题,迅速的回退到原有的版本,支持应用访问。
目前,Oracle推荐两种大规模升级的方法:In-Place和Out-of-Place。In Place升级方法下,升级动作直接在原有的Database Home目录下。Out-of-Place则是选择了一个新的Oracle Database Home目录。相对于In place策略,Out-of-Place在空间上需要更多的消耗。
但是,Out-of-Place的好处也是比较明显的,首先是可以比较方便的进行回退,同时在Downtime停机时间上,也有比较强的优势。
Out-of-Place支持Oracle大版本和Patch两种操作方式,本文主要介绍使用Out-Of-Place方法打补丁Patch方法。
1、环境介绍
选择Oracle 11gR2进行测试,版本为11.2.0.4基础版。
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 – Production
环境变量情况如下:
[oracle@testlife OPatch]$ env | grep ORA
ORACLE_SID=testdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
目标是在当前版本基础上,使用最新的PSU补丁文件。
2、补丁操作
升级OPatch工具。Oracle默认自带的OPatch通常是不能满足补丁要求的,比较稳妥的手段是到MOS上下载最新的OPatch升级包,替代原有的程序。
[oracle@testlife upload]$ cp p6880880_112000_Linux-x86-64.zip $ORACLE_HOME
[oracle@testlife upload]$ cd $ORACLE_HOME
[oracle@testlife dbhome_1]$ mv OPatch OPatch_BAK
[oracle@testlife dbhome_1]$ unzip p6880880_112000_Linux-x86-64.zip
Archive: p6880880_112000_Linux-x86-64.zip
creating: OPatch/
inflating: OPatch/opatch.bat
inflating: OPatch/operr.bat
(篇幅原因,有省略……)
[oracle@testlife dbhome_1]$ cd OPatch
[oracle@testlife OPatch]$ ./opatch lsinventory
Oracle Interim Patch Installer version 11.2.0.3.10
Copyright (c) 2016, Oracle Corporation. All rights reserved.
Oracle Home : /u01/app/oracle/product/11.2.0/dbhome_1
Central Inventory : /u01/app/oraInventory
from : /u01/app/oracle/product/11.2.0/dbhome_1/oraInst.loc
OPatch version : 11.2.0.3.10
OUI version : 11.2.0.4.0
在升级过程中,会有两套Oracle环境变量切换的动作,为避免出现问题,可以设置两套ORACLE_HOME环境变量,根据不同的情况进行切换。
[oracle@testlife OPatch]$ cd ~
[oracle@testlife ~]$ export ORACLE_OWNER_GROUP=oracle:dba
[oracle@testlife ~]$ export ORACLE_BASE=$ORACLE_BASE
[oracle@testlife ~]$ export O_ORACLE_HOME=$ORACLE_HOME –Original Oracle Home
[oracle@testlife ~]$ export O_ORACLE_HOME_NAME=`grep OHOMENAME= $O_ORACLE_HOME/oui/bin/attachHome.sh | cut -f2 -d=`
[oracle@testlife ~]$ export C_ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1_clone –Clone Environment
[oracle@testlife ~]$ export C_ORACLE_HOME_NAME=clone
[oracle@testlife ~]$ env | grep ORA
C_ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1_clone
ORACLE_OWNER_GROUP=oracle:dba
ORACLE_SID=testdb
ORACLE_BASE=/u01/app/oracle
C_ORACLE_HOME_NAME=clone
O_ORACLE_HOME_NAME=OraDb11g_home1
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
O_ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
下面,使用原有数据库克隆新的环境Clone数据库。
--目录创建
[oracle@testlife ~]$ cd /
[oracle@testlife /]$ mkdir $C_ORACLE_HOME
[oracle@testlife /]$ chown $ORACLE_OWNER_GROUP $C_ORACLE_HOME
[oracle@testlife dbhome_1]$ pwd
/u01/app/oracle/product/11.2.0/dbhome_1
[oracle@testlife dbhome_1]$ ls -l | grep dbhome
[oracle@testlife dbhome_1]$ cd ..
[oracle@testlife 11.2.0]$ ls -l | grep dbhome
drwxr-xr-x. 75 oracle oinstall 4096 Oct 18 17:52 dbhome_1
drwxr-xr-x 2 oracle dba 4096 Oct 18 18:07 dbhome_1_clone
[oracle@testlife 11.2.0]$ cd $O_ORACLE_HOME
[oracle@testlife dbhome_1]$ tar cfp - . | (cd $C_ORACLE_HOME; tar xf - )
tar: ./bin/nmo: Cannot open: Permission denied
tar: ./bin/nmb: Cannot open: Permission denied
tar: ./bin/nmhs: Cannot open: Permission denied
tar: Exiting with failure status due to previous errors
[oracle@testlife dbhome_1]$
使用clone.pl脚本创建全新的Clone数据库。
[oracle@testlife dbhome_1]$ export ORACLE_HOME=$C_ORACLE_HOME –Clone数据库目录
[oracle@testlife dbhome_1]$ perl $ORACLE_HOME/clone/bin/clone.pl
> ORACLE_BASE=$ORACLE_BASE
> ORACLE_HOME=$C_ORACLE_HOME
> ORACLE_HOME_NAME=$C_ORACLE_HOME_NAME
./runInstaller -clone -waitForCompletion "ORACLE_BASE=/u01/app/oracle" "ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1_clone" "ORACLE_HOME_NAME=clone" -silent -noConfig -nowait
Starting Oracle Universal Installer...
Checking swap space: must be greater than 500 MB. Actual 7967 MB Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2016-10-18_06-13-35PM. Please wait ...Oracle Universal Installer, Version 11.2.0.4.0 Production
Copyright (C) 1999, 2013, Oracle. All rights reserved.
You can find the log of this install session at:
/u01/app/oraInventory/logs/cloneActions2016-10-18_06-13-35PM.log
.................................................................................................... 100% Done.
Installation in progress (Tuesday, October 18, 2016 6:13:56 PM CST)
.............................................................................. 78% Done.
Install successful
Linking in progress (Tuesday, October 18, 2016 6:14:00 PM CST)
Link successful
Setup in progress (Tuesday, October 18, 2016 6:14:35 PM CST)
Setup successful
End of install phases.(Tuesday, October 18, 2016 6:14:59 PM CST)
WARNING:
The following configuration scripts need to be executed as the "root" user.
/u01/app/oracle/product/11.2.0/dbhome_1_clone/root.sh
To execute the configuration scripts:
1. Open a terminal window
2. Log in as "root"
3. Run the scripts
The cloning of clone was successful.
Please check '/u01/app/oraInventory/logs/cloneActions2016-10-18_06-13-35PM.log' for more details.
切换到root进行脚本执行。
[root@testlife ~]# cd /u01/app/oracle/product/11.2.0/dbhome_1_clone
[root@testlife dbhome_1_clone]# ./root.sh
Check /u01/app/oracle/product/11.2.0/dbhome_1_clone/install/root_testlife.localdomain_2016-10-18_18-16-58.log for the output of root script
注意:此时Clone是一个和主库一样的程序包,主库在补丁过程中是支持对外操作的。
下面可以在Clone数据库上进行补丁操作。
[oracle@testlife upload]$ export ORACLE_HOME=$C_ORACLE_HOME
[oracle@testlife upload]$ cd 23615392/
[oracle@testlife 23615392]$ ls -l
total 32
drwxr-xr-x 13 oracle oinstall 4096 Jul 20 19:30 23054359
drwxr-xr-x 5 oracle oinstall 4096 Jul 20 19:30 23177551
-rw-r--r-- 1 oracle oinstall 18806 Jul 20 19:50 README.html
-rw-r--r-- 1 oracle oinstall 25 Jul 20 19:30 README.txt
[oracle@testlife 23615392]$ cd 23054359/
[oracle@testlife 23054359]$ $ORACLE_HOME/OPatch/opatch apply
Oracle Interim Patch Installer version 11.2.0.3.10
Copyright (c) 2016, Oracle Corporation. All rights reserved.
Oracle Home : /u01/app/oracle/product/11.2.0/dbhome_1_clone
Central Inventory : /u01/app/oraInventory
from : /u01/app/oracle/product/11.2.0/dbhome_1_clone/oraInst.loc
OPatch version : 11.2.0.3.10
OUI version : 11.2.0.4.0
(中间略...)
两个补丁均完成。暂停原有服务,进行目录切换。
[oracle@testlife 23177551]$ export ORACLE_HOME=$O_ORACLE_HOME
[oracle@testlife 23177551]$ env | grep ORACLE_SID
ORACLE_SID=testdb
[oracle@testlife 23177551]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:33:52 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
SQL> conn / as sysdba
Connected.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@testlife 23177551]$ $ORACLE_HOME/bin/lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 18-OCT-2016 19:34:32
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
The command completed successfully
将原有数据库目录Detach。
[oracle@testlife 23177551]$ cd ~
[oracle@testlife ~]$ export ORACLE_HOME=$O_ORACLE_HOME
[oracle@testlife ~]$ cd $ORACLE_HOME/..
[oracle@testlife 11.2.0]$ $ORACLE_HOME/oui/bin/detachHome.sh
Starting Oracle Universal Installer...
Checking swap space: must be greater than 500 MB. Actual 7967 MB Passed
The inventory pointer is located at /etc/oraInst.loc
The inventory is located at /u01/app/oraInventory
'DetachHome' was successful.
--原有目录修改名称
[oracle@testlife 11.2.0]$ mv $ORACLE_HOME `echo $ORACLE_HOME`_to_be_removed
[oracle@testlife 11.2.0]$ ls -l
total 8
drwxr-xr-x 79 oracle dba 4096 Oct 18 19:31 dbhome_1_clone
drwxr-xr-x. 75 oracle oinstall 4096 Oct 18 17:52 dbhome_1_to_be_removed
Detach新的目录对象。
[oracle@testlife 11.2.0]$ export ORACLE_HOME=$C_ORACLE_HOME
[oracle@testlife 11.2.0]$ $ORACLE_HOME/oui/bin/detachHome.sh
Starting Oracle Universal Installer...
Checking swap space: must be greater than 500 MB. Actual 7967 MB Passed
The inventory pointer is located at /etc/oraInst.loc
The inventory is located at /u01/app/oraInventory
'DetachHome' was successful.
--Clone出新的文件目录。
[oracle@testlife 11.2.0]$ mv $C_ORACLE_HOME $O_ORACLE_HOME
[oracle@testlife 11.2.0]$ export ORACLE_HOME=$O_ORACLE_HOME
[oracle@testlife 11.2.0]$ perl $ORACLE_HOME/clone/bin/clone.pl
> ORACLE_BASE=$ORACLE_BASE
> ORACLE_HOME=$O_ORACLE_HOME
> ORACLE_HOME_NAME=$O_ORACLE_HOME_NAME
./runInstaller -clone -waitForCompletion "ORACLE_BASE=/u01/app/oracle" "ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1" "ORACLE_HOME_NAME=OraDb11g_home1" -silent -noConfig -nowait
Starting Oracle Universal Installer...
Checking swap space: must be greater than 500 MB. Actual 7967 MB Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2016-10-18_07-39-36PM. Please wait ...Oracle Universal Installer, Version 11.2.0.4.0 Production
Copyright (C) 1999, 2013, Oracle. All rights reserved.
You can find the log of this install session at:
/u01/app/oraInventory/logs/cloneActions2016-10-18_07-39-36PM.log
.................................................................................................... 100% Done.
(篇幅原因,有省略……)
The cloning of OraDb11g_home1 was successful.
Please check '/u01/app/oraInventory/logs/cloneActions2016-10-18_07-39-36PM.log' for more details.
(执行脚本略……)
启动原有数据库,此时已经是更新过的目录了。
[oracle@testlife 11.2.0]$ export ORACLE_HOME=$O_ORACLE_HOME
[oracle@testlife 11.2.0]$ env | grep ORACLE_SID
ORACLE_SID=testdb
[oracle@testlife 11.2.0]$ $ORACLE_HOME/bin/lsnrctl start LISTENER
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 18-OCT-2016 19:44:23
The listener supports no services
The command completed successfully
[oracle@testlife 11.2.0]$ $ORACLE_HOME/bin/sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:44:45 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup
ORACLE instance started.
Total System Global Area 3540881408 bytes
Fixed Size 2258320 bytes
Variable Size 855640688 bytes
Database Buffers 2667577344 bytes
Redo Buffers 15405056 bytes
Database mounted.
Database opened.
注意:此时可以完成各个升级补丁需要进行的postinstall操作,更新数据库或者重新编译数据库对象。
[oracle@testlife 11.2.0]$ cd $ORACLE_HOME/rdbms/admin
[oracle@testlife admin]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:46:45 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
SQL> conn / as sysdba
Connected.
SQL> @catbundle.sql psu apply
后续将新创建设置的环境变量重置,重新启动服务器。
[oracle@testlife admin]$ exit
logout
[root@testlife ~]# su - oracle
[oracle@testlife ~]$ env | grep ORA
ORACLE_SID=testdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
[oracle@testlife ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 20:01:08 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
SQL> conn / as sysdba
Connected.
SQL> startup force
ORACLE instance started.
Total System Global Area 3540881408 bytes
Fixed Size 2258320 bytes
Variable Size 855640688 bytes
Database Buffers 2667577344 bytes
Redo Buffers 15405056 bytes
Database mounted.
Database opened.
--升级完成
SQL> select version, comments from dba_registry_history;
VERSION COMMENTS
------------------------------ -----------------------------
11.2.0.4 Patchset 11.2.0.2.0
11.2.0.4 Patchset 11.2.0.2.0
11.2.0.4.160719OJVMPSU RAN jvmpsu.sql
11.2.0.4 PSU 11.2.0.4.160719
11.2.0.4.160719OJVMPSU OJVM PSU post-install
Patch 23177551 applied
6 rows selected
最后,运行一段时间之后,确认升级版本没有问题,就可以将原有的目录删除掉。
[oracle@testlife trace]$ cd ~
[oracle@testlife ~]$ rm -rf `echo $ORACLE_HOME`_to_be_removed
[oracle@testlife ~]$ cd $ORACLE_HOME
[oracle@testlife dbhome_1]$ cd ..
[oracle@testlife 11.2.0]$ ls -l
total 4
drwxr-xr-x 79 oracle dba 4096 Oct 18 19:39 dbhome_1
3、结论
Out of Place升级策略,很大程度上可以确保减少停机时间,原有配置保留和快速回退的要求。在实际场景下,对于升级要有全面的计划和多种备选预案准备,防止出现潜在风险,威胁系统数据安全。
: