Node.js连接mysql
Posted 追风逐月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
以上是关于Node.js连接mysql的主要内容,如果未能解决你的问题,请参考以下文章