Method _ Node. js for configuring MySQL or Oracle database for node. js programs in Linux

Source: Internet
Author: User
This article mainly introduces Node in Linux. the method for configuring MySQL or Oracle databases in js programs. By default, the Node environment has been assembled and we can use the npm package management tool for configuration. For more information, see Mysql usage
Install the mysql module:
Run the command in the cmd command line of the installation root directory.

npm install mysql

After the installation is successful,
The mysql database table already exists.
Create mysql. js in the nodejs root directory:

Var sys = require ('util'); var mysql = require ('mysql'); console. log ('connecting to MySQL... '); var http = require ("http"); var server = http. createServer (function (request, response) {response. writeHead (200, {"Content-Type": "text/html; charset: UTF-8"}); response. write ("
 "); Var client = mysql. createConnection ({'host': 'localhost', 'Port': 3306, 'user': 'testmysql', 'Password': '66661 '}); 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 server has started working...
"); Response. write (" MySQL already connected ....
") ;}Clientready (client) ;}; clientReady = function (client) {var values = ['nice, ']; 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 ";}Response. write (data); response. write (" Close MySQL connection... "); response. write (""); 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.
You can see the effect by accessing http: // localhost: 8033 in the browser.

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 

Go to the nodejs directory to install oracle module support

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

Write an oracle. js file to test whether the connection is normal when the SQL statement is executed.

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 note that the connection.exe cute method must have three parameters. Otherwise, connection.exe cute ("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 running command

 node oracle.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.