在DB运维的过程中,难免碰到需要跨多个服务器以及多个Oracle实例查询数据库的情形。比如我们需要查看当前所有生产环境数据库上open_cursor参数的值以便考虑是否需要增加。而需要查看的数据库服务器上有多个实例,同时又存在多个数据库服务器,这样子下来,几十个DB,上百个的情形一个个查,那得把人给累死。那就像点办法撒,写个shell脚本,轮巡所有服务器及服务器上的所有实例。见本文的描述。
关于shell脚本中如何调用SQL,可以参考下列链接:
Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell sql 之间传递变量
1、使用ssh-keygen生成密钥实现快速登陆
要跨服务器自动执行脚本,得需要实现免密码自动登陆,然后才能在多个服务器之间跳转,因此我们首先需要生成登陆密钥。
生存登陆密钥需要三个步骤,在本地机器创建密钥,复制公钥要远程主机,将公钥追加到远程主机的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