需要跨多个服务器以及多个oracle实例查询数据库的情形。比如我们需要查看当前所有生产环境数据库上open_cursor参数的值以便考虑是
在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