Skip to content

Commit a446fac

Browse files
docs: document SSL options (#3384)
* Update 00-index.mdx * Create ssl.mdx * Update ssl.mdx * Update ssl.mdx * Update ssl.mdx * add default cert example * Update website/docs/documentation/ssl.mdx Co-authored-by: Weslley Araújo <[email protected]> * Update website/docs/documentation/ssl.mdx Co-authored-by: Weslley Araújo <[email protected]> * Update website/docs/documentation/ssl.mdx Co-authored-by: Weslley Araújo <[email protected]> * Update website/docs/documentation/ssl.mdx Co-authored-by: Weslley Araújo <[email protected]> * Update website/docs/documentation/ssl.mdx Co-authored-by: Weslley Araújo <[email protected]> * some changes * lint * Update website/docs/documentation/ssl.mdx --------- Co-authored-by: Weslley Araújo <[email protected]>
1 parent 37e4e74 commit a446fac

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

website/docs/documentation/00-index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Not only **MySQL2** offers better performance over [Node MySQL][node-mysql], we
2525
- [More Features](/docs/documentation/extras)
2626
- [MySQL Server](/docs/documentation/mysql-server)
2727
- Pooling
28-
- SSL
28+
- [SSL](/docs/documentation/ssl)
2929
- MySQL Compression
3030
- Binary Log Protocol Client
3131

website/docs/documentation/ssl.mdx

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SSL
2+
3+
As part of the connection options, you can specify the `ssl` object property or a string containing the SSL profile content (**deprecated**).
4+
5+
```ts
6+
ssl?: string | SslOptions;
7+
```
8+
9+
See full list of [SslOptions](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L24-L80), which are in the same format as [tls.createSecureContext](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options).
10+
11+
## SSL Options
12+
13+
To enable SSL without manually providing certificates and assuming they are already trusted by the host machine, you can specify an empty object, for example:
14+
15+
```ts
16+
const connection = await mysql.createConnection({
17+
host: 'localhost',
18+
ssl: {},
19+
});
20+
```
21+
22+
You can also specify custom certificate(s) as an individual string or array of strings. Please note the arguments expect a string of the certificate, not a file name to the certificate:
23+
24+
```ts
25+
import fs from 'node:fs';
26+
27+
const connection = await mysql.createConnection({
28+
host: 'localhost',
29+
ssl: {
30+
ca: fs.readFileSync(__dirname + '/mysql-ca.crt'),
31+
},
32+
});
33+
```
34+
35+
When a certificate is read from an environment variable, it's recommended to replace escaped `\n` characters with proper new line characters, for example:
36+
37+
```ts
38+
const connection = await mysql.createConnection({
39+
host: 'localhost',
40+
ssl: {
41+
ca: process.env.DB_SSL_CA?.replace(/\\n/gm, '\n'),
42+
},
43+
});
44+
```
45+
46+
## SSL Certificate Bundle
47+
48+
Alternatively, you can use a bundle with CA certificates. For example for Amazon RDS you could use:
49+
50+
```ts
51+
import awsCaBundle from 'aws-ssl-profiles';
52+
53+
const connection = await mysql.createConnection({
54+
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
55+
ssl: awsCaBundle,
56+
});
57+
```
58+
59+
For detailed instructions, please follow [aws-ssl-profiles](https://github.com/mysqljs/aws-ssl-profiles) documentation.
60+
61+
## SSL Profile (deprecated)
62+
63+
There is also a **deprecated option** allowing to specify a string containing name of SSL profile:
64+
65+
```ts
66+
const connection = await mysql.createConnection({
67+
host: 'localhost',
68+
ssl: 'Amazon RDS',
69+
});
70+
```
71+
72+
Following profiles are included in the package:
73+
74+
- `Amazon RDS` - in this case https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used
75+
76+
## Ignoring Unauthorized SSL Errors
77+
78+
You can also connect to a MySQL server without providing an appropriate CA to trust. **This is highly discouraged** as being insecure.
79+
80+
```ts
81+
const connection = await mysql.createConnection({
82+
host: 'localhost',
83+
ssl: {
84+
// Beware, set `rejectUnauthorized` as `false` is strongly discouraged for security reasons:
85+
rejectUnauthorized: false,
86+
},
87+
});
88+
```

website/docs/examples/connections/create-connection.mdx

+43
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,49 @@ connection.addListener('error', (err) => {
160160
```js
161161
import mysql from 'mysql2/promise';
162162

163+
try {
164+
// highlight-start
165+
const connection = await mysql.createConnection({
166+
// ...
167+
ssl: {},
168+
});
169+
// highlight-end
170+
} catch (err) {
171+
console.log(err);
172+
}
173+
```
174+
175+
</TabItem>
176+
<TabItem value='callback.js'>
177+
178+
```js
179+
const mysql = require('mysql2');
180+
181+
const connection = mysql.createConnection({
182+
// ...
183+
ssl: {},
184+
});
185+
186+
connection.addListener('error', (err) => {
187+
console.log(err);
188+
});
189+
```
190+
191+
</TabItem>
192+
</Tabs>
193+
194+
<hr />
195+
196+
## createConnection(config) — SSL Custom Certificate
197+
198+
> **createConnection(config: [ConnectionOptions](#connectionoptions))**
199+
200+
<Tabs>
201+
<TabItem value='promise.js' default>
202+
203+
```js
204+
import mysql from 'mysql2/promise';
205+
163206
try {
164207
// highlight-start
165208
const connection = await mysql.createConnection({

0 commit comments

Comments
 (0)