到的NodeJS MySQL连接错误
答
使用连接池:
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query('SELECT something FROM sometable', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
答
您可以使用node-mysql,它非常容易使用。我已经使用过一次,这里是例子: -
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(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
您可以发布在上下文中的代码的NodeJS? – 2016-04-28 06:20:46
看到这个帖子:http://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection – Subburaj
变化'主持人: “127.0.0.1”''到主机:“localhost”的' –