這篇文章主要介紹了Linux下為Node.js程序配置MySQL或Oracle數(shù)據(jù)庫的方法,這里默認(rèn)已經(jīng)裝配好了Node環(huán)境然后我們利用npm包管理工具來進(jìn)行配置,需要的朋友可以參考下
mysql使用
安裝mysql 模塊:
在安裝根目錄 cmd命令行執(zhí)行命令
npm install mysql
安裝成功后、
mysql數(shù)據(jù)庫表 已存在的情況下。
在nodejs根目錄 新建mysql.js:
var sys = require('util');
var mysql=require('mysql');
console.log('正在連接MySQL...');
var http = require("http");
var server=http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html;charset:utf-8"});
response.write("<!doctype html><html><meta charset='utf-8'/>");
var client = mysql.createConnection({'host':'localhost','port':3306,'user':'testmysql','password':'123456'});
clientConnectionReady = function(client)
{
client.query('use test', function(error, results) {
if(error) {
console.log('ClientConnectionReady Error: ' + error.message);
client.end();
return;
}else{
response.write("nodejs 服務(wù)器已經(jīng)開始工作...<br/>");
response.write("已經(jīng)連接上MySQL....<br/>");
}
clientReady(client);
});
};
clientReady = function(client) {
var values = ['不錯啊'];
client.query('insert into nodemysql set names = :1', values,
function(error, results) {
if(error) {
console.log("ClientReady Error: " + error.message);
client.end();
return;
}
console.log('Inserted: ' + results.affectedRows + ' row.');
console.log('Id inserted: ' + results.insertId);
}
);
getData(client);
}
getData = function(client) {
client.query(
'select * from nodemysql',
function selectCb(error, results, fields) {
if (error) {
console.log('GetData Error: ' + error.message);
client.end();
return;
}
var data = '';
for(var i=0; i<results.length; i++){
var firstResult = results[i];
data += 'id: ' + firstResult['id']+' name: ' + firstResult['names']+"<br/>";
}
response.write(data);
response.write("關(guān)閉MySQL連接...");
response.write("</html>");
response.end();
}
);
client.end();
};
clientConnectionReady(client);
});
server.listen(8033,"127.0.0.1");
var sys = require("util");
sys.puts("Server running at http://localhost:8033/");
運行 node mysql.js 。
瀏覽器 訪問 http://localhost:8033 即可看到效果。
配置oracle支持
在oracle網(wǎng)站下載oracle數(shù)據(jù)庫客戶端連接包
instantclient-basic-linux,instantclient-sdk-linux
解壓oracle客戶端連接模塊
$ unzip instantclient-basic-linux-11.2.0.3.0.zip
$ unzip instantclient-sdk-linux-11.2.0.3.0.zip
$ sudo mv instantclient_11_2/ /opt/instantclient
$ cd /opt/instantclient
$ sudo ln -s libocci.so.11.1 libocci.so
$ sudo ln -s libclntsh.so.11.1 libclntsh.so
配置環(huán)境變量
$ export OCI_INCLUDE_DIR=/opt/instantclient/sdk/include/
$ export OCI_LIB_DIR=/opt/instantclient
進(jìn)入nodejs目錄 安裝oracle模塊支持
$ cd /usr/local/lib
$ npm install oracle
export LD_LIBRARY_PATH=/opt/instantclient
編寫oracle.js文件 測試連接于執(zhí)行sql是否正常
var oracle = require("oracle");
oracle.connect({ "hostname": "localhost", "user": "demo", "password": "demo", "database": "orcl", "port": 1521}, function(err, connection) {
if(err) {
console.log(err);
}
// selecting rows 注意 connection.execute 方法必須要三個參數(shù) 不然會出錯
connection.execute("SELECT * FROM TEST WHERE ID = :1", ['1'], function(err1, results) {
// results will be an array of objects
console.log("query start");
if(err1) {
console.log(err1);
}
// console.log(results.length);
for(var i = 0; i < results.length; i++) {
console.log(results[i].ID);
}
connection.close();
});
});
終端運行命令
node oracle.js