Oracle12c中多宿主容器數據庫(CDBs)和可插拔數據庫(PDBs)新特性之運行腳本
對開發者和DBA們來說,對shell腳本批量任務的影響成了多宿主選項帶來的最大改變之一。因為多宿主環境通過服務來連接到可插拔數據庫,因此,依靠CRON和OS認證成了換成多宿主環境後的一個最大問題。本文提供了一些辦法來解決之前shell腳本工作在多宿主環境的問題。
1. 設置容器
對於那些工作在容器級的DBA腳本來說,用"/ AS SYSDBA"就可以像之前一樣工作。當你在可插拔數據庫內運行腳本時,就會出現問題。解決這個問題的最簡單辦法就是繼續用"/ asSYSDBA"連接,但在腳本中用ALTER SESSION SET CONTAINER命令設置容器。
sqlplus / as sysdba <<EOF
ALTER SESSION SET CONTAINER = pdb1;
-- 和之前一樣運行任務
SHOW CON_NAME;
EXIT;
EOF
為了讓腳本更通用,把PDB名當做參數。將下面的腳本存為"set_container_test.sh".
sqlplus / as sysdba <<EOF
ALTER SESSION SET CONTAINER = $1;
--像之前一樣運行任務
SHOW CON_NAME;
EXIT;
EOF
把PDB名作為第一個參數運行腳本顯示,設置的容器是對的。
$ chmod u+x set_container_test.sh
$ ./set_container_test.sh pdb1
SQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:48:51 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise EditionRelease 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options
SQL> SQL>
Session altered.
SQL> SQL> SQL>
CON_NAME
------------------------------
PDB1
SQL> SQL> Disconnected from OracleDatabase 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options
$
2. TWO_TASK方法
用TWO_TASK環境變量是連接到特定用戶的一個淺顯的方法,可惜的是,用"/ ASSYSDBA"連接方法行不通。
$ export TWO_TASK=pdb1
$ sqlplus / as sysdba
SQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:54:34 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-01017: invalid username/password; logondenied
Enter user-name:
用確定的用戶名和口令結合TWO_TASK方法,能像之前一樣正常工作。
$ export TWO_TASK=pdb1
$ sqlplus test/test
SQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:57:46 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Last Successful login time: Wed Apr 02 201410:05:22 +01:00
Connected to:
Oracle Database 12c Enterprise EditionRelease 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options
SQL> SHOW CON_NAME;
CON_NAME
------------------------------
PDB1
SQL>
也許你並不希望在腳本中包含確定的用戶名和密碼,但如果增加一個指向連接的服務或使用TWO_TASK環境變量,就可以連接到確定的PDB。
3. 安全的外部口令存儲
Oracle 10g引進了不用顯式提供認證,而是使用安全外部口令存儲來連接數據庫的能力。這種基於服務的方式事實上也會很好的使用PDB環境。
把下面的項放入"$ORACLE_HOME/network/admin/sqlnet.ora" 文件,並確定要求的錢包目錄。
WALLET_LOCATION =
(SOURCE =
(METHOD = FILE)
(METHOD_DATA =
(DIRECTORY = /u01/app/oracle/wallet)
)
)
SQLNET.WALLET_OVERRIDE = TRUE
SSL_CLIENT_AUTHENTICATION = FALSE
SSL_VERSION = 0
創建一個錢包來存儲認證信息。Oracle11gR2之後,通過orapki很好的實現了該功能,如果將錢包拷到其他機器,將會阻止自動登錄。
$ mkdir -p /u01/app/oracle/wallet
$ orapki wallet create -wallet"/u01/app/oracle/wallet" -pwd "mypassword"-auto_login_local
Oracle Secret Store Tool : Version 12.1.0.1
Copyright (c) 2004, 2012, Oracle and/or itsaffiliates. All rights reserved.
Enter password:
Enter password again:
$
然後,在創建一個和TNS別名相關的認證項。參數為"aliasusername password".
$ mkstore -wrl"/u01/app/oracle/wallet" -createCredential pdb1_test test test
Oracle Secret Store Tool : Version 12.1.0.1
Copyright (c) 2004, 2012, Oracle and/or itsaffiliates. All rights reserved.
Enter wallet password:
Create credentialoracle.security.client.connect_string1
$
在"$ORACLE_HOME/network/admin/tnsnames.ora"文件中創建一個和錢包中匹配的別名。
PDB1_TEST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ol6-121.localdomain)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pdb1)
)
)
至此,我們就可以用錢包中的認證項去連接確定的數據庫。
$ sqlplus /@pdb1_test
SQL*Plus: Release 12.1.0.1.0 Production onSat Apr 19 10:19:38 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Last Successful login time: Sat Apr 19 201410:18:52 +01:00
Connected to:
Oracle Database 12c Enterprise Edition Release12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options
SQL> SHOW USER
USER is "TEST"
SQL> SHOW CON_NAME
CON_NAME
------------------------------
PDB1
SQL>
4. 調度器
Oracle12c中的調度器已被增強,以便可以包含基於腳本的任務,這樣,你就可以定義行內腳本,或在文件系統上調用腳本。這些是外部任務的一個變種,但SQL_SCRIPT和BACKUP_SCRIPT任務類型使得處理認證和多宿主環境變得 更加容易。
catcon.pl
當在多宿主環境運行腳本時,DBA遇到的另一個問題是在多個PDBS中運行同樣的腳本。這可以通過前面的方法實現,但Oracle提供的叫"catcon.pl"的PERL模塊也許更加方便。
在多宿主環境中,有些Oracle提供的腳本必須按照特定順序執行,先在CDB$ROOT容器中執行。"catcon.pl" 模塊可以完成它,並且提供確定容器的日誌,這樣,你可以很容易的檢查任務完成情況。
該模塊的完整語法如下,不帶參數運行該模塊會顯示所有的用法。
$ perl catcon.pl
Usage: catcon [-uusername[/password]] [-U username[/password]]
[-d directory] [-l directory]
[{-c|-C} container] [-pdegree-of-parallelism]
[-e] [-s]
[-E { ON |errorlogging-table-other-than-SPERRORLOG } ]
[-g]
-b log-file-name-base
--
{ sqlplus-script [arguments] |--x<SQL-statement> } ...
Optional:
-uusername (optional /password; otherwise prompts for password)
used to connect to the database to run user-supplied scripts or
SQL statements
defaults to "/ as sysdba"
-Uusername (optional /password; otherwise prompts for password)
used to connect to the database to perform internal tasks
defaults to "/ as sysdba"
-ddirectory containing the file to be run
-ldirectory to use for spool log files
-ccontainer(s) in which to run sqlplus scripts, i.e. skip all
Containers not named here; for example,
-c ‘PDB1 PDB2‘,
-Ccontainer(s) in which NOT to run sqlplus scripts, i.e. skip all
Containers named here; for example,
-C ‘CDB PDB3‘
NOTE: -c and -C are mutually exclusive
-pexpected number of concurrent invocations of this script on a given
host
NOTE: this parameter rarely needs to be specified
-esets echo on while running sqlplus scripts
-soutput of running every script will be spooled into a file whose name
will be
<log-file-name-base>_<script_name_without_extension>_[<container_name_if_any>].<default_extension>
-Esets errorlogging on; if ON is specified, default error logging table
will be used, otherwise, specified error logging table (which must
have been created in every Container) will be used
-gturns on production of debugging info while running this script
Mandatory:
-bbase name (e.g. catcon_test) for log and spool file names
sqlplus-script - sqlplus script to run OR
SQL-statement - a statement toexecute
NOTES:
-if --x<SQL-statement> is the first non-option string, it needs to be
preceeded with -- to avoid confusing module parsing options into
assuming that ‘-‘ is an option which that module is not expecting and
about which it will complain
-command line parameters to SQL scripts can be introduced using --p
interactive (or secret) parameters to SQL scripts can be introduced
using --P
For example,
perl catcon.pl ... x.sql ‘--pJohn‘ ‘--PEnter Password for John:‘ ...
$
關於運行Oracle提供的腳本,手冊中使用了在所有容器中運行"catblock.sql"腳本的例子。
$ . oraenv
ORACLE_SID = [cdb1] ?
The Oracle base remains unchanged with value/u01/app/oracle
$ cd $ORACLE_HOME/rdbms/admin/
$ perl catcon.pl -d $ORACLE_HOME/rdbms/admin-b /tmp/catblock_output catblock.sql
$ ls /tmp/catblock_output*
catblock_output0.log catblock_output1.log catblock_output2.log catblock_output3.log
$
第一個輸出文件包含了來自"cdb$root" and "pdb$seed"容器的輸出。最後一個文件包含了該任務的整體狀態輸出信息。中間的其他文件包含了所有用戶自己創建的PDBS的輸出。
"catcon.pl"模塊也能用來在CDB中所有容器中運行查詢。下面的命令在所有容器中運行一個查詢,針對每個容器,其信息將會輸出到名為"/tmp/tbs_files_outputN.log"的文件中。
$ cd $ORACLE_HOME/rdbms/admin/
$ perl catcon.pl -e -b /tmp/tbs_files_output-- --x"SELECT tablespace_name,file_name FROM dba_data_files"
$ ls /tmp/tbs_files_output*
/tmp/tbs_files_output0.log /tmp/tbs_files_output1.log /tmp/tbs_files_output2.log /tmp/tbs_files_output3.log
$
通過"-c"選項和"-C"選項,你可以包含和排除特定的PDBS。下例通過漏掉root 和seed容器來在所有用戶定義的容器中運行一個查詢。
$ rm -f /tmp/tbs_files_output*
$ cd $ORACLE_HOME/rdbms/admin/
$ perl catcon.pl -e -C ‘CDB$ROOT PDB$SEED‘ -b/tmp/tbs_files_output -- --x"SELECT tablespace_name,file_name FROM dba_data_files"
$
Oracle12c中多宿主容器數據庫(CDBs)和可插拔數據庫(PDBs)新特性之運行腳本