awsnodejs

Use Oracle Database with Node.js

Linux

In order to connect your Node.js application to an Oracle Database, you need to install the following packages.

  • libaio
  • Oracle Instant Client (and SDK)
  • oracledb Node.js module

Step 1: Install libaio

  • Execute the following command:

      $ sudo apt-get install libaio1
    

Step 2: Install Oracle Instant Client and Oracle Instant Client SDK

  • Download both instantclient-basic-linux.x64.zip and instantclient-sdk-linux.x64.zip. Remember to replace the X.Y.Z placeholder with the corresponding version. Download instantclient-basic-linux.x64-X.Y.Z.zip and instantclient-sdk-linux.x64-X.Y.Z.zip (we will use them in the rest of the examples).

  • Uncompress instantclient-basic-linux.x64.zip by executing the following command:

      $ unzip instantclient-basic-linux.x64-X.Y.Z.zip
    
  • Copy the extracted contents to /opt/bitnami/common/lib:

      $ sudo cp -r *instantclient_X_Y/ /opt/bitnami/common/lib
    
  • Execute the following command:

      $ sudo ln -s /opt/bitnami/common/lib/libclntsh.so.X.Y /opt/bitnami/common/lib/libclntsh.so
    
  • Extract instantclient-sdk-linux.x64-X.Y.Z.zip:

      $ unzip instantclient-sdk-linux.x64-X.Y.Z.zip
    
  • Copy the contents of the sdk subfolder to /opt/bitnami/common/include:

      $ sudo cp -r instantclient_X_Y/sdk/include/* /opt/bitnami/common/include
    

Step 3: Install OracleDB Node.js module

  • Before installing, set the following environment variables:

      $   export OCI_LIB_DIR=/opt/bitnami/common/lib
      $   export OCI_INC_DIR=/opt/bitnami/common/include
    
  • In your Node.js application folder, execute the following command:

      $   npm install oracledb
    

Step 4: Test the connection

In order to test that the plugin was installed correctly, execute one of the examples detailed in the OracleDB Node.js module documentation.

  • Download this example from the OracleDB Node.js module website.

  • In the same folder where connect.js was downloaded, create the file dbconfig.js with the following contents (remembering to replace the DATABASE_USER, DATABASE_PASSWORD and DATABASE_CONNECTION_STRING placeholders with the correct values):

      module.exports = {
          user          : "DATABASE_USER",
          password      : "DATABASE_PASSWORD",
          connectString : "DATABASE_CONNECTION_STRING"
      };
    

    For example:

      module.exports = {
          user          : "SYSTEM",
          password      : "mypassword",
          connectString : "127.0.0.1:1521/PDB1.oracleuser.oraclecloud.internal"
      };
    
  • Run the testing program:

      $ node connect.js
    

If everything was correct, you should see the message “Connection was successful!”

Last modification December 21, 2022