Linux/Unix shell脚本跨服务器跨实例执行SQL
Linux/Unix shell脚本跨服务器跨实例执行SQL
在DB运维的过程中,难免碰到需要跨多个服务器以及多个Oracle实例查询数据库的情形。比如我们需要查看当前所有生产环境数据库上open_cursor参数的值以便考虑是否需要增加。而需要查看的数据库服务器上有多个实例,同时又存在多个数据库服务器,这样子下来,几十个DB,上百个的情形一个个查,那得把人给累死。那就像点办法撒,写个shell脚本,轮巡所有服务器及服务器上的所有实例。
1、使用ssh-keygen生成密钥实现快速登陆
[python] 要跨服务器自动执行脚本,得需要实现免密码自动登陆,然后才能在多个服务器之间跳转,因此我们首先需要生成登陆密钥。 生存登陆密钥需要三个步骤,在本地机器创建密钥,复制公钥要远程主机,将公钥追加到远程主机的authorized_keys 下面是一个操作示例: oracle@linux1:~> mkdir ~/.ssh #首先在本地创建.ssh目录并赋予权限 oracle@linux1:~> chmod 700 ~/.ssh oracle@linux1:~> ssh-keygen -t rsa #使用ssh-keygen生成密钥对,也可以使用dsa方式 Generating public/private rsa key pair. Enter file in which to save the key (/users/oracle/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /users/oracle/.ssh/id_rsa. Your public key has been saved in /users/oracle/.ssh/id_rsa.pub. The key fingerprint is: 01:c8:48:01:f2:3d:a7:b4:cd:4a:9c:10:2d:ba:ef:4e oracle@linux1 oracle@linux2:~> mkdir ~/.ssh #远程服务器创建.ssh目录并赋予权限 oracle@linux2:~> chmod 700 ~/.ssh oracle@linux1:~> scp ~/.ssh/id_rsa.pub 172.168.1.196:~/.ssh #复制公钥到远程服务器,即机器linux2 The authenticity of host '172.168.1.196 (172.168.1.196)' can't be established. RSA key fingerprint is 08:3d:69:80:85:1d:ce:57:32:e0:72:e0:38:66:0c:36. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.168.1.196' (RSA) to the list of known hosts. Password: id_rsa.pub 100% 393 0.4KB/s 00:00 oracle@linux2:~> cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys #将公钥追加到远程服务器的authorized_keys oracle@linux1:~> ssh 172.168.1.196 date #验证是否需要输入密码 Thu Aug 22 10:50:47 HKT 2013
2、使用tnsnames方式轮巡多服务器及实例
[python] #下面是通过tnsnames方式进行轮巡的shell脚本,也就是仅仅需要从远程服务器获取/etc/oratab下的所有实例即可 #需要注意的: # a. 确保本地环境存在到所有远程服务器的tnsnames连接字符串,如果没有也没有关系,会收到错误 # b. 需要所有被轮巡数据库的用户及密码并具有相应的权限 # c. 如果所需要执行的sql属于比较复杂的情形,我们可以将sql语句写入到一个单独的文件,然后调用 oracle@linux1:~> more query_multi_inst_tns.sh # ------------------------------------------------------------------------------------+ # Script Name: query_multi_inst_tns.sh | # Desc: This script login to different remote host where define in known_host file, | # after that look through oratab and execute SQL for each instance | # in local machine by tnsnames. | # Req: Configure a secure shell by ssh-keygen to all remote host | # Add Oracle Net string to local tnsnames.ora for each remote instance | # Author : Robinson | # Blog : http://blog.csdn.net/robinson_0612 | # ------------------------------------------------------------------------------------+ #!/bin/bash # -------------------------------------------- # Set environment vairable and define variable # -------------------------------------------- if [ -f ~/.bash_profile ]; then . ~/.bash_profile fi ORATAB=/etc/oratab RHOST=~/.ssh/known_hosts LOGFILE=/users/robin/dba_scripts/custom/log/query_multi_inst.log # ------------------------------- # take a loop in each hostname # ------------------------------- { for host in `cat $RHOST | awk '{print $1}'` do echo "************************************" echo "Current host is $host." echo "************************************" echo "" # --------------------------- # take a loop in ORATAB file # --------------------------- for db in `ssh $host cat $ORATAB |egrep ':N|:Y'|grep -v \* | grep -v \# |cut -d ":" -f1` do echo "------------------------------------" echo "Current database is $db. " echo "------------------------------------" $ORACLE_HOME/bin/sqlplus -S usr/passwd@$db <<EOF col name format a30 col value format a20 select name,value from v\$parameter where name='open_cursors'; exit EOF done done }|tee -a ${LOGFILE} 2>&1 exit
3、直接在远程服务器环境轮巡所有实例
[python] #下面是直接在远程主机环境执行SQL并轮巡的shell脚本,这种方式可以用于没有或不知道数据库用户及密码的情形 #需要注意的: # a. 该方式使用了基于操作系统用户身份验证,应确保支持该方式 # b. 如果需要执行的SQL语句比较复杂,直接嵌套的shell脚本也复杂。如果写成.sql文件,每一个服务器需要存在一个副本 oracle@linux1:~> more query_multi_inst_notns.sh #!/bin/bash # ------------------------------------------------------------------------------------+ # Script Name: query_multi_inst_notns.sh | # Desc: This script login to different remote host where define in known_host file, | # after that look through oratab and execute SQL in each remote instance. | # Req: Configure a secure shell by ssh-keygen to all remote host | # Author : Robinson | # Blog : http://blog.csdn.net/robinson_0612 | # ------------------------------------------------------------------------------------+ # # -------------------------------------------- # Set environment vairable and define variable # -------------------------------------------- if [ -f ~/.bash_profile ]; then . ~/.bash_profile fi ORATAB=/etc/oratab RHOST=~/.ssh/known_hosts LOGFILE=/users/robin/dba_scripts/custom/log/query_multi_inst_notns.log # ------------------------------- # take a loop in each hostname # ------------------------------- { for host in `cat $RHOST | awk '{print $1}'` do echo "************************************" echo "Current host is $host." echo "************************************" echo "" # --------------------------- # take a loop in ORATAB file # --------------------------- for db in `ssh $host cat $ORATAB |egrep ':N|:Y'|grep -v \* | grep -v \# |cut -d ":" -f1` do echo "------------------------------------" echo "Current database is $db. " echo "------------------------------------" home=`ssh $host cat $ORATAB |egrep ':N|:Y'|grep -v \* | grep -v \# | grep $db |cut -d ":" -f2` ssh $host " export ORACLE_SID=$db export ORACLE_HOME=${home} ${ORACLE_HOME}/bin/sqlplus -S / as sysdba <<EOF col name format a30 col value format a20 select name,value from v\\"\$"parameter
- 更多SQLServer疑问解答:
- 配置MSSQL复制指定快照文件夹提示:不是有效的路径或文件名
- 详细解读varchar和Nvarchar区别
- SQL SERVER 2005 同步复制技术
- 进程未能大容量复制到表 解决方法
- MSSql实例教程:MSSql数据库同步
- SQLServer2000同步复制技术实现(分发和订阅)
- sqlserver
- SQLSERVER
- 测试 sqlserver 最大用户数连接
- 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的
- sqlserver2008的安装问题。
- 为什么安装的SQL server 2008我的没有MSSQLSERVER协议啊?
- 怎样把exel表里的数据复制到sqlserver表里
- sqlserver数据库主键和外键问题
- sqlserver 中的左表连接查询和右表连接查询有啥不同?有什么用?