时间:2021-07-01 10:21:17 帮助过:15人阅读
================创建链接池=====================
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