Node版本:
[root@soft opt]# node --version
v0.10.38
OS:CentOS 7.3.1611
主机没有安装Oracle数据库,去http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html下载Oracle的客户端,我下在的文件为:instantclient-basic-macos.x64-12.1.0.2.0.zip,instantclient-sdk-macos.x64-12.1.0.2.0.zip。将他们解压到/opt/oracle下面,并将文件夹重命名为instantclient。
cd /opt/oracle/instantclient,建立一个软链接ln -s libclntsh.so.12.1 libclntsh.so
设置export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH
为了让系统能够找到oracle客户端的so库,新建一个文件/etc/ld.so.conf.d/oracle-instantclient.conf,增加如下内容:/opt/oracle/instantclient。运行ldconfig。
增加环境变量:
export OCI_LIB_DIR=/opt/oracle/instantclient
export OCI_INC_DIR=/opt/oracle/instantclient/sdk/include
通过nmp安装oracledb,需要支持C++11的C++编译器。
npm install -g oracledb
安装完编写一个test.js文件进行测试:
module.exports = {
user : "hr",
password : "welcome",
connectString : "localhost/XE"
};
运行node test.js 如果出现下面的错误提示:
[root@soft opt]# node test.js
/usr/local/lib/node_modules/oracledb/lib/oracledb.js:38
throw err;
^
Error: libaio.so.1: cannot open shared object file: No such file or directory
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/usr/local/lib/node_modules/oracledb/lib/oracledb.js:35:19)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
[root@APPCAN1 opt]# ls /usr/local/lib/node_modules/oracledb/lib/oracledb.js
/usr/local/lib/node_modules/oracledb/lib/oracledb.js
需要安装libaio libaio-devel,也可以去https://centos.pkgs.org/下载相应的rpm文件进行安装
[root@soft opt]# yum install libaio libaio-devel
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
* base: mirrors.btte.net
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
base | 3.7 kB 00:00
base/primary_db | 4.7 MB 00:00
extras | 3.4 kB 00:00
extras/primary_db | 37 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 3.7 MB 00:00
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.107-10.el6 will be installed
---> Package libaio-devel.x86_64 0:0.3.107-10.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================
Installing:
libaio x86_64 0.3.107-10.el6 base 21 k
libaio-devel x86_64 0.3.107-10.el6 base 13 k
Transaction Summary
=======================================================================================================================================================
Install 2 Package(s)
Total download size: 34 k
Installed size: 57 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): libaio-0.3.107-10.el6.x86_64.rpm | 21 kB 00:00
(2/2): libaio-devel-0.3.107-10.el6.x86_64.rpm | 13 kB 00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------
Total 466 kB/s | 34 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libaio-0.3.107-10.el6.x86_64 1/2
Installing : libaio-devel-0.3.107-10.el6.x86_64 2/2
Verifying : libaio-0.3.107-10.el6.x86_64 1/2
Verifying : libaio-devel-0.3.107-10.el6.x86_64 2/2
Installed:
libaio.x86_64 0:0.3.107-10.el6 libaio-devel.x86_64 0:0.3.107-10.el6
Complete!
安装完毕后,再次运行node test.js。
也可以测试下下面的脚本:
var oracledb = require('oracledb');
oracledb.getConnection({
user : "user",
password : "pwd",
connectString : "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.100)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))"
},
function(err,connection){
if(err){
console.error(err.message);
return;
}
connection.execute(
"select sysdate from dual",[],function(err,result){
if(err){
console.error(err.message);
return;
}
console.log(result.rows);
});
});
运行上面的脚本会输出如下内容:[ [ Tue Jan 10 2017 15:12:04 GMT+0800 (CST) ] ]
参考:https://github.com/oracle/node-oracledb/blob/master/INSTALL.md#instzip
http://www.th7.cn/db/Oracle/201406/56370.shtml
http://www.infoq.com/cn/news/2015/02/oracle-node-js-node-oracledb
http://blog.csdn.net/banterise/article/details/51604274