对开发者和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)新特性之运行脚本的更多相关文章

  1. Oracle12cr1新特性之容器数据库(CDB)和可插拔数据库(PDB) 的启动和关闭

    Oracle12c中引入的多宿主选项(multitenant option)允许一个容器数据库容纳多个独立的可插拔数据库(PDB).本文将说明如何启动和关闭容器数据库(CDB)和可插拔数据库(PDB) ...

  2. oracle12c新特点之可插拔数据库(Pluggable Database,PDB)

    1.    12c PDB新特点的优势 1)    可以把多个PDB集成进一个平台. 2)    可以快速提供一个新的PDB或一个已有PDB的克隆. 3)    通过拔插技术,可以快速把存在的数据库重 ...

  3. Oracle12c中配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB)

    Oracle12c中的多宿主选项允许一个容器数据库(CDB)容纳多个独立的可插拔数据库(PDB).本文将展示如何配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB).1. 配置CDB中的实例 ...

  4. Oracle12c中多宿主环境(CDB&amp;PDB)的数据库触发器(Database Trigger)

    Oracle12c中可插拔数据库(PDBs)上的多宿主数据库触发器 随着多宿主选项的引入,数据库事件触发器可以在CDB和PDB范围内创建. 1.   触发器范围 为了在CDB中创建数据库事件触发器,需 ...

  5. Oracle可插拔数据库的jdbc连接串写法

    我在服务器上部署某个第三方系统的数据库的时候,服务器数据库版本为oracle 12c.我采用的方式是新建了一个实例.访问正常. 后来项目的负责人告诉我,oracle12C支持所谓的可插拔数据库.可插拔 ...

  6. Oracle 12C 新特性 - “可插拔数据库”功能

    Oracle 12C加入了一个非常有新意的功能"可插拔数据库"特性,实现了数据库(PDB)在"容器"(CDB)上的拔功能,既能提高系统资源的利用率,也简化大面积 ...

  7. 如何连接oracle 12c可插拔数据库

    启动根容器:[oracle@eric ~]$ export ORACLE_SID=cup[oracle@eric ~]$ sqlplus / as sysdbaSQL*Plus: Release 12 ...

  8. Oracle 12c的可插拔数据库PDB

    1. 默认安装之后会有一个可插拔数据库:pdborcl 2. 启动根容器: [oracle@eric ~]$ export ORACLE_SID=orcl [oracle@eric ~]$ sqlpl ...

  9. 12c可插拔数据库CDB与PDB管理总结

    12c可插拔数据库CDB与PDB管理总结 创建pdb1.直接创建 CREATE PLUGGABLE DATABASE pdb2 ADMIN USER boswll IDENTIFIED BY orac ...

随机推荐

  1. 手机广告投放(phone advertising)唯一标识

    手机标示,为了识别用户,方面advertising. 使用设备id,相当于暴露用户隐私.慢慢已不允许使用.. (长时间跟踪用户无异于暴露用户隐私,虽然大量数据适合兴趣建模,广告个性化推荐.但复杂多样会 ...

  2. linux,shell脚本set -x的意思

    set -x a=10 命令执行结果: + a=10 echo $a + echo 10 10 set指令能设置所使用shell的执行方式,可依照不同的需求来做设置 -a 标示已修改的变量,以供输出至 ...

  3. 视音频编解码学习工程:JPEG分析器

    =====================================================视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习工 ...

  4. hive发杂数据结构的使用,struct,array,map

    一个简单介绍http://jimi68.iteye.com/blog/980573 一般的复杂介绍:http://songpo-ath-taobao-com.iteye.com/blog/140513 ...

  5. 【Qt编程】Qt版扫雷

    学习要学会举一反三.在以前的<用matlab扫扫雷>一文中,我用matlab简单的编写了一个扫雷小程序.当然,与Windows自带的扫雷程序自然是不敢相提并论.今天我就用c++来写个扫雷程 ...

  6. com.android.dex.DexException: Multiple dex files define(jar包重复引用) 错误解决

    前段时间开始转入Android studio,不料果真使用时候遇到些错误,在此记下! 出现这个错误往往是在libs目录下有个jar包,然后在gradle文件中又引用了,即: 共同引用了. 解决方法: ...

  7. kettle简介(整体架构,运行方式,使用方法)

    项目负责人Matt的说法:把各种数据放到一个壶里,然后呢,以一种你希望的格式流出.呵呵,外国人都很有联想力.看了提供的文档,然后对发布程序的简单试用后,可以很清楚得看到Kettle的四大块: Chef ...

  8. ORACLE EBS AP invoice 到付款的数据流

    --1.Invoice创建时生成数据如下表 --Invoice主表 SELECT * FROM AP_INVOICES_ALL A WHERE A.INVOICE_NUM = '20111213001 ...

  9. linux内核 container_ofC语言之应用

    之前在剖析内核链表的文章中就有说到这个 container_of宏展开后的应用技巧. //offset(struct list , list);----->展开后((size_t) & ...

  10. linux服务搭建----NFS服务搭建

    nfs  服务     //配置脚本         /etc/exports     //服务器  发布目录     1> 确定需要发布的目录(想要共享的目录)     例如:      想发 ...