Pular para o conteúdo principal

createConnection

info

For queries please see the Simple Queries and Prepared Statements examples.

createConnection(connectionUri)

createConnection(connectionUri: string)

import mysql from 'mysql2/promise';

try {
const connection = await mysql.createConnection(
'mysql://root:password@localhost:3306/test'
);
} catch (err) {
console.log(err);
}

createConnection(config)

createConnection(config: ConnectionOptions)

import mysql from 'mysql2/promise';

try {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
} catch (err) {
console.log(err);
}

createConnection(config) — SHA1

createConnection(config: ConnectionOptions)

import mysql from 'mysql2/promise';

try {
const connection = await mysql.createConnection({
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
} catch (err) {
console.log(err);
}

createConnection(config) — SSL

createConnection(config: ConnectionOptions)

import mysql from 'mysql2/promise';

try {
const connection = await mysql.createConnection({
// ...
ssl: {
// key: fs.readFileSync('./certs/client-key.pem'),
// cert: fs.readFileSync('./certs/client-cert.pem')
ca: fs.readFileSync('./certs/ca-cert.pem'),
},
});
} catch (err) {
console.log(err);
}

createConnection(config) — RDS SSL

createConnection(config: ConnectionOptions)

You can use Amazon RDS string as value to ssl property to connect to Amazon RDS MySQL over SSL.

In that case https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used:

npm install --save aws-ssl-profiles
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';

try {
const connection = await mysql.createConnection({
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
} catch (err) {
console.log(err);
}
info

For detailed instructions, please follow the AWS SSL Profiles documentation.

Testing
try {
const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
await connection.end();

console.log(res);
} catch (err) {
console.log(err);
}

createConnection(config) — Socks

createConnection(config: ConnectionOptions)

const mysql = require('mysql2');
const SocksConnection = require('socksjs');

const socksProxy = new SocksConnection({ port: 3306 });
const connection = mysql.createConnection({
stream: socksProxy,
});

connection.addListener('error', (err) => {
console.log(err);
});
Testing
connection.execute('SELECT SLEEP(1.1) AS `www`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

connection.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

connection.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

Glossary

ConnectionOptions

ConnectionOptions Specification