SoFunction
Updated on 2025-03-09

Methods to configure MySQL or Oracle database for programs under Linux

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

npm install mysql

After successful installation,
MySQL database table already exists.
Create new in nodejs root directory:

var sys = require('util'); 
var mysql=require('mysql'); 
('Connecting MySQL...'); 
var http = require("http"); 
var server=(function(request, response) { 
  (200, {"Content-Type": "text/html;charset:utf-8"});  
  ("<!doctype html><html><meta charset='utf-8'/>"); 
   var client = ({'host':'localhost','port':3306,'user':'testmysql','password':'123456'}); 
  clientConnectionReady = function(client) 
  { 
    ('use test', function(error, results) { 
      if(error) { 
        ('ClientConnectionReady Error: ' + ); 
        (); 
        return; 
      }else{ 
        ("The nodejs server has started working...<br/>"); 
        ("MySQL is already connected...<br/>"); 
    } 
      clientReady(client); 
    }); 
  }; 
    
  clientReady = function(client) { 
    var values = ['It's not bad']; 
    ('insert into nodemysql set names = :1', values, 
      function(error, results) { 
        if(error) { 
          ("ClientReady Error: " + ); 
          (); 
          return; 
        } 
        ('Inserted: ' +  + ' row.'); 
        ('Id inserted: ' + ); 
      } 
    ); 
    getData(client); 
  } 
    
  getData = function(client) { 
    ( 
      'select * from nodemysql', 
      function selectCb(error, results, fields) { 
        if (error) { 
          ('GetData Error: ' + ); 
          (); 
          return; 
        } 
     var data = ''; 
      for(var i=0; i&lt;; i++){ 
      var firstResult = results[i]; 
         data += 'id: ' + firstResult['id']+'  name: ' + firstResult['names']+"&lt;br/&gt;"; 
      } 
       
       (data);  
       ("Close MySQL connection..."); 
       ("&lt;/html&gt;"); 
      (); 
      } 
    ); 
    (); 
     
  }; 
    
  clientConnectionReady(client); 
}); 
(8033,"127.0.0.1"); 
 
var sys = require("util"); 
("Server running at http://localhost:8033/");  

Run node   .
Browser Visit http://localhost:8033 to see the effect.

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

$ unzip instantclient-basic-linux-11.2.0.3. 
$ unzip instantclient-sdk-linux-11.2.0.3.  
$ sudo mv instantclient_11_2/ /opt/instantclient 
 
$ cd /opt/instantclient 
$ sudo ln -s .11.1  
$ sudo ln -s .11.1  

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 a file to test whether it is normal to connect to the execution of SQL.
var oracle = require("oracle"); 
 
({ "hostname": "localhost", "user": "demo", "password": "demo", "database": "orcl", "port": 1521}, function(err, connection) { 
 if(err) { 
  (err); 
 } 
 // selecting rows Note The method must have three parameters, otherwise an error will occur ("SELECT * FROM TEST WHERE ID = :1", ['1'], function(err1, results) { 
  // results will be an array of objects 
  ("query start"); 
  if(err1) { 
    (err1); 
  } 
  // (); 
  for(var i = 0; i &lt; ; i++) { 
   (results[i].ID); 
  } 
  (); 
 }); 
}); 
Terminal run command
 node