You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Incorporated nwoltman's PR#2233 that adds support for authentication using the caching_sha2_password plugin which is the default in MySQL 8
mysqljs#2233
also possible to blacklist default ones. For more information, check
236
237
[Connection Flags](#connection-flags).
237
238
*`ssl`: object with ssl parameters or a string containing name of ssl profile. See [SSL options](#ssl-options).
239
+
*`secureAuth`: required to support `caching_sha2_password` handshakes over insecure connections (default behavior on MySQL 8.0.4 or higher). See [Authentication options](#authentication-options).
238
240
239
241
240
242
In addition to passing these options as an object, you can also use a url
@@ -247,6 +249,82 @@ var connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&ch
247
249
Note: The query values are first attempted to be parsed as JSON, and if that
248
250
fails assumed to be plaintext strings.
249
251
252
+
### Authentication options
253
+
254
+
MySQL 8.0 introduces a new default authentication plugin - [`caching_sha2_password`](https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html).
255
+
This is a breaking change from MySQL 5.7 wherein [`mysql_native_password`](https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html) was used by default.
256
+
257
+
The initial handshake for this plugin will only work if the connection is secure or the server
258
+
uses a valid RSA public key for the given type of authentication (both default MySQL 8 settings).
259
+
By default, if the connection is not secure, the client will fetch the public key from the server
260
+
and use it (alongside a server-generated nonce) to encrypt the password.
261
+
262
+
After a successful initial handshake, any subsequent handshakes will always work, until the
263
+
server shuts down or the password is somehow removed from the server authentication cache.
264
+
265
+
The default connection options provide compatibility with both MySQL 5.7 and MySQL 8 servers.
266
+
267
+
```js
268
+
// default options
269
+
var connection =mysql.createConnection({
270
+
ssl :false,
271
+
secureAuth :true
272
+
});
273
+
```
274
+
275
+
If you are in control of the server public key, you can also provide it explicitly and avoid
276
+
the additional round-trip.
277
+
278
+
```js
279
+
var connection =mysql.createConnection({
280
+
ssl :false,
281
+
secureAuth : {
282
+
key:fs.readFileSync(__dirname+'/mysql-pub.key')
283
+
}
284
+
});
285
+
```
286
+
287
+
As an alternative to providing just the key, you can provide additional options, in the same
288
+
format as [crypto.publicEncrypt](https://nodejs.org/docs/latest-v4.x/api/crypto.html#crypto_crypto_publicencrypt_public_key_buffer),
289
+
which means you can also specify the key padding type.
290
+
291
+
**Caution** MySQL 8.0.4 specifically requires `RSA_PKCS1_PADDING` whereas MySQL 8.0.11 GA (and above) require `RSA_PKCS1_OAEP_PADDING` (which is the default value).
292
+
293
+
```js
294
+
var constants =require('constants');
295
+
296
+
var connection =mysql.createConnection({
297
+
ssl :false,
298
+
secureAuth : {
299
+
key:fs.readFileSync(__dirname+'/mysql-pub.key'),
300
+
padding:constants.RSA_PKCS1_PADDING
301
+
}
302
+
});
303
+
```
304
+
305
+
At least one of these options needs to be enabled for the initial handshake to work. So, the
306
+
following flavour will also work.
307
+
308
+
```js
309
+
var connection =mysql.createConnection({
310
+
ssl :true, // or a valid ssl configuration object
311
+
secureAuth :false
312
+
});
313
+
```
314
+
315
+
If both `secureAuth` and `ssl` options are disabled, the connection will fail.
0 commit comments