|
| 1 | +# Sequelize 用于 MySQL |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +请参阅 [Releases](/releases#mysql-support-table) 以了解支持哪些版本的 MySQL。 |
| 6 | + |
| 7 | + |
| 8 | +要在 MySQL 上使用 Sequelize,你需要安装 `@sequelize/mysql` 方言包: |
| 9 | + |
| 10 | +```bash npm2yarn |
| 11 | +npm i @sequelize/mysql |
| 12 | +``` |
| 13 | + |
| 14 | + |
| 15 | +然后在 Sequelize 构造函数中使用 `MySqlDialect` 作为 dialect 选项: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { Sequelize } from '@sequelize/core'; |
| 19 | +import { MySqlDialect } from '@sequelize/mysql'; |
| 20 | + |
| 21 | +const sequelize = new Sequelize({ |
| 22 | + dialect: MySqlDialect, |
| 23 | + database: 'mydb', |
| 24 | + user: 'myuser', |
| 25 | + password: 'mypass', |
| 26 | + host: 'localhost', |
| 27 | + port: 3306, |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | +## 连接选项 |
| 33 | + |
| 34 | +import ConnectionOptions from './_connection-options.md'; |
| 35 | + |
| 36 | +<ConnectionOptions /> |
| 37 | + |
| 38 | + |
| 39 | +以下选项会原样传递给 Sequelize 用于连接 MySQL 的 `mysql2` 包。 |
| 40 | +更多关于每个选项的作用,请参考 [mysql2 官方文档](https://sidorares.github.io/node-mysql2/docs)。 |
| 41 | + |
| 42 | +为方便起见,下面是仅包含 Sequelize 支持选项的文档摘录: |
| 43 | + |
| 44 | +| 选项 | 说明 | |
| 45 | +| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 46 | +| `database` | 此连接要使用的数据库名称 | |
| 47 | +| `user` | 用于认证的 MySQL 用户名 | |
| 48 | +| `port` | 要连接的端口号(默认:3306) | |
| 49 | +| `host` | 要连接的数据库主机名(默认:localhost) | |
| 50 | +| `localAddress` | 用于 TCP 连接的源 IP 地址 | |
| 51 | +| `password` | 该 MySQL 用户的密码 | |
| 52 | +| `password1` | MySQL 用户密码的别名。在多因素认证场景下更有意义(参见 "password2" 和 "password3") | |
| 53 | +| `password2` | 第二因素认证密码。当 MySQL 用户账户的认证策略要求额外的密码认证方法时必填。详见:https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html | |
| 54 | +| `password3` | 第三因素认证密码。当 MySQL 用户账户的认证策略要求两个额外的认证方法且最后一个需要密码时必填。详见:https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html | |
| 55 | +| `passwordSha1` | _暂无文档说明_ | |
| 56 | +| `socketPath` | 用于连接的 unix 域套接字路径。使用该选项时 host 和 port 会被忽略。 | |
| 57 | +| `ssl` | 包含 ssl 参数的对象或包含 ssl 配置名称的字符串 | |
| 58 | +| `charset` | The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci). If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used. (Default: 'UTF8_GENERAL_CI') | |
| 59 | +| `compress` | _no documentation available_ | |
| 60 | +| `trace` | Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight performance penalty for most calls. (Default: true) | |
| 61 | +| `enableKeepAlive` | Enable keep-alive on the socket. (Default: true) | |
| 62 | +| `isServer` | _no documentation available_ | |
| 63 | +| `insecureAuth` | Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) | |
| 64 | +| `multipleStatements` | Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false) | |
| 65 | +| `waitForConnections` | _no documentation available_ | |
| 66 | +| `connectionLimit` | _no documentation available_ | |
| 67 | +| `connectTimeout` | The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds) | |
| 68 | +| `charsetNumber` | _no documentation available_ | |
| 69 | +| `maxIdle` | _no documentation available_ | |
| 70 | +| `queueLimit` | _no documentation available_ | |
| 71 | +| `idleTimeout` | _no documentation available_ | |
| 72 | +| `maxPreparedStatements` | _no documentation available_ | |
| 73 | +| `keepAliveInitialDelay` | If keep-alive is enabled users can supply an initial delay. (Default: 0) | |
| 74 | +| `infileStreamFactory` | By specifying a function that returns a readable stream, an arbitrary stream can be sent when sending a local fs file. | |
| 75 | +| `flags` | List of connection flags to use other than the default ones. It is also possible to denylist default ones | |
| 76 | +| `authSwitchHandler` | _no documentation available_ | |
| 77 | +| `connectAttributes` | _no documentation available_ | |
| 78 | +| `authPlugins` | _no documentation available_ | |
| 79 | +| `debug` | This will print all incoming and outgoing packets on stdout. You can also restrict debugging to packet types by passing an array of types (strings) to debug; | |
| 80 | +| `stream` | _no documentation available_ | |
| 81 | + |
| 82 | +## Other MySQL Options |
| 83 | + |
| 84 | +The following options are also available for MySQL: |
| 85 | + |
| 86 | +| Option | Description | |
| 87 | +| -------------- | ------------------------------------------------------------------------------------------------------------------------ | |
| 88 | +| `showWarnings` | If `true`, warnings produced during the execution of a query will be sent to the `logging` callback. Default is `false`. | |
0 commit comments