Kata Pengantar
MySQL ialah produk pangkalan data sumber terbuka yang biasa digunakan dan biasanya merupakan pilihan pertama untuk pangkalan data percuma. Saya menyemak senarai NPM dan mendapati bahawa Nodejs mempunyai 13 perpustakaan yang boleh mengakses MySQL felixge/node-mysql nampaknya merupakan projek yang paling popular, jadi saya memutuskan untuk mencubanya.
Perhatikan nama, "felixge/node-mysql" bukan "node-mysql", episod ini akan diperkenalkan di bahagian pemasangan!
Jadual Kandungan
1. Pengenalan kepada node-mysql
felixge/node-mysql ialah program klien MySQL yang dilaksanakan dalam nodej tulen menggunakan javascript. felixge/node-mysql merangkum operasi asas Nodejs pada MySQL, 100% lesen awam MIT.
Alamat projek: https://github.com/felixge/node-mysql
2. Wujudkan perpustakaan ujian MySQL
Buat perpustakaan ujian MySQL secara setempat: nodejs
~ mysql -uroot -p mysql> CREATE DATABASE nodejs; mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | nodejs | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
mysql> GRANT ALL ON nodejs.* to nodejs@'%' IDENTIFIED BY 'nodejs'; mysql> GRANT ALL ON nodejs.* to nodejs@localhost IDENTIFIED BY 'nodejs';
Log masuk ke MySQL sekali lagi
C:\Users\Administrator>mysql -unodejs -p Enter password: ****** mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | nodejs | | test | +--------------------+ 3 rows in set (0.00 sec)
mysql> USE nodejs Database changed
Buat jadual pengguna baharu
CREATE TABLE t_user( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(16) NOT NULL , create_date TIMESTAMP NULL DEFAULT now() )ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE UNIQUE INDEX t_quiz_IDX_0 on t_user(name);
mysql> SHOW TABLES; +------------------+ | Tables_in_nodejs | +------------------+ | t_user | +------------------+ 1 row in set (0.04 sec)
3. pemasangan nod-mysql
Persekitaran sistem saya
win7 64bit
Nodejs:v0.10.5
Npm:1.2.19
MySQL:Versi pelayan: 5.6.11 MySQL Community Server (GPL)
Cipta projek: nodejs-node-mysql
~ D:\workspace\javascript>mkdir nodejs-node-mysql ~ D:\workspace\javascript>cd nodejs-node-mysql ~ D:\workspace\javascript\nodejs-node-mysql>npm install node-mysql node-mysql@0.2.0 node_modules\node-mysql ├── better-js-class@0.1.2 ├── cps@0.1.7 ├── underscore@1.5.2 └── mysql@2.0.0-alpha9 (require-all@0.0.3, bignumber.js@1.0.1)
Ini adalah episod kecil
Selepas memasang "node-mysql", buka fail package.json dan dapatkan alamat projek ialah
https://github.com/redblaze/node-mysql.git
Seperti yang anda lihat dari perhubungan pergantungan, ia bergantung pada perpustakaan mysql dan merupakan enkapsulasi felixge/node-mysql.
nod-mysql1
Memandangkan bintang projek ini ialah 0, garpunya juga 0. Oleh itu, saya tidak akan menghabiskan masa mengujinya dan memasang semula pakej felixge/node-mysql.
Pasang semula node-mysql
~ D:\workspace\javascript\nodejs-node-mysql>rm -rf node_modules ~ D:\workspace\javascript\nodejs-node-mysql>npm install mysql@2.0.0-alpha9 npm http GET https://registry.npmjs.org/mysql/2.0.0-alpha9 npm http 200 https://registry.npmjs.org/mysql/2.0.0-alpha9 npm http GET https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha9.tgz npm http 200 https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha9.tgz npm http GET https://registry.npmjs.org/require-all/0.0.3 npm http GET https://registry.npmjs.org/bignumber.js/1.0.1 npm http 304 https://registry.npmjs.org/require-all/0.0.3 npm http 304 https://registry.npmjs.org/bignumber.js/1.0.1 mysql@2.0.0-alpha9 node_modules\mysql ├── require-all@0.0.3 └── bignumber.js@1.0.1
Itu sahaja kali ini, mari teruskan pembangunan!
Buat fail permulaan program nod: app.js
Ujian pertama
~ vi app.js
var mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'nodejs', password: 'nodejs', database:'nodejs', port: 3306 }); conn.connect(); conn.query('SELECT 1 + 1 AS solution', function(err, rows, fields) { if (err) throw err; console.log('The solution is: ', rows[0].solution); }); conn.end();
Jalankan nod
~ D:\workspace\javascript\nodejs-node-mysql>node app.js The solution is: 2
Dengan cara ini kami membenarkan Nodejs menyambung ke MySQL.
4. Menggunakan node-mysql
Sekarang kami akan menjalankan ujian biasa pada API nod-mysql.
Jadual baharu, dipadamkan, diubah suai dan disemak
Konfigurasi kolam sambungan
MySQL memutuskan sambungan dan menyambung semula
Ujian tamat masa kolam sambungan
1). Semak kebaharuan jadual, pemadaman dan pengubahsuaian
Ubah suai app.js
~ vi app.js
var mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); conn.connect(); var insertSQL = 'insert into t_user(name) values("conan"),("fens.me")'; var selectSQL = 'select * from t_user limit 10'; var deleteSQL = 'delete from t_user'; var updateSQL = 'update t_user set name="conan update" where name="conan"'; //delete conn.query(deleteSQL, function (err0, res0) { if (err0) console.log(err0); console.log("DELETE Return ==> "); console.log(res0); //insert conn.query(insertSQL, function (err1, res1) { if (err1) console.log(err1); console.log("INSERT Return ==> "); console.log(res1); //query conn.query(selectSQL, function (err2, rows) { if (err2) console.log(err2); console.log("SELECT ==> "); for (var i in rows) { console.log(rows[i]); } //update conn.query(updateSQL, function (err3, res3) { if (err3) console.log(err3); console.log("UPDATE Return ==> "); console.log(res3); //query conn.query(selectSQL, function (err4, rows2) { if (err4) console.log(err4); console.log("SELECT ==> "); for (var i in rows2) { console.log(rows2[i]); } }); }); }); }); }); //conn.end();
Output konsol:
D:\workspace\javascript\nodejs-node-mysql>node app.js
DELETE Return ==> { fieldCount: 0, affectedRows: 2, insertId: 0, serverStatus: 34, warningCount: 0, message: '', protocol41: true, changedRows: 0 } INSERT Return ==> { fieldCount: 0, affectedRows: 2, insertId: 33, serverStatus: 2, warningCount: 0, message: '&Records: 2 Duplicates: 0 Warnings: 0', protocol41: true, changedRows: 0 } SELECT ==> { id: 33, name: 'conan', create_date: Wed Sep 11 2013 12:09:15 GMT+0800 (中国标准时间) } { id: 34, name: 'fens.me', create_date: Wed Sep 11 2013 12:09:15 GMT+0800 (中国标准时间) } UPDATE Return ==> { fieldCount: 0, affectedRows: 1, insertId: 0, serverStatus: 2, warningCount: 0, message: '(Rows matched: 1 Changed: 1 Warnings: 0', protocol41: true, changedRows: 1 } SELECT ==> { id: 33, name: 'conan update', create_date: Wed Sep 11 2013 12:09:15 GMT+0800 (中国标准时间) } { id: 34, name: 'fens.me', create_date: Wed Sep 11 2013 12:09:15 GMT+0800 (中国标准时间) }
Disebabkan sifat tak segerak nod, perkara di atas ialah operasi berterusan dan kod akan ditulis dalam serpihan. Kami boleh merangkum kod di atas melalui perpustakaan async, sila rujuk artikel: Nodejs kawalan proses tak segerak Async
2). Tambah fail: app-pooling.js
~ vi app-pooling.js
var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); var selectSQL = 'select * from t_user limit 10'; pool.getConnection(function (err, conn) { if (err) console.log("POOL ==> " + err); conn.query(selectSQL,function(err,rows){ if (err) console.log(err); console.log("SELECT ==> "); for (var i in rows) { console.log(rows[i]); } conn.release(); }); });
D:\workspace\javascript\nodejs-node-mysql>node app-pooling.js
SELECT ==> { id: 39, name: 'conan update', create_date: Wed Sep 11 2013 13:41:18 GMT+0800 (中国标准时间) } { id: 40, name: 'fens.me', create_date: Wed Sep 11 2013 13:41:18 GMT+0800 (中国标准时间) }
Simulasikan 3 jenis ralat masing-masing
a. Kata laluan log masuk salah
b. Pangkalan data tidak berfungsi
c. tamat masa sambungan pangkalan data
Fail baharu: app-reconnect.js
~ vi app-reconnect.js
var mysql = require('mysql'); var conn; function handleError () { conn = mysql.createConnection({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); //连接错误,2秒重试 conn.connect(function (err) { if (err) { console.log('error when connecting to db:', err); setTimeout(handleError , 2000); } }); conn.on('error', function (err) { console.log('db error', err); // 如果是连接断开,自动重新连接 if (err.code === 'PROTOCOL_CONNECTION_LOST') { handleError(); } else { throw err; } }); } handleError();
Ubah suai kata laluan: 'nodejs11'
Keluaran konsol.
D:\workspace\javascript\nodejs-node-mysql>node app-reconnect.js
error when connecting to db: { [Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'nodejs'@'localhost' (using pass rd: YES)] code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlState: '28000', fatal: true } error when connecting to db: { [Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'nodejs'@'localhost' (using pass rd: YES)] code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlState: '28000', fatal: true }
Mulakan nod seperti biasa dan kemudian matikan proses mysqld.
Keluaran konsol.
D:\workspace\javascript\nodejs-node-mysql>node app-reconnect.js
db error { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read', fatal: true } Error: read ECONNRESET at errnoException (net.js:884:11) at TCP.onread (net.js:539:19)
Tukar kepada akaun akar, ubah suai parameter tunggu_masa tamat MySQL dan tetapkannya kepada tamat masa 10 milisaat.
~ mysql -uroot -p mysql> show variables like 'wait_timeout'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wait_timeout | 28800 | +---------------+-------+ 1 row in set (0.00 sec) mysql> set global wait_timeout=10; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'wait_timeout'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wait_timeout | 10 | +---------------+-------+ 1 row in set (0.00 sec)
~ vi app-reconnection.js
function query(){ console.log(new Date()); var sql = "show variables like 'wait_timeout'"; conn.query(sql, function (err, res) { console.log(res); }); } query(); setInterval(query, 15*1000);
Output konsol
D:\workspace\javascript\nodejs-node-mysql>node app-reconnect.js Wed Sep 11 2013 15:21:14 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ] db error { [Error: Connection lost: The server closed the connection.] fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } Wed Sep 11 2013 15:21:28 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ] db error { [Error: Connection lost: The server closed the connection.] fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } Wed Sep 11 2013 15:21:43 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ]
Untuk masalah tunggu_masa tamat, mari uji sambungan sekali lagi.
Ubah suai fail app-pooling.js
var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); var selectSQL ="show variables like 'wait_timeout'"; pool.getConnection(function (err, conn) { if (err) console.log("POOL ==> " + err); function query(){ conn.query(selectSQL, function (err, res) { console.log(new Date()); console.log(res); conn.release(); }); } query(); setInterval(query, 5000); });
D:\workspace\javascript\nodejs-node-mysql>node app-pooling.js Wed Sep 11 2013 15:32:25 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ] Wed Sep 11 2013 15:32:30 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ] Wed Sep 11 2013 15:32:35 GMT+0800 (中国标准时间) [ { Variable_name: 'wait_timeout', Value: '10' } ]