1var mysql = require('mysql');
2
3// connect to the db
4dbConnectionInfo = {
5 host: "localhost",
6 port: "3306",
7 user: "root",
8 password: "root",
9 connectionLimit: 5, //mysql connection pool length
10 database: "db_name"
11};
12
13//For mysql single connection
14/* var dbconnection = mysql.createConnection(
15 dbConnectionInfo
16);
17
18 dbconnection.connect(function (err) {
19 if (!err) {
20 console.log("Database is connected ... nn");
21 } else {
22 console.log("Error connecting database ... nn");
23 }
24});
25
26*/
27
28//create mysql connection pool
29var dbconnection = mysql.createPool(
30 dbConnectionInfo
31);
32
33// Attempt to catch disconnects
34dbconnection.on('connection', function (connection) {
35 console.log('DB Connection established');
36
37 connection.on('error', function (err) {
38 console.error(new Date(), 'MySQL error', err.code);
39 });
40 connection.on('close', function (err) {
41 console.error(new Date(), 'MySQL close', err);
42 });
43
44});
45
46
47module.exports = dbconnection;
48
1var pool = mysql.createPool({
2 connectionLimit : 10,
3 host : 'example.org',
4 user : 'bobby',
5 password : 'pass'
6 });
7
8pool.getConnection(function(err, connection){
9 if(err){
10 return cb(err);
11 }
12 connection.changeUser({database : "firm1"});
13 connection.query("SELECT * from history", function(err, data){
14 connection.release();
15 cb(err, data);
16 });
17});
18