Node中怎么連接mysql數(shù)據(jù)庫,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

npm install --save mysql
使用上述命令安裝完MySQL的模塊后,就可以直接使用了,官網(wǎng)的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)方法創(chuàng)建一個連接對象,然后連接對象的connect()方法創(chuàng)建連接,最后使用query()方法執(zhí)行SQL語句,返回結果作為回調函數(shù)的參數(shù)rows返回,rows為數(shù)組類型。
1. 連接
創(chuàng)建連接對象,需要傳入連接數(shù)據(jù)庫的一些連接參數(shù),也就是createConnection(option)里的option,option是一個對象,以鍵值對的形式傳入createConnection()方法里。上例列舉出了最基本的參數(shù):
host 主機名
user 連接數(shù)據(jù)庫的用戶
password 密碼
database 數(shù)據(jù)庫名稱
還有其他的參數(shù),可以查詢下官方DOCS,這里不一一列舉了,初期學習上面這些參數(shù)就足以。
2. 關閉
關閉一個連接使用end()方法,end()方法提供一個回調函數(shù),如下:
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數(shù)據(jù)表結果后,再打印end a connection。而如果你將關閉方法換成conn.destroy();,那么你就別想返回任何結果了,因為還沒等回調結束就已經(jīng)終止連接了。
3. 連接池
連接池的原理是一開始就給你創(chuàng)建多個連接對象放在一個“池子”里,用的時候取一個,用完了放回“池子”里,在一定程度上是有利于節(jié)省系統(tǒng)開銷的,因為連接對象是在最開始的時候就創(chuàng)建好了,使用的時候不再需要系統(tǒng)開銷去創(chuàng)建數(shù)據(jù)庫連接對象。官方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);
});創(chuàng)建連接池的方法是createPool(option),option里多了一個參數(shù)connectionLimit指的是一次性在連接池里創(chuàng)建多少個連接對象,默認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.
});
});使用一個連接對象執(zhí)行兩次query()函數(shù)。
4. 示例1
使用基本的連接方式來連接數(shù)據(jù)庫,分別定義數(shù)據(jù)連接以及關閉的function,如下示例:
// connect.js 數(shù)據(jù)庫連接與關閉
var mysql = require('mysql');
var config = require('./config.json'); // 將數(shù)據(jù)庫連接參數(shù)寫入mysql對象,即config.mysql
var connCount = 0; // 統(tǒng)計目前未關閉的連接
exports.getConn = function(){
connCount ++;
console.log('............................OPEN a connection, has '+ connCount + ' connection.');
return mysql.createConnection(config.mysql);
};
exports.endConn = function(conn){
conn.end(function(err){
if(!err){
connCount --;
console.log('.........................CLOSE a connection, has '+ connCount + ' connection.');
}
});
};然后給個使用數(shù)據(jù)庫的示例,
// db.js 查詢用戶信息
var connect = require('./connect.js'); // 引入數(shù)據(jù)連接方法
exports.getUser = function(username, callback){
var connection = connect.getConn();
var sql = 'select * from user where username = "' + username + '"';
connection.query(sql,function(err,rows,fields){
callback(err,rows,fields);
});
connect.endConn(connection);
}5. 示例2
使用數(shù)據(jù)庫連接池,同樣先創(chuàng)建數(shù)據(jù)庫連接池的方法,如下兩種方式:
// connect.js 直接使用
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool(config.mysql);
exports.querySQL = function(sql,callback){
pool.query(sql, function(err,rows,fields){
callback(err,rows,fields);
});
}// connect.js 使用getConnection方法
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool(config.mysql);
exports.querySQL = function(sql, callback){
pool.getConnection(function(err,conn){
conn.query(sql,function(err,rows,fields){
callback(err,rows,fields);
conn.release(); // 不要忘了釋放
});
});
}使用的時候,直接使用querySQL方法即可,如下:
// db.js 查詢用戶信息
var connect = require('./connect.js');
exports.getUser = function(username,callback){
var sql = 'select * from user where username = "' + username + '"';
connect.querySQL(sql,function(err,rows,fields){
callback(err,rows,fields);
});
};看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)建站的支持。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、建站服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享標題:Node中怎么連接mysql數(shù)據(jù)庫-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article30/shjpo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、手機網(wǎng)站建設、用戶體驗、微信公眾號、搜索引擎優(yōu)化、網(wǎng)站內鏈
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容