是否有工具可以检查远程计算机上 Oracle 11g 数据库的可用性?

发布于 2025-01-04 22:19:02 字数 215 浏览 3 评论 0原文

我正在一台 Linux 机器上配置启动顺序,其中一个启动应用程序依赖于另一台机器上可用的 Oracle 实例。我想要一个 bash 脚本工具来检查 Oracle 实例的可用性。很可能,两个盒子上的电源按钮都会在几秒钟内被按下,并且在我的应用程序尝试连接之前我需要更长的 Oracle 进程来完成。


编辑:除了瘦 JDBC jar 之外,我没有安装 Oracle,并且不希望在客户端上安装。

I'm configuring the boot sequence on a Linux box, and one of the startup applications is dependent upon an Oracle instance being available on a different box. I would like a bash scriptable tool to check the availability of the Oracle instance. Likely, the power button on both boxes will have been hit within seconds of each other, and I need the longer Oracle process to finish before my application attempts to connect.


EDIT: I don't have Oracle installed other than the thin JDBC jar, and would prefer not to on the client box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2025-01-11 22:19:02

确保远程数据库已启动、侦听器已启动以及数据库已正确注册到侦听器的唯一方法是实际建立连接。您可以使用 SQL*Plus 实用程序(假设 Oracle 客户端安装在运行应用程序的 Linux 机器上)来尝试建立连接。类似

创建一个文件 check_db_up.sql

whenever sqlerror exit 1;
connect username/password@<<TNS alias>>
select 1 from dual;
exit 0;

然后在 bash shell 脚本中调用此脚本并查看返回代码

sqlplus /nolog @check_db_up.sql

如果返回 1,则存在错误并且数据库未启动。如果返回 0,则数据库已启动并接受连接。

The only way to be sure that the remote database is up and that the listener is up and that the database is registered with the listener properly would be to actually make a connection. You could use the SQL*Plus utility (assuming the Oracle client is installed on the linux box your application runs on) to attempt to make a connection. Something like

Create a file check_db_up.sql

whenever sqlerror exit 1;
connect username/password@<<TNS alias>>
select 1 from dual;
exit 0;

Then invoke this script in your bash shell script and look at the return code

sqlplus /nolog @check_db_up.sql

If that returns 1, there was an error and the database isn't up. If it returns a 0, the database is up and accepting connections.

妖妓 2025-01-11 22:19:02

您可以编写一个简单的脚本,使用 for 循环来使用 nc 检查 oracle 端口。像这样:

#!/bin/bash
for i in `seq 1 10`
do
    nc oracle_server oracle_port -w 2
    res=$?
    if [ $res -eq 0 ] ; then
        echo "OK"
        exit 0
    else
        echo "Sleeping for 5 second and retry"
        sleep 5
    fi
done

您可以自定义循环迭代次数和等待时间(-w 2sleep 5)。

You can write a simple script that uses a for loop to check for oracle port using nc. Something like this:

#!/bin/bash
for i in `seq 1 10`
do
    nc oracle_server oracle_port -w 2
    res=$?
    if [ $res -eq 0 ] ; then
        echo "OK"
        exit 0
    else
        echo "Sleeping for 5 second and retry"
        sleep 5
    fi
done

You can customize the number of loop iterations and waiting times (-w 2 and sleep 5).

听闻余生 2025-01-11 22:19:02

首先要尝试的是 tnsping,它会告诉您侦听器是否正在运行。如果侦听器正在运行,您可以尝试连接到实例,然后运行,例如,select 1 from Dual。您可以使用sqlplus在命令行上执行此操作。根据您的 Oracle 数据库的配置方式(以及您购买的选项),SNMP 也可能是一个选项。

您还可以让您的应用程序更好地处理尚未启动的数据库(例如,休眠 10 秒并重试)。

另外,您可能需要考虑使用开关式 PDU(例如 APC 的 PDU)来控制电源恢复后机器启动的顺序。如果您想要其他系统管理员答案(因为他们都解决了这个问题),我建议在 Serverfault 上询问。

回应您的编辑:

如果您不想安装 Oracle 客户端,那么您将没有 sqlplus 或 tnsping。相反,只需编写一个简单的 Java 程序来尝试使用 JDBC 瘦驱动程序连接到数据库。如果可以连接,请尝试从双中选择 1。如果有效,那么 Oracle 就成功了。如果您遇到失败或异常,那么它就没有启动。

The first thing to try is tnsping which will tell you if the listener is running. If the listener is running, you can just try and connect to the instance, and run e.g., select 1 from dual. You can do that on the command line using sqlplus. Depending on how your Oracle database is configured (and which options you purchased), SNMP may also be an option.

You could also have your app better handle the database not being up yet (but sleeping 10 seconds and trying again, for example).

Also, you may want to consider using a switched PDU (e.g. like one from APC) to control the order machines are booted after the power comes back. If you want other sysadmin answers (as they've all solved this problem), I suggest asking on Serverfault.

in response to your edit:

If you don't want to install the Oracle Client, then you won't have sqlplus or tnsping. Instead, just write a trivial Java program to attempt to connect to the database using the JDBC thin drivers. If you can connect, try the select 1 from dual. If that works, then Oracle is up. If you instead get failures or exceptions, then its not up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文