Home >Web Front-end >JS Tutorial >How to configure MySQL or Oracle database for Node.js program under Linux_node.js

How to configure MySQL or Oracle database for Node.js program under Linux_node.js

WBOY
WBOYOriginal
2016-05-16 15:10:062206browse

mysql usage
Install mysql module:
Execute the command on the cmd command line in the installation root directory

npm install mysql

After successful installation,
When the mysql database table already exists.
Create a new mysql.js in the nodejs root directory:

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 服务器已经开始工作...<br/>"); 
        response.write("已经连接上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("关闭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/");  

Run node mysql.js .
Visit http://localhost:8033 with your browser to see the effect.

Configure oracle support
Download the oracle database client connection package from the oracle website
instantclient-basic-linux, instantclient-sdk-linux
Unzip the oracle client connection module

$ 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 

Configure environment variables

$ export OCI_INCLUDE_DIR=/opt/instantclient/sdk/include/ 
$ export OCI_LIB_DIR=/opt/instantclient 

Enter the nodejs directory and install oracle module support

$ cd /usr/local/lib 
 
$ npm install oracle 
 
export LD_LIBRARY_PATH=/opt/instantclient 

Write the oracle.js file to test whether the connection and execution of sql are normal
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 方法必须要三个参数 不然会出错 
 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(); 
 }); 
}); 
Terminal run command
 node oracle.js
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn