码迷,mamicode.com
首页 > 数据库 > 详细

Node.js连接mysql

时间:2017-06-09 20:52:06      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:require   mysq   ret   UI   建立   database   user   creat   host   

================创建链接池=====================
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();

Node.js连接mysql

标签:require   mysq   ret   UI   建立   database   user   creat   host   

原文地址:http://www.cnblogs.com/xbblogs/p/6973631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!