createConnection
For queries please see the Simple Queries and Prepared Statements examples.
createConnection(connectionUri)โ
createConnection(connectionUri: string)
- promise.js
- callback.js
- await using
- using
import mysql from 'mysql2/promise';
try {
const connection = await mysql.createConnection(
'mysql://root:password@localhost:3306/test'
);
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const connection = mysql.createConnection(
'mysql://root:password@localhost:3306/test'
);
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
import mysql from 'mysql2/promise';
{
// .end() is called automatically when leaving the scope
await using connection = await mysql.createConnection(
'mysql://root:password@localhost:3306/test'
);
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection(
'mysql://root:password@localhost:3306/test'
);
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
createConnection(config)โ
createConnection(config: ConnectionOptions)
- promise.js
- callback.js
- await using
- using
import mysql from 'mysql2/promise';
try {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
import mysql from 'mysql2/promise';
{
// .end() is called automatically when leaving the scope
await using connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
createConnection(config) โ SHA1โ
createConnection(config: ConnectionOptions)
- promise.js
- callback.js
- await using
- using
import mysql from 'mysql2/promise';
try {
const connection = await mysql.createConnection({
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const connection = mysql.createConnection({
// ...
passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex'),
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
import mysql from 'mysql2/promise';
{
// .end() is called automatically when leaving the scope
await using connection = await mysql.createConnection({
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection({
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
createConnection(config) โ SSLโ
createConnection(config: ConnectionOptions)
- promise.js
- callback.js
- await using
- using
import mysql from 'mysql2/promise';
try {
const connection = await mysql.createConnection({
// ...
ssl: {},
});
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const connection = mysql.createConnection({
// ...
ssl: {},
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
import mysql from 'mysql2/promise';
{
// .end() is called automatically when leaving the scope
await using connection = await mysql.createConnection({
// ...
ssl: {},
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection({
// ...
ssl: {},
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
createConnection(config) โ SSL Custom Certificateโ
createConnection(config: ConnectionOptions)
- promise.js
- callback.js
- await using
- using
- certs/ca-cert.pem
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'),
},
});
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const connection = mysql.createConnection({
// ...
ssl: {
// key: fs.readFileSync('./certs/client-key.pem'),
// cert: fs.readFileSync('./certs/client-cert.pem')
ca: fs.readFileSync('./certs/ca-cert.pem'),
},
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
import mysql from 'mysql2/promise';
{
// .end() is called automatically when leaving the scope
await using 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'),
},
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection({
// ...
ssl: {
// key: fs.readFileSync('./certs/client-key.pem'),
// cert: fs.readFileSync('./certs/client-cert.pem')
ca: fs.readFileSync('./certs/ca-cert.pem'),
},
});
// ... some query
}
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
- See ssl/certs.
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
- promise.js
- callback.js
- await using
- using
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,
});
// ... some query
// Close the connection
await connection.end();
} catch (err) {
console.log(err);
}
For detailed instructions, please follow the AWS SSL Profiles documentation.
try {
const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
await connection.end();
console.log(res);
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');
const connection = mysql.createConnection({
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
For detailed instructions, please follow the AWS SSL Profiles documentation.
connectionquery('SHOW `status` LIKE "Ssl_cipher"', function (err, res) {
connection.end();
if (err instanceof Error) {
console.log(err);
return;
}
console.log(res);
});
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';
{
// .end() is called automatically when leaving the scope
await using connection = await mysql.createConnection({
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// ... some query
}
For detailed instructions, please follow the AWS SSL Profiles documentation.
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');
{
// .end() is called automatically when leaving the scope
using connection = mysql.createConnection({
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// ... some query
}
For detailed instructions, please follow the AWS SSL Profiles documentation.
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
createConnection(config) โ Socksโ
createConnection(config: ConnectionOptions)
- A.js
- B.js
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);
});
// ... some query
// Close the connection
connection.end();
const mysql = require('mysql2');
const SocksConnection = require('socksjs');
const connection = mysql.createConnection({
debug: 1,
stream: function () {
return new SocksConnection({ port: 3306 });
},
});
connection.addListener('error', (err) => {
console.log(err);
});
// ... some query
// Close the connection
connection.end();
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);
});