首页 / 数据库 / MySQL / 使用Out of Place升级策略进行Oracle Patch操作
数据库升级、打补丁是我们经常面对的日常工作内容。在正常情况下,两个因素是我们必须要考虑的问题:停机时间窗和回退方案。就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 ProductionPL/SQL Release 11.2.0.4.0 - ProductionCORE 11.2.0.4.0 ProductionTNS for Linux: Version 11.2.0.4.0 - ProductionNLSRTL Version 11.2.0.4.0 – Production环境变量情况如下:[oracle@testlife OPatch]$ env | grep ORAORACLE_SID=testdbORACLE_BASE=/u01/app/oracleORACLE_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 lsinventoryOracle Interim Patch Installer version 11.2.0.3.10Copyright (c) 2016, Oracle Corporation. All rights reserved.Oracle Home : /u01/app/oracle/product/11.2.0/dbhome_1Central Inventory : /u01/app/oraInventory from : /u01/app/oracle/product/11.2.0/dbhome_1/oraInst.locOPatch version : 11.2.0.3.10OUI 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 ORAC_ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1_cloneORACLE_OWNER_GROUP=oracle:dbaORACLE_SID=testdbORACLE_BASE=/u01/app/oracleC_ORACLE_HOME_NAME=cloneO_ORACLE_HOME_NAME=OraDb11g_home1ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1O_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 dbhomedrwxr-xr-x. 75 oracle oinstall 4096 Oct 18 17:52 dbhome_1drwxr-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 deniedtar: ./bin/nmb: Cannot open: Permission deniedtar: ./bin/nmhs: Cannot open: Permission deniedtar: 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 PassedPreparing to launch Oracle Universal Installer from /tmp/OraInstall2016-10-18_06-13-35PM. Please wait ...Oracle Universal Installer, Version 11.2.0.4.0 ProductionCopyright (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 successfulLinking in progress (Tuesday, October 18, 2016 6:14:00 PM CST)Link successfulSetup in progress (Tuesday, October 18, 2016 6:14:35 PM CST)Setup successfulEnd 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.shTo 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.shCheck /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 -ltotal 32drwxr-xr-x 13 oracle oinstall 4096 Jul 20 19:30 23054359drwxr-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 applyOracle Interim Patch Installer version 11.2.0.3.10Copyright (c) 2016, Oracle Corporation. All rights reserved.Oracle Home : /u01/app/oracle/product/11.2.0/dbhome_1_cloneCentral Inventory : /u01/app/oraInventory from : /u01/app/oracle/product/11.2.0/dbhome_1_clone/oraInst.locOPatch version : 11.2.0.3.10OUI version : 11.2.0.4.0(中间略...)两个补丁均完成。暂停原有服务,进行目录切换。[oracle@testlife 23177551]$ export ORACLE_HOME=$O_ORACLE_HOME[oracle@testlife 23177551]$ env | grep ORACLE_SIDORACLE_SID=testdb[oracle@testlife 23177551]$ sqlplus /nologSQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:33:52 2016Copyright (c) 1982, 2013, Oracle. All rights reserved.SQL> conn / as sysdbaConnected.SQL> shutdown immediate;Database closed.Database dismounted.ORACLE instance shut down.SQL> exitDisconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing options[oracle@testlife 23177551]$ $ORACLE_HOME/bin/lsnrctl stopLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 18-OCT-2016 19:34:32Copyright (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.shStarting Oracle Universal Installer...Checking swap space: must be greater than 500 MB. Actual 7967 MB PassedThe inventory pointer is located at /etc/oraInst.locThe 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 -ltotal 8drwxr-xr-x 79 oracle dba 4096 Oct 18 19:31 dbhome_1_clonedrwxr-xr-x. 75 oracle oinstall 4096 Oct 18 17:52 dbhome_1_to_be_removedDetach新的目录对象。[oracle@testlife 11.2.0]$ export ORACLE_HOME=$C_ORACLE_HOME[oracle@testlife 11.2.0]$ $ORACLE_HOME/oui/bin/detachHome.shStarting Oracle Universal Installer...Checking swap space: must be greater than 500 MB. Actual 7967 MB PassedThe inventory pointer is located at /etc/oraInst.locThe 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 PassedPreparing to launch Oracle Universal Installer from /tmp/OraInstall2016-10-18_07-39-36PM. Please wait ...Oracle Universal Installer, Version 11.2.0.4.0 ProductionCopyright (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_SIDORACLE_SID=testdb[oracle@testlife 11.2.0]$ $ORACLE_HOME/bin/lsnrctl start LISTENERLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 18-OCT-2016 19:44:23The listener supports no servicesThe command completed successfully[oracle@testlife 11.2.0]$ $ORACLE_HOME/bin/sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:44:45 2016Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to an idle instance.SQL> startupORACLE instance started.Total System Global Area 3540881408 bytesFixed Size 2258320 bytesVariable Size 855640688 bytesDatabase Buffers 2667577344 bytesRedo Buffers 15405056 bytesDatabase mounted.Database opened.注意:此时可以完成各个升级补丁需要进行的postinstall操作,更新数据库或者重新编译数据库对象。[oracle@testlife 11.2.0]$ cd $ORACLE_HOME/rdbms/admin[oracle@testlife admin]$ sqlplus /nologSQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 19:46:45 2016Copyright (c) 1982, 2013, Oracle. All rights reserved.SQL> conn / as sysdbaConnected.SQL> @catbundle.sql psu apply后续将新创建设置的环境变量重置,重新启动服务器。[oracle@testlife admin]$ exitlogout[root@testlife ~]# su - oracle[oracle@testlife ~]$ env | grep ORAORACLE_SID=testdbORACLE_BASE=/u01/app/oracleORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1[oracle@testlife ~]$ sqlplus /nologSQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 18 20:01:08 2016Copyright (c) 1982, 2013, Oracle. All rights reserved.SQL> conn / as sysdbaConnected.SQL> startup forceORACLE instance started.Total System Global Area 3540881408 bytesFixed Size 2258320 bytesVariable Size 855640688 bytesDatabase Buffers 2667577344 bytesRedo Buffers 15405056 bytesDatabase mounted.Database opened.--升级完成SQL> select version, comments from dba_registry_history;VERSION COMMENTS------------------------------ -----------------------------11.2.0.4 Patchset 11.2.0.2.011.2.0.4 Patchset 11.2.0.2.011.2.0.4.160719OJVMPSU RAN jvmpsu.sql11.2.0.4 PSU 11.2.0.4.16071911.2.0.4.160719OJVMPSU OJVM PSU post-install Patch 23177551 applied6 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 -ltotal 4drwxr-xr-x 79 oracle dba 4096 Oct 18 19:39 dbhome_13、结论Out of Place升级策略,很大程度上可以确保减少停机时间,原有配置保留和快速回退的要求。在实际场景下,对于升级要有全面的计划和多种备选预案准备,防止出现潜在风险,威胁系统数据安全。更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址