通过 Oracledb Driver 使用 Nodejs 连接到远程 Oracle DB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43645841/
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
Connecting to a remote Oracle DB with Nodejs through Oracledb Driver
提问by Brooke Clonts
Hey I'm really trying to figure out how to connect to a remote Oracle test DB and I have no experience in Java. So if I could get your help, I would be forever grateful.
嘿,我真的想弄清楚如何连接到远程 Oracle 测试数据库,但我没有 Java 经验。所以如果我能得到你的帮助,我将永远感激不尽。
I have a remote test database I'm trying to connect to and I have a jdbc connection with an old style SID. According to this link: https://github.com/oracle/node-oracledb/blob/master/doc/api.md#notjdbc, I'm supposed to create a tnsnames.ora file to put the connection in, like so:
我有一个正在尝试连接的远程测试数据库,并且我有一个带有旧式 SID 的 jdbc 连接。根据这个链接:https: //github.com/oracle/node-oracledb/blob/master/doc/api.md#notjdbc,我应该创建一个 tnsnames.ora 文件来放置连接,就像这样:
tnsnames.ora:
tnsnames.ora:
appDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))
(CONNECT_DATA =
(SID = ORCL)
)
)
and then I'm supposed to reference it in my node server.js file, like so
然后我应该在我的节点 server.js 文件中引用它,就像这样
server.js:
服务器.js:
const oracledb = require('oracledb');
oracledb.getConnection(
{
user : process.env.ORACLE_USER,
password : process.env.ORACLE_PASSWORD,
connectString : "appDB"
},
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}
connection.execute(
"SELECT * " +
"FROM BOS_course",
function(err, result)
{
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.rows);
doRelease(connection);
});
});
module.exports = {
oracledb
};
However, I have no idea where I'm supposed to put the tnsnames.ora file. I found this link online $ORACLE_HOME/network/admin
but I don't know what it's referring to. I only have limited access to the database. Excuse my Java ignorance. How can I use this connection in my node app, which lives completely separate from the database? How does my app know what "appDB" is and how to find it in the tnsnames.ora file?
但是,我不知道应该将 tnsnames.ora 文件放在哪里。我在网上找到了这个链接,$ORACLE_HOME/network/admin
但我不知道它指的是什么。我对数据库的访问权限有限。请原谅我对 Java 的无知。如何在与数据库完全分离的节点应用程序中使用此连接?我的应用程序如何知道“appDB”是什么以及如何在 tnsnames.ora 文件中找到它?
Thanks in advance!
提前致谢!
回答by lsalamon
put the complete connection address in the connectString variable
将完整的连接地址放入connectString变量中
{
user : process.env.ORACLE_USER,
password : process.env.ORACLE_PASSWORD,
connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))(CONNECT_DATA =(SID= ORCL)))"
}
{
user : process.env.ORACLE_USER,
password : process.env.ORACLE_PASSWORD,
connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))(CONNECT_DATA =(SID= ORCL)))"
}
回答by Christopher Jones
To technically answer the question, set the environment variable TNS_ADMIN
. Then node-oracledb (and other OCI-based language APIs like Python cx_Oracle, PHP OCI8, Ruby ruby-oci8 etc) will look for $TNS_ADMIN/tnsnames.ora
.
要从技术上回答这个问题,请设置环境变量TNS_ADMIN
。然后 node-oracledb(以及其他基于 OCI 的语言 API,如 Python cx_Oracle、PHP OCI8、Ruby ruby-oci8 等)将查找$TNS_ADMIN/tnsnames.ora
.
回答by user557657
I have a similar case where I have two VM on azure. One for Oracle12c DB and other is for nodejs oracledb package with Oracle client-side libraries. This is my connection string from client-side to connect with remote DB.
我有一个类似的案例,我在 azure 上有两个 VM。一个用于 Oracle12c DB,另一个用于带有 Oracle 客户端库的 nodejs oracledb 包。这是我从客户端连接到远程数据库的连接字符串。
module.exports = {
user : process.env.NODE_ORACLEDB_USER || "hr",
password : process.env.NODE_ORACLEDB_PASSWORD || "welcome",
connectString : process.env.LOCAL || "myipaddress:1521/servicename",
};