首页 / 数据库 / MySQL / Oracle 12c中多宿主容器数据库(CDBs)和可插拔数据库(PDBs)新特性之运行脚本
对开发者和DBA们来说,对shell脚本批量任务的影响成了多宿主选项带来的最大改变之一。因为多宿主环境通过服务来连接到可插拔数据库,因此,依靠CRON和OS认证成了换成多宿主环境后的一个最大问题。本文提供了一些办法来解决之前shell脚本工作在多宿主环境的问题。1. 设置容器对于那些工作在容器级的DBA脚本来说,用"/ AS SYSDBA"就可以像之前一样工作。当你在可插拔数据库内运行脚本时,就会出现问题。解决这个问题的最简单办法就是继续用"/ asSYSDBA"连接,但在脚本中用ALTER SESSION SET CONTAINER命令设置容器。sqlplus / as sysdba <<EOFALTER SESSION SET CONTAINER = pdb1;-- 和之前一样运行任务SHOW CON_NAME;EXIT;EOF为了让脚本更通用,把PDB名当做参数。将下面的脚本存为"set_container_test.sh".sqlplus / as sysdba <<EOFALTER SESSION SET CONTAINER = $1;--像之前一样运行任务SHOW CON_NAME;EXIT;EOF把PDB名作为第一个参数运行脚本显示,设置的容器是对的。$ chmod u+x set_container_test.sh$ ./set_container_test.sh pdb1SQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:48:51 2014Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to:Oracle Database 12c Enterprise EditionRelease 12.1.0.1.0 - 64bit ProductionWith the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing optionsSQL> SQL>Session altered.SQL> SQL> SQL>CON_NAME------------------------------PDB1SQL> SQL> Disconnected from OracleDatabase 12c Enterprise Edition Release 12.1.0.1.0 - 64bit ProductionWith the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options$2. TWO_TASK方法用TWO_TASK环境变量是连接到特定用户的一个浅显的方法,可惜的是,用"/ ASSYSDBA"连接方法行不通。$ export TWO_TASK=pdb1$ sqlplus / as sysdbaSQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:54:34 2014Copyright (c) 1982, 2013, Oracle. All rights reserved.ERROR:ORA-01017: invalid username/password; logondeniedEnter user-name:用确定的用户名和口令结合TWO_TASK方法,能像之前一样正常工作。$ export TWO_TASK=pdb1$ sqlplus test/testSQL*Plus: Release 12.1.0.1.0 Production onFri Apr 18 16:57:46 2014Copyright (c) 1982, 2013, Oracle. All rights reserved.Last Successful login time: Wed Apr 02 201410:05:22 +01:00Connected to:Oracle Database 12c Enterprise EditionRelease 12.1.0.1.0 - 64bit ProductionWith the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing optionsSQL> SHOW CON_NAME;CON_NAME------------------------------PDB1SQL>也许你并不希望在脚本中包含确定的用户名和密码,但如果增加一个指向连接的服务或使用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 = TRUESSL_CLIENT_AUTHENTICATION = FALSESSL_VERSION = 0创建一个钱包来存储认证信息。Oracle11gR2之后,通过orapki很好的实现了该功能,如果将钱包拷到其他机器,将会阻止自动登录。$ mkdir -p /u01/app/oracle/wallet$ orapki wallet create -wallet"/u01/app/oracle/wallet" -pwd "mypassword"-auto_login_localOracle Secret Store Tool : Version 12.1.0.1Copyright (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 testOracle Secret Store Tool : Version 12.1.0.1Copyright (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_testSQL*Plus: Release 12.1.0.1.0 Production onSat Apr 19 10:19:38 2014Copyright (c) 1982, 2013, Oracle. All rights reserved.Last Successful login time: Sat Apr 19 201410:18:52 +01:00Connected to:Oracle Database 12c Enterprise Edition Release12.1.0.1.0 - 64bit ProductionWith the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing optionsSQL> SHOW USERUSER is "TEST"SQL> SHOW CON_NAMECON_NAME------------------------------PDB1SQL>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 } ... 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 __[]. -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 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"脚本的例子。$ . oraenvORACLE_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"$更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址