在实际得维护工作中,可能需要同时在多个实例中对比同一个查询结果,比如,要得到在运生产库100多套实例得字符集设置,这样得工作用shell可以大大减少人力得支出。本文做简单示例:
1.准备环境,一套19c得cdb,里面有两个pdb,拉起所有pdb和监听。

[oracle@vm1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Jun 13 08:04:07 2020
Version 19.7.0.0.0

Copyright (c) 1982, 2020, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 2516582152 bytes
Fixed Size                  9137928 bytes
Variable Size             822083584 bytes
Database Buffers         1677721600 bytes
Redo Buffers                7639040 bytes
Database mounted.
Database opened.
SQL> alter pluggable database all open;

Pluggable database altered.

SQL> !lsnrctl start

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 13-JUN-2020 08:04:42

Copyright (c) 1991, 2019, Oracle.  All rights reserved.

Starting /app/oracle/db/19.3.0/home1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 19.0.0.0.0 - Production
System parameter file is /app/oracle/db/19.3.0/home1/network/admin/listener.ora
Log messages written to /app/oracle/db/diag/tnslsnr/vm1/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vm1)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=vm1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date                13-JUN-2020 08:04:43
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /app/oracle/db/19.3.0/home1/network/admin/listener.ora
Listener Log File         /app/oracle/db/diag/tnslsnr/vm1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vm1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
The listener supports no services
The command completed successfully

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ZHYV                           READ WRITE NO
         4 ZHYU                           READ WRITE NO

SQL> !lsnrctl status

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 13-JUN-2020 08:06:48

Copyright (c) 1991, 2019, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=vm1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date                13-JUN-2020 08:04:43
Uptime                    0 days 0 hr. 2 min. 4 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /app/oracle/db/19.3.0/home1/network/admin/listener.ora
Listener Log File         /app/oracle/db/diag/tnslsnr/vm1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vm1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "86b637b62fdf7a65e053f706e80a27ca" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "a5930a2320e01e62e0536538a8c08fdd" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "a5a4fb52bba60ad1e0536538a8c0fd8b" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "ora19c" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "ora19cXDB" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "zhyu" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
Service "zhyv" has 1 instance(s).
  Instance "ora19c", status READY, has 1 handler(s) for this service...
The command completed successfully

2.准备脚本

[oracle@vm1 tmp]$ cd dbs
[oracle@vm1 dbs]$ ll
total 4
-rw-rw-r-- 1 oracle oracle 412 Jun 13 00:45 check.sh    ------------------执行得shell
drwxrwxr-x 2 oracle oracle  40 Jun 13 00:50 dbs            ------------------需要查询得实例列表
drwxrwxr-x 2 oracle oracle  40 Jun 13 00:48 res             ------------------查询结果存放目录
drwxrwxr-x 2 oracle oracle 220 Apr  5 22:12 sqls           ------------------查询语句存放目录

3.脚本详解
dbs下有个实列列表,用简单连接得方式,是明文得,有兴趣得朋友可以改成加密得:

[oracle@vm1 dbs]$ cd dbs
[oracle@vm1 dbs]$ ls
db_con_103  dbs_con1
[oracle@vm1 dbs]$ cat dbs_con1
dbsnmp/[email protected]:1521/zhyu
dbsnmp/[email protected]:1521/zhyv
[oracle@vm1 dbs]$ 

res下是存放sql语句得查询结果得,每个sql一个结果文件,以日期+sql名字为前缀:

[oracle@vm1 dbs]$ cd res
[oracle@vm1 res]$ ls
20200613.character.sql.log
[oracle@vm1 res]$ ll
total 4
-rw-rw-r-- 1 oracle oracle 82 Jun 13 00:48 20200613.character.sql.log
[oracle@vm1 res]$ cat 20200613.character.sql.log

192.168.56.101:1521/zhyu       AL32UTF8

192.168.56.101:1521/zhyv       AL32UTF8

sql下存放着常用得或者自定义得sql语句:

[oracle@vm1 dbs]$ cd sqls
[oracle@vm1 sqls]$ ls
character.sql  ckbingdu.sql  db_file_pct.sql  info_archived_log.sql  info_asm_diskgroup.sql  info_db_user.sql  info_redo_swith.sql  instance.sql  sess_cnt.sql
[oracle@vm1 sqls]$ cat character.sql
SET ECHO        OFF
SET FEEDBACK    off
SET HEADING     off
SET LINESIZE    180
SET PAGESIZE    2000
SET TIMING      OFF
SET TRIMSPOOL   ON
SET VERIFY      OFF
CLEAR COLUMNS BREAKS COMPUTES
COLUMN db_con_name                    FORMAT a30      HEADING "Database_connect"
COLUMN character_set                  FORMAT a80      HEADING "Character_Set"

select '&dn' db_con_name,value character_set from nls_database_parameters where parameter='NLS_CHARACTERSET';

check.sh是一个调用脚本,执行时先读取实例列表,再选择使用哪一个sql语句,然后循环查询,把结果都写到一个日志上面:

[oracle@vm1 dbs]$ cat check.sh
#!/bin/bash
source ~/.bash_profile
time=`date '+%Y%m%d'`
dbc_dir=/tmp/dbs/dbs
sql_dir=/tmp/dbs/sqls
res_dir=/tmp/dbs/res
ls $sql_dir
echo -n "enter sql you want:"
read sql
rm -f $res_dir/$sql.log
for i in `cat $dbc_dir/dbs_con1`
do
dn=`echo $i|awk -F [@] '{print $2}'`
echo $dn
sqlplus -S $i<

4.运行过如下:

[oracle@vm1 dbs]$ sh check.sh
character.sql  ckbingdu.sql  db_file_pct.sql  info_archived_log.sql  info_asm_diskgroup.sql  info_db_user.sql  info_redo_swith.sql  instance.sql  sess_cnt.sql
enter sql you want:character.sql
192.168.56.101:1521/zhyu
192.168.56.101:1521/zhyv
[oracle@vm1 dbs]$ cd res
[oracle@vm1 res]$ ls
20200613.character.sql.log
[oracle@vm1 res]$ cat 20200613.character.sql.log

192.168.56.101:1521/zhyu       AL32UTF8

192.168.56.101:1521/zhyv       AL32UTF8

更多精彩文章请关注微信公众号: godba