|
| 1 | +## Node MySQL 2 |
| 2 | + |
| 3 | +[](https://greenkeeper.io/) |
| 4 | +[![NPM Version][npm-image]][npm-url] |
| 5 | +[![NPM Downloads][downloads-image]][downloads-url] |
| 6 | +[![Node.js Version][node-version-image]][node-version-url] |
| 7 | +[![Linux Build][travis-image]][travis-url] |
| 8 | +[![Windows Build][appveyor-image]][appveyor-url] |
| 9 | +[![License][license-image]][license-url] |
| 10 | + |
| 11 | +> 适用于Node.js的MySQL客户端,专注于性能优化。支持SQL预处理、非UTF-8编码支持、二进制文件编码支持、压缩和SSL等等 [查看更多](https://github.com/sidorares/node-mysql2/tree/master/documentation) |
| 12 | +
|
| 13 | +__目录__ |
| 14 | + |
| 15 | + - [MySQL2的历史以及选择原因](#MySQL2的历史以及选择原因) |
| 16 | + - [安装](#安装) |
| 17 | + - [查询数据](#查询数据) |
| 18 | + - [SQL预处理的使用](#SQL预处理的使用) |
| 19 | + - [连接池的使用](#连接池的使用) |
| 20 | + - [Promise封装](#Promise封装) |
| 21 | + - [API配置项](#API配置项) |
| 22 | + - [文档](#文档) |
| 23 | + - [鸣谢](#鸣谢) |
| 24 | + - [贡献](#贡献) |
| 25 | + |
| 26 | +## MySQL2的历史以及选择原因 |
| 27 | + |
| 28 | +MySQL2 项目是 [MySQL-Native][mysql-native] 的延续。 协议解析器代码从头开始重写,api 更改为匹配流行的 [mysqljs/mysql][node-mysql]。 MySQL2 团队正在与 [mysqljs/mysql][node-mysql] 团队合作,将共享代码分解并移至 [mysqljs][node-mysql] 组织下。 |
| 29 | + |
| 30 | +MySQL2 大部分 API 与 [mysqljs][node-mysql] 兼容,并支持大部分功能。 MySQL2 还提供了更多的附加功能 |
| 31 | + |
| 32 | + - 更快、更好的性能 |
| 33 | + - [支持预处理](https://github.com/sidorares/node-mysql2/tree/master/documentation/Prepared-Statements.md) |
| 34 | + - MySQL二进制日志协议 |
| 35 | + - [MySQL Server](https://github.com/sidorares/node-mysql2/tree/master/documentation/MySQL-Server.md) |
| 36 | + - 对编码和排序规则有很好的支持 |
| 37 | + - [Promise封装](https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md) |
| 38 | + - 支持压缩 |
| 39 | + - SSL 和 [Authentication Switch](https://github.com/sidorares/node-mysql2/tree/master/documentation/Authentication-Switch.md) |
| 40 | + - [自定义流](https://github.com/sidorares/node-mysql2/tree/master/documentation/Extras.md) |
| 41 | + - [连接池](#using-connection-pools) |
| 42 | + |
| 43 | +## 安装 |
| 44 | + |
| 45 | +MySQL2 可以跨平台使用,毫无疑问可以安装在 Linux、Mac OS 或 Windows 上。 |
| 46 | + |
| 47 | +```bash |
| 48 | +npm install --save mysql2 |
| 49 | +``` |
| 50 | + |
| 51 | +## 查询数据 |
| 52 | + |
| 53 | +```js |
| 54 | +// 导入模块 |
| 55 | +const mysql = require('mysql2'); |
| 56 | + |
| 57 | +// 创建一个数据库连接 |
| 58 | +const connection = mysql.createConnection({ |
| 59 | + host: 'localhost', |
| 60 | + user: 'root', |
| 61 | + database: 'test' |
| 62 | +}); |
| 63 | + |
| 64 | +// 简单查询 |
| 65 | +connection.query( |
| 66 | + 'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45', |
| 67 | + function(err, results, fields) { |
| 68 | + console.log(results); // 结果集 |
| 69 | + console.log(fields); // 额外的元数据(如果有的话) |
| 70 | + } |
| 71 | +); |
| 72 | + |
| 73 | +// 使用占位符 |
| 74 | +connection.query( |
| 75 | + 'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', |
| 76 | + ['Page', 45], |
| 77 | + function(err, results) { |
| 78 | + console.log(results); |
| 79 | + } |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +## SQL预处理的使用 |
| 84 | + |
| 85 | +使用 MySQL2,您还可以提前准备好SQL预处理语句。 使用准备好的SQL预处理语句,MySQL 不必每次都为相同的查询做准备,这会带来更好的性能。 如果您不知道为什么它们很重要,请查看这些讨论 |
| 86 | + |
| 87 | +- [如何防止预处理语句SQL注入攻击](http://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks) |
| 88 | + |
| 89 | +MySQL 提供了 `execute` 辅助函数,它将准备和查询语句。 您还可以使用 `prepare` / `unprepare` 方法手动准备/取消准备。 |
| 90 | + |
| 91 | +```js |
| 92 | +// 导入模块 |
| 93 | +const mysql = require('mysql2'); |
| 94 | + |
| 95 | +// 创建一个数据库连接 |
| 96 | +const connection = mysql.createConnection({ |
| 97 | + host: 'localhost', |
| 98 | + user: 'root', |
| 99 | + database: 'test' |
| 100 | +}); |
| 101 | + |
| 102 | +// execute 将在内部调用 prepare 和 query |
| 103 | +connection.execute( |
| 104 | + 'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', |
| 105 | + ['Rick C-137', 53], |
| 106 | + function(err, results, fields) { |
| 107 | + console.log(results); // 结果集 |
| 108 | + console.log(fields); // 额外元数据(如果有) |
| 109 | + |
| 110 | + // 如果再次执行相同的语句,他将从缓存中选取 |
| 111 | + // 这能有效的节省准备查询时间获得更好的性能 |
| 112 | + } |
| 113 | +); |
| 114 | +``` |
| 115 | + |
| 116 | +## 连接池的使用 |
| 117 | + |
| 118 | +连接池通过重用以前的连接来帮助减少连接到 MySQL 服务器所花费的时间,当你完成它们时让它们保持打开而不是关闭。 |
| 119 | + |
| 120 | +这改善了查询的延迟,因为您避免了建立新连接所带来的所有开销。 |
| 121 | + |
| 122 | +```js |
| 123 | +// 导入模块 |
| 124 | +const mysql = require('mysql2'); |
| 125 | + |
| 126 | +// 创建连接池,设置连接池的参数 |
| 127 | +const pool = mysql.createPool({ |
| 128 | + host: 'localhost', |
| 129 | + user: 'root', |
| 130 | + database: 'test', |
| 131 | + waitForConnections: true, |
| 132 | + connectionLimit: 10, |
| 133 | + queueLimit: 0 |
| 134 | +}); |
| 135 | +``` |
| 136 | +该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。 |
| 137 | + |
| 138 | +您可以像直接连接一样使用池(使用 `pool.query()` 和 `pool.execute()`): |
| 139 | +```js |
| 140 | +// For pool initialization, see above |
| 141 | +pool.query("SELECT field FROM atable", function(err, rows, fields) { |
| 142 | + // Connection is automatically released when query resolves |
| 143 | +}) |
| 144 | +``` |
| 145 | + |
| 146 | +或者,也可以手动从池中获取连接并稍后返回: |
| 147 | +```js |
| 148 | +// For pool initialization, see above |
| 149 | +pool.getConnection(function(err, conn) { |
| 150 | + // Do something with the connection |
| 151 | + conn.query(/* ... */); |
| 152 | + // Don't forget to release the connection when finished! |
| 153 | + pool.releaseConnection(conn); |
| 154 | +}) |
| 155 | +``` |
| 156 | + |
| 157 | +## Promise封装 |
| 158 | + |
| 159 | +MySQL2 也支持 Promise API。 这与 ES7 异步等待非常有效。 |
| 160 | + |
| 161 | +<!--eslint-disable-next-block--> |
| 162 | +```js |
| 163 | +async function main() { |
| 164 | + // get the client |
| 165 | + const mysql = require('mysql2/promise'); |
| 166 | + // create the connection |
| 167 | + const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test'}); |
| 168 | + // query database |
| 169 | + const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]); |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +MySQL2 使用范围内可用的默认 `Promise` 对象。 但是你可以选择你想使用的 `Promise` 实现 |
| 174 | + |
| 175 | +<!--eslint-disable-next-block--> |
| 176 | +```js |
| 177 | +// get the client |
| 178 | +const mysql = require('mysql2/promise'); |
| 179 | + |
| 180 | +// get the promise implementation, we will use bluebird |
| 181 | +const bluebird = require('bluebird'); |
| 182 | + |
| 183 | +// create the connection, specify bluebird as Promise |
| 184 | +const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test', Promise: bluebird}); |
| 185 | + |
| 186 | +// query database |
| 187 | +const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]); |
| 188 | +``` |
| 189 | + |
| 190 | +MySQL2 还在 Pools 上公开了一个 .promise()函数,因此您可以从同一个池创建一个 promise/non-promise 连接 |
| 191 | +```js |
| 192 | +async function main() { |
| 193 | + // get the client |
| 194 | + const mysql = require('mysql2'); |
| 195 | + // create the pool |
| 196 | + const pool = mysql.createPool({host:'localhost', user: 'root', database: 'test'}); |
| 197 | + // now get a Promise wrapped instance of that pool |
| 198 | + const promisePool = pool.promise(); |
| 199 | + // query database using promises |
| 200 | + const [rows,fields] = await promisePool.query("SELECT 1"); |
| 201 | +``` |
| 202 | +
|
| 203 | +MySQL2 在 Connections 上公开了一个 .promise*()函数,以“升级”现有的 non-promise 连接以使用 Promise |
| 204 | +```js |
| 205 | +// get the client |
| 206 | +const mysql = require('mysql2'); |
| 207 | +// create the connection |
| 208 | +const con = mysql.createConnection( |
| 209 | + {host:'localhost', user: 'root', database: 'test'} |
| 210 | +); |
| 211 | +con.promise().query("SELECT 1") |
| 212 | + .then( ([rows,fields]) => { |
| 213 | + console.log(rows); |
| 214 | + }) |
| 215 | + .catch(console.log) |
| 216 | + .then( () => con.end()); |
| 217 | +``` |
| 218 | +
|
| 219 | +## 结果返回 |
| 220 | +
|
| 221 | +如果你有两个相同名称的列,你可能希望以数组而不是对象的形式获取结果,为了防止冲突,这是与 [Node MySQL][node-mysql] 库的区别。 |
| 222 | +
|
| 223 | +例如: `select 1 as foo, 2 as foo`. |
| 224 | +
|
| 225 | +您可以在连接级别(适用于所有查询)或查询级别(仅适用于该特定查询)启用此设置。 |
| 226 | +
|
| 227 | +### 连接参数 |
| 228 | +```js |
| 229 | +const con = mysql.createConnection( |
| 230 | + { host: 'localhost', database: 'test', user: 'root', rowsAsArray: true } |
| 231 | +); |
| 232 | + |
| 233 | +``` |
| 234 | +
|
| 235 | +### 查询参数 |
| 236 | +
|
| 237 | +```js |
| 238 | +con.query({ sql: 'select 1 as foo, 2 as foo', rowsAsArray: true }, function(err, results, fields) { |
| 239 | + console.log(results) // 返回数组而不是数组对象 |
| 240 | + console.log(fields) // 无变化 |
| 241 | +}); |
| 242 | + |
| 243 | +``` |
| 244 | +
|
| 245 | +## API配置项 |
| 246 | +
|
| 247 | +MySQL2大部分的API与 [Node MySQL][node-mysql] 基本上相同,你应该查看他们的API文档来知道更多的API选项。 |
| 248 | +
|
| 249 | +如果您发现与 [Node MySQL][node-mysql] 的任何不兼容问题,请通过`isesue`报告。 我们将优先修复报告的不兼容问题。 |
| 250 | +
|
| 251 | +## 文档 |
| 252 | +
|
| 253 | +你可以在[这里](https://github.com/sidorares/node-mysql2/tree/master/documentation)获得更多的详细文档,并且你应该查阅各种代码[示例](https://github.com/sidorares/node-mysql2/tree/master/examples)来获得更高级的概念。 |
| 254 | +
|
| 255 | +## 鸣谢 |
| 256 | +
|
| 257 | + - 内部协议由@sidorares编写 [MySQL-Native](https://github.com/sidorares/nodejs-mysql-native) |
| 258 | + - 常量、SQL参数插值、连接池、`ConnectionConfig` 类取自[node-mysql](https://github.com/mysqljs/mysql) |
| 259 | + - 基于@TooTallNate的SSL代码升级[代码地址](https://gist.github.com/TooTallNate/848444) |
| 260 | + - 与[MariaSQL](https://github.com/mscdex/node-mariasql/)客户端兼容安全连接/压缩连接 API。 |
| 261 | + - [贡献者](https://github.com/sidorares/node-mysql2/graphs/contributors) |
| 262 | +
|
| 263 | +## 贡献 |
| 264 | +
|
| 265 | +如果要为`node-mysql2`做些贡献.请查阅 [Contributing.md](https://github.com/sidorares/node-mysql2/blob/master/Contributing.md) 来获得更多详细信息。 |
| 266 | +
|
| 267 | +
|
| 268 | +[npm-image]: https://img.shields.io/npm/v/mysql2.svg |
| 269 | +[npm-url]: https://npmjs.org/package/mysql2 |
| 270 | +[node-version-image]: http://img.shields.io/node/v/mysql2.svg |
| 271 | +[node-version-url]: http://nodejs.org/download/ |
| 272 | +[travis-image]: https://img.shields.io/travis/sidorares/node-mysql2/master.svg?label=linux |
| 273 | +[travis-url]: https://travis-ci.org/sidorares/node-mysql2 |
| 274 | +[appveyor-image]: https://img.shields.io/appveyor/ci/sidorares/node-mysql2/master.svg?label=windows |
| 275 | +[appveyor-url]: https://ci.appveyor.com/project/sidorares/node-mysql2 |
| 276 | +[downloads-image]: https://img.shields.io/npm/dm/mysql2.svg |
| 277 | +[downloads-url]: https://npmjs.org/package/mysql2 |
| 278 | +[license-url]: https://github.com/sidorares/node-mysql2/blob/master/License |
| 279 | +[license-image]: https://img.shields.io/npm/l/mysql2.svg?maxAge=2592000 |
| 280 | +[node-mysql]: https://github.com/mysqljs/mysql |
| 281 | +[mysql-native]: https://github.com/sidorares/nodejs-mysql-native |
0 commit comments