Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a remote Oracle DB with Nodejs through Oracledb Driver

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.

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:

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:

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?

Thanks in advance!

like image 646
Brooke Clonts Avatar asked Apr 26 '17 22:04

Brooke Clonts


People also ask

How do I connect to an Oracle database remotely?

Connecting remotely means running the SQL Command Line (or any other Oracle command-line utility) on a computer other than the Oracle Database XE host computer, and then initiating a database connection from the SQL Command Line (or other utility) over the network.

Can we use Oracle with node js?

Your applications can also use Oracle's document storage SODA calls. Node-oracledb can be used with TypeScript or directly with Node. js.


3 Answers

put the complete connection address in the connectString variable

{ user : process.env.ORACLE_USER, password : process.env.ORACLE_PASSWORD, connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))(CONNECT_DATA =(SID= ORCL)))" }

like image 68
lsalamon Avatar answered Sep 17 '22 21:09

lsalamon


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.

like image 44
Christopher Jones Avatar answered Sep 18 '22 21:09

Christopher Jones


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.

module.exports = {
  user          : process.env.NODE_ORACLEDB_USER || "hr",
  password      : process.env.NODE_ORACLEDB_PASSWORD || "welcome",
  connectString : process.env.LOCAL || "myipaddress:1521/servicename",
};
like image 28
user557657 Avatar answered Sep 17 '22 21:09

user557657