Node.js连接mysql

================创建链接池=====================
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});

pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', rows[0].solution);
});

pool.end(function (err) {
// all connections in the pool have ended
});


=================创建单个链接======================
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});

connection.connect(); //这行代码不写,也可以通过调用查询来隐式地建立连接:

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});

connection.end();

posted @ 2017-06-09 20:29  .追风逐月  阅读(205)  评论(0编辑  收藏  举报