Nodejs 在 Linux 上连接 Oracle 数据库的方法

Nodejs 在 Linux 上连接 Oracle 数据库的方法

Linux下Nodejs链接oracle 需要哪些环境要求啊?都要被这东西搞烦躁了.

4 回复

Node.js 在 Linux 上连接 Oracle 数据库的方法

在 Linux 环境下使用 Node.js 连接 Oracle 数据库需要一些特定的环境配置和依赖项。以下是一些关键步骤和示例代码,帮助你顺利实现这一目标。

环境要求

  1. Oracle Instant Client:首先需要安装 Oracle Instant Client,它包含了与 Oracle 数据库通信所需的动态库。
  2. node-oracledb:这是一个用于 Node.js 的 Oracle 数据库驱动程序。
  3. Node.js:确保你的系统已经安装了 Node.js。

安装步骤

  1. 安装 Oracle Instant Client 你可以从 Oracle 官方网站下载适合你系统的 Instant Client 包。例如,对于 64 位 Linux 系统,可以下载 instantclient-basic-linux.x64-<version>.zipinstantclient-sdk-linux.x64-<version>.zip

    wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
    wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip
    unzip instantclient-basic-linux.x64-19.8.0.0.0dbru.zip -d /opt/oracle
    unzip instantclient-sdk-linux.x64-19.8.0.0.0dbru.zip -d /opt/oracle
    
  2. 设置环境变量 设置 LD_LIBRARY_PATHORACLE_HOME 环境变量:

    export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_8:$LD_LIBRARY_PATH
    export ORACLE_HOME=/opt/oracle/instantclient_19_8
    
  3. 安装 node-oracledb 使用 npm 安装 node-oracledb 模块:

    npm install oracledb
    

示例代码

以下是一个简单的示例代码,演示如何使用 Node.js 连接到 Oracle 数据库并执行查询:

const oracledb = require('oracledb');

async function run() {
    let connection;

    try {
        // 创建数据库连接
        connection = await oracledb.getConnection({
            user: 'your_username',
            password: 'your_password',
            connectString: 'your_host:port/service_name'
        });

        // 执行查询
        const result = await connection.execute(`SELECT * FROM your_table`);

        // 输出结果
        console.log(result.rows);
    } catch (err) {
        console.error(err);
    } finally {
        if (connection) {
            try {
                // 关闭连接
                await connection.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
}

run();

请将 your_username, your_password, your_host, port, service_nameyour_table 替换为实际的值。

通过以上步骤和示例代码,你应该能够在 Linux 系统上成功地使用 Node.js 连接到 Oracle 数据库。希望这些信息对你有所帮助!


你需要装oracle,配置响应的环境变量, node-gyp 要安装,可以参考 https://github.com/zhs077/node-oracle

要在Linux上使用Node.js连接Oracle数据库,你需要满足以下环境要求:

  1. 安装Node.js:确保你的系统上已安装Node.js。你可以通过运行node -v来检查当前版本。
  2. 安装Oracle Instant Client:Oracle Instant Client 是一个轻量级的库,用于与Oracle数据库进行通信。你需要下载并安装它。确保将安装路径添加到系统的环境变量中。
  3. 安装oracledb模块:这是Node.js连接Oracle数据库的主要模块。你可以使用npm来安装。

安装步骤

1. 安装Node.js

如果你还没有安装Node.js,可以通过包管理器或官方站点进行安装:

sudo apt-get update
sudo apt-get install nodejs

2. 安装Oracle Instant Client

  • 下载Oracle Instant Client的预编译二进制文件(例如Basic和SDK包)。
  • 解压到指定目录,例如 /opt/oracle/instantclient_19_8
  • 添加Oracle Instant Client目录到LD_LIBRARY_PATH环境变量:
    export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_8:$LD_LIBRARY_PATH
    

3. 安装oracledb模块

使用npm安装oracledb模块:

npm install oracledb

示例代码

下面是一个简单的示例代码,演示如何使用Node.js连接到Oracle数据库:

const oracledb = require('oracledb');

async function run() {
  let connection;

  try {
    // 创建数据库连接
    connection = await oracledb.getConnection({
      user: "your_username",
      password: "your_password",
      connectString: "your_connect_string"
    });

    // 执行SQL查询
    const result = await connection.execute(`SELECT * FROM your_table`);
    console.log(result.rows);
  } catch (err) {
    console.error(err);
  } finally {
    if (connection) {
      try {
        // 关闭数据库连接
        await connection.close();
      } catch (err) {
        console.error(err);
      }
    }
  }
}

run();

在这个示例中:

  • your_usernameyour_password 是你的Oracle数据库用户名和密码。
  • your_connect_string 是你的数据库连接字符串,格式可以是类似于 hostname:port/service_name 或者 hostname:port/SERVICENAME
  • your_table 是你要查询的表名。

希望这些信息能帮助你在Linux上成功连接Oracle数据库!

回到顶部