???????Node??Web?????????????????? NoSQL ??????????????? MongoDB ???????????Щ????Web???????????????????????? MySQL ????????????????????????? MySQL ???????????????????????????
????npm install --save mysql
?????????????????? MySQL ?????????????????????????DOCS???????????????????????????
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();
???????????????????????????????ó?????? createConnection(option) ????????????????????????????? connect() ?????????????????? query() ???????SQL???????????????????????? rows ????? rows ??????????
????1. ????
?????????????????????????????????Щ???????????? createConnection(option) ??? option ?? option ??????????????????????? createConnection() ???????????о??????????????
????host ??????
????user ?????????????
????password ????
????database ?????????
??????????????????????????1??DOCS?????????о????????????????Щ?????????
????2. ???
????????????????? end() ?????? end() ?????????????????????£?
????connect.end(function(err){
????console.log('End a connection');
????});
???????????????????? end() ????????????????????????????????????????????? destroy() ??????????????????????????????????ɡ?
????????????????
var mysql = require('mysql');
var option = require('./connect.js').option;
var conn = mysql.createConnection(option);
conn.query('select * from message'??function(err??rows??fields){
if(!err){
console.log(rows);
}
});
conn.end(function(err){
console.log('end a connection');
});
??????????????????? SELECT ????????????? end a connection ??????????????????? conn.destroy(); ?????????????κν????????????????????????????????
????3. ?????
?????????????????????????????????????????“????”??????????????????????“????”?????????????????????????????????????????????????????????????????????????????????????????????????DOCS???????????????
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(err?? rows?? fields){
if (err) throw err;
console.log('The solution is: '?? rows[0].solution);
});
??????????????????? createPool(option) ?? option ???????????? connectionLimit ????????????????????????????????????10?????????????????????????????????淽???????????
var mysql = require('mysql');
var pool  = mysql.createPool({
host     : 'example.org'??
user     : 'bob'??
password : 'secret'??
database : 'my_db'
});
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.
});
// Use the connection
connection.query( 'SELECT something2 FROM sometable2'?? function(err?? rows){
// And done with the connection.
connection.release();
// Don't use the connection here?? it has been returned to the pool.
});
});
???????????????????????? query() ??????