Skip to content

Commit b34200e

Browse files
committed
Update Chinese Simplified
1 parent dbb344e commit b34200e

File tree

2 files changed

+283
-1
lines changed

2 files changed

+283
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Linux Build][travis-image]][travis-url]
88
[![Windows Build][appveyor-image]][appveyor-url]
99
[![License][license-image]][license-url]
10-
10+
[简体中文 | Chinese Simplified](./documentation_zh-cn/)
1111
> MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl [much more](https://github.com/sidorares/node-mysql2/tree/master/documentation)
1212
1313
__Table of contents__

documentation_zh-cn/README.md

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
## Node MySQL 2
2+
3+
[![Greenkeeper badge](https://badges.greenkeeper.io/sidorares/node-mysql2.svg)](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的历史以及选择原因](#history-and-why-mysql2)
16+
- [安装](#installation)
17+
- [查询数据](#first-query)
18+
- [SQL预处理的使用](#using-prepared-statements)
19+
- [连接池的使用](#using-connection-pools)
20+
- [Promise封装的使用](#using-promise-wrapper)
21+
- [API配置项](#api-and-configuration)
22+
- [文档](#documentation)
23+
- [鸣谢](#acknowledgements)
24+
- [贡献](#contributing)
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 and [Authentication Switch](https://github.com/sidorares/node-mysql2/tree/master/documentation/Authentication-Switch.md)
40+
- [Custom Streams](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 provides `execute` helper which will prepare and query the statement. You can also manually prepare / unprepare statement with `prepare` / `unprepare` methods.
90+
MySQL 提供了 `execute` 辅助函数,它将准备和查询语句。 您还可以使用 `prepare` / `unprepare` 方法手动准备/取消准备。
91+
92+
```js
93+
// 导入模块
94+
const mysql = require('mysql2');
95+
96+
// 创建一个数据库连接
97+
const connection = mysql.createConnection({
98+
host: 'localhost',
99+
user: 'root',
100+
database: 'test'
101+
});
102+
103+
// execute 将在内部调用 prepare 和 query
104+
connection.execute(
105+
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
106+
['Rick C-137', 53],
107+
function(err, results, fields) {
108+
console.log(results); // 结果集
109+
console.log(fields); // 额外元数据(如果有)
110+
111+
// 如果再次执行相同的语句,他将从缓存中选取
112+
// 这能有效的节省准备查询时间获得更好的性能
113+
}
114+
);
115+
```
116+
117+
## 使用连接池
118+
119+
连接池通过重用以前的连接来帮助减少连接到 MySQL 服务器所花费的时间,当你完成它们时让它们保持打开而不是关闭。
120+
121+
这改善了查询的延迟,因为您避免了建立新连接所带来的所有开销。
122+
123+
```js
124+
// 导入模块
125+
const mysql = require('mysql2');
126+
127+
// 创建连接池,设置连接池的参数
128+
const pool = mysql.createPool({
129+
host: 'localhost',
130+
user: 'root',
131+
database: 'test',
132+
waitForConnections: true,
133+
connectionLimit: 10,
134+
queueLimit: 0
135+
});
136+
```
137+
该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。
138+
139+
您可以像直接连接一样使用池(使用 `pool.query()``pool.execute()`):
140+
```js
141+
// For pool initialization, see above
142+
pool.query("SELECT field FROM atable", function(err, rows, fields) {
143+
// Connection is automatically released when query resolves
144+
})
145+
```
146+
147+
或者,也可以手动从池中获取连接并稍后返回:
148+
```js
149+
// For pool initialization, see above
150+
pool.getConnection(function(err, conn) {
151+
// Do something with the connection
152+
conn.query(/* ... */);
153+
// Don't forget to release the connection when finished!
154+
pool.releaseConnection(conn);
155+
})
156+
```
157+
158+
## Promise封装
159+
160+
MySQL2 也支持 Promise API。 这与 ES7 异步等待非常有效。
161+
162+
<!--eslint-disable-next-block-->
163+
```js
164+
async function main() {
165+
// get the client
166+
const mysql = require('mysql2/promise');
167+
// create the connection
168+
const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test'});
169+
// query database
170+
const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
171+
}
172+
```
173+
174+
MySQL2 使用范围内可用的默认 `Promise` 对象。 但是你可以选择你想使用的 `Promise` 实现
175+
176+
<!--eslint-disable-next-block-->
177+
```js
178+
// get the client
179+
const mysql = require('mysql2/promise');
180+
181+
// get the promise implementation, we will use bluebird
182+
const bluebird = require('bluebird');
183+
184+
// create the connection, specify bluebird as Promise
185+
const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test', Promise: bluebird});
186+
187+
// query database
188+
const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
189+
```
190+
191+
MySQL2 还在 Pools 上公开了一个 .promise()函数,因此您可以从同一个池创建一个 promise/non-promise 连接
192+
```js
193+
async function main() {
194+
// get the client
195+
const mysql = require('mysql2');
196+
// create the pool
197+
const pool = mysql.createPool({host:'localhost', user: 'root', database: 'test'});
198+
// now get a Promise wrapped instance of that pool
199+
const promisePool = pool.promise();
200+
// query database using promises
201+
const [rows,fields] = await promisePool.query("SELECT 1");
202+
```
203+
204+
MySQL2 在 Connections 上公开了一个 .promise*()函数,以“升级”现有的 non-promise 连接以使用 Promise
205+
```js
206+
// get the client
207+
const mysql = require('mysql2');
208+
// create the connection
209+
const con = mysql.createConnection(
210+
{host:'localhost', user: 'root', database: 'test'}
211+
);
212+
con.promise().query("SELECT 1")
213+
.then( ([rows,fields]) => {
214+
console.log(rows);
215+
})
216+
.catch(console.log)
217+
.then( () => con.end());
218+
```
219+
220+
## 结果返回
221+
222+
如果你有两个相同名称的列,你可能希望以数组而不是对象的形式获取结果,为了防止冲突,这是与 [Node MySQL][node-mysql] 库的区别。
223+
224+
例如: `select 1 as foo, 2 as foo`.
225+
226+
您可以在连接级别(适用于所有查询)或查询级别(仅适用于该特定查询)启用此设置。
227+
228+
### 连接参数
229+
```js
230+
const con = mysql.createConnection(
231+
{ host: 'localhost', database: 'test', user: 'root', rowsAsArray: true }
232+
);
233+
234+
```
235+
236+
### 查询参数
237+
238+
```js
239+
con.query({ sql: 'select 1 as foo, 2 as foo', rowsAsArray: true }, function(err, results, fields) {
240+
console.log(results) // 返回数组而不是数组对象
241+
console.log(fields) // 无变化
242+
});
243+
244+
```
245+
246+
## API配置项
247+
248+
MySQL2大部分的API与 [Node MySQL][node-mysql] 基本上相同,你应该查看他们的API文档来知道更多的API选项。
249+
250+
如果您发现与 [Node MySQL][node-mysql] 的任何不兼容问题,请通过`isesue`报告。 我们将优先修复报告的不兼容问题。
251+
252+
## 文档
253+
254+
你可以在[这里](https://github.com/sidorares/node-mysql2/tree/master/documentation)获得更多的详细文档,并且你应该查阅各种代码[示例](https://github.com/sidorares/node-mysql2/tree/master/examples)来获得更高级的概念。
255+
256+
## 致谢
257+
258+
- Internal protocol is written by @sidorares [MySQL-Native](https://github.com/sidorares/nodejs-mysql-native)
259+
- Constants, SQL parameters interpolation, Pooling, `ConnectionConfig` class taken from [node-mysql](https://github.com/mysqljs/mysql)
260+
- SSL upgrade code based on @TooTallNate [code](https://gist.github.com/TooTallNate/848444)
261+
- Secure connection / compressed connection api flags compatible to [MariaSQL](https://github.com/mscdex/node-mariasql/) client.
262+
- [Contributors](https://github.com/sidorares/node-mysql2/graphs/contributors)
263+
264+
## 贡献
265+
266+
如果要为`node-mysql2`做些贡献.请查阅 [Contributing.md](https://github.com/sidorares/node-mysql2/blob/master/Contributing.md) 来获得更多详细信息。
267+
268+
269+
[npm-image]: https://img.shields.io/npm/v/mysql2.svg
270+
[npm-url]: https://npmjs.org/package/mysql2
271+
[node-version-image]: http://img.shields.io/node/v/mysql2.svg
272+
[node-version-url]: http://nodejs.org/download/
273+
[travis-image]: https://img.shields.io/travis/sidorares/node-mysql2/master.svg?label=linux
274+
[travis-url]: https://travis-ci.org/sidorares/node-mysql2
275+
[appveyor-image]: https://img.shields.io/appveyor/ci/sidorares/node-mysql2/master.svg?label=windows
276+
[appveyor-url]: https://ci.appveyor.com/project/sidorares/node-mysql2
277+
[downloads-image]: https://img.shields.io/npm/dm/mysql2.svg
278+
[downloads-url]: https://npmjs.org/package/mysql2
279+
[license-url]: https://github.com/sidorares/node-mysql2/blob/master/License
280+
[license-image]: https://img.shields.io/npm/l/mysql2.svg?maxAge=2592000
281+
[node-mysql]: https://github.com/mysqljs/mysql
282+
[mysql-native]: https://github.com/sidorares/nodejs-mysql-native

0 commit comments

Comments
 (0)