oracle 使用 UNIX shell 脚本建立数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31941666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:55:19  来源:igfitidea点击:

to establish database connection using UNIX shell script

oracleshellunix

提问by Neethu Shaji

I am new to shell scripting and have less or no idea on this. I have to read a db.properties file which has the database connection details i.e. to which db to connect. Then i have to establish a connection to that database and perform an operation to check the current time.

我是 shell 脚本的新手,对此知之甚少或一无所知。我必须读取一个 db.properties 文件,其中包含数据库连接的详细信息,即要连接到哪个 db。然后我必须建立与该数据库的连接并执行操作以检查当前时间。

Below is my db.properties file :-

以下是我的 db.properties 文件:-

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@171.01.187.94:1532:DEV
userName=abc
password=abc

Below is my script to call the db.properties file :-

下面是我调用 db.properties 文件的脚本:-

#!/bin/bash

file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " ${userName}
  echo "user password = " ${password}
  echo "url  = " ${url}

  sqlplus -S ${userName}/${password}@${url}

else
    echo "$file not found."
fi

But i am getting the below error :-

但我收到以下错误:-

ERROR: ORA-12154: TNS:could not resolve the connect identifier specified

错误:ORA-12154:TNS:无法解析指定的连接标识符

Could anyone please help on the above issue ?

任何人都可以帮助解决上述问题吗?

回答by Daniel Vukasovich

Don't worry about tnsnames.ora definition, you have all information needed to establish a connection using sqlnet.

不要担心 tnsnames.ora 的定义,您已经拥有使用 sqlnet 建立连接所需的所有信息。

Modify your database.properties file as follows:

修改您的 database.properties 文件,如下所示:

driverClassName=oracle.jdbc.driver.OracleDriver
url='(description=(address_list=(address=(protocol=TCP)(host=171.01.187.94)(port=1532)))(connect_data=(service_name=DEV)))'
userName=abc
password=abc

And that's it. You don't need to change your script.

就是这样。您无需更改脚本。

#!/bin/bash

file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " ${userName}
  echo "user password = " ${password}
  echo "url  = " ${url}

sqlplus -S ${userName}/${password}@${url}

else
    echo "$file not found."
fi

Note: I assumed that DEV is the database service name, if it's the database SID just modify configuration string as:

注意:我假设 DEV 是数据库服务名称,如果是数据库 SID 只需将配置字符串修改为:

driverClassName=oracle.jdbc.driver.OracleDriver
url='(description=(address_list=(address=(protocol=TCP)(host=171.01.187.94)(port=1532)))(connect_data=(sid=DEV)))'
userName=abc
password=abc

Regards

问候

回答by user9491210

please try / in between port and SID name. username/password@host:port/SID

请尝试 / 在端口和 SID 名称之间。用户名/密码@主机:端口/SID