Back to Node Mysql2

createPool

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

3.22.318.3 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import { FAQ } from '@site/src/components/FAQ'; import { ExternalCodeEmbed } from '@site/src/components/ExternalCodeEmbed';

createPool

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

createPool(connectionUri)

createPool(connectionUri: string)

<Tabs> <TabItem value='promise.js' default>
js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const pool = mysql.createPool('mysql://root:password@localhost:3306/test');
  const connection = await pool.getConnection();
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  await pool.end();
} catch (err) {
  console.log(err);
}

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='callback.js'>
js
const mysql = require('mysql2');

// highlight-start
const pool = mysql.createPool('mysql://root:password@localhost:3306/test');
// highlight-end

pool.getConnection(function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  pool.end();
});

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='await using'>
ts
import mysql from 'mysql2/promise';

{
  // highlight-start
  await using pool = mysql.createPool(
    'mysql://root:password@localhost:3306/test'
  );
  // .release() is called automatically when leaving the scope
  await using connection = await pool.getConnection();
  // highlight-end

  // ... some query
}

:::tip 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. :::

</TabItem> <TabItem value='using'>
ts
const mysql = require('mysql2');

{
  // highlight-next-line
  using pool = mysql.createPool('mysql://root:password@localhost:3306/test');

  pool.getConnection(function (err, _connection) {
    if (err instanceof Error) {
      console.log(err);
      return;
    }

    // highlight-start
    // .release() is called automatically when leaving the scope
    using connection = _connection;
    // highlight-end

    // ... some query
  });
}

:::tip 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. :::

</TabItem> </Tabs> <hr />

createPool(config)

createPool(config: PoolOptions)

<Tabs> <TabItem value='promise.js' default>
js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    database: 'test',
    // port: 3306,
    // password: '',
  });
  const connection = await pool.getConnection();
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  await pool.end();
} catch (err) {
  console.log(err);
}

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='callback.js'>
js
const mysql = require('mysql2');

// highlight-start
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  database: 'test',
  // port: 3306,
  // password: '',
});
// highlight-end

pool.getConnection(function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  pool.end();
});

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='await using'>
ts
import mysql from 'mysql2/promise';

{
  // highlight-start
  await using pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    database: 'test',
    // port: 3306,
    // password: '',
  });
  // .release() is called automatically when leaving the scope
  await using connection = await pool.getConnection();
  // highlight-end

  // ... some query
}

:::tip 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. :::

</TabItem> <TabItem value='using'>
ts
const mysql = require('mysql2');

{
  // highlight-next-line
  using pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    database: 'test',
    // port: 3306,
    // password: '',
  });

  pool.getConnection(function (err, _connection) {
    if (err instanceof Error) {
      console.log(err);
      return;
    }

    // highlight-start
    // .release() is called automatically when leaving the scope
    using connection = _connection;
    // highlight-end

    // ... some query
  });
}

:::tip 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. :::

</TabItem> </Tabs> <hr />

createPool(config) — SHA1

createPool(config: PoolOptions)

<Tabs> <TabItem value='promise.js' default>
js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const pool = mysql.createPool({
    // ...
    passwordSha1: Buffer.from(
      '8bb6118f8fd6935ad0876a3be34a717d32708ffd',
      'hex'
    ),
  });
  const connection = await pool.getConnection();
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  await pool.end();
} catch (err) {
  console.log(err);
}

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='callback.js'>
js
const mysql = require('mysql2');

// highlight-start
const pool = mysql.createPool({
  // ...
  passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex'),
});
// highlight-end

pool.getConnection(function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  pool.end();
});

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='await using'>
ts
import mysql from 'mysql2/promise';

{
  // highlight-start
  await using pool = mysql.createPool({
    // ...
    passwordSha1: Buffer.from(
      '8bb6118f8fd6935ad0876a3be34a717d32708ffd',
      'hex'
    ),
  });
  // .release() is called automatically when leaving the scope
  await using connection = await pool.getConnection();
  // highlight-end

  // ... some query
}

:::tip 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. :::

</TabItem> <TabItem value='using'>
ts
const mysql = require('mysql2');

{
  // highlight-next-line
  using pool = mysql.createPool({
    // ...
    passwordSha1: Buffer.from(
      '8bb6118f8fd6935ad0876a3be34a717d32708ffd',
      'hex'
    ),
  });

  pool.getConnection(function (err, _connection) {
    if (err instanceof Error) {
      console.log(err);
      return;
    }

    // highlight-start
    // .release() is called automatically when leaving the scope
    using connection = _connection;
    // highlight-end

    // ... some query
  });
}

:::tip 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. :::

</TabItem> </Tabs> <hr />

createPool(config) — SSL

createPool(config: PoolOptions)

<Tabs> <TabItem value='promise.js' default>
js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const pool = mysql.createPool({
    // ...
    ssl: {
      // key: fs.readFileSync('./certs/client-key.pem'),
      // cert: fs.readFileSync('./certs/client-cert.pem')
      ca: fs.readFileSync('./certs/ca-cert.pem'),
    },
  });
  const connection = await pool.getConnection();
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  await pool.end();
} catch (err) {
  console.log(err);
}

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='callback.js'>
js
const mysql = require('mysql2');

// highlight-start
const pool = mysql.createPool({
  // ...
  ssl: {
    // key: fs.readFileSync('./certs/client-key.pem'),
    // cert: fs.readFileSync('./certs/client-cert.pem')
    ca: fs.readFileSync('./certs/ca-cert.pem'),
  },
});
// highlight-end

pool.getConnection(function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  pool.end();
});

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

</TabItem> <TabItem value='await using'>
ts
import mysql from 'mysql2/promise';

{
  // highlight-start
  await using pool = mysql.createPool({
    // ...
    ssl: {
      // key: fs.readFileSync('./certs/client-key.pem'),
      // cert: fs.readFileSync('./certs/client-cert.pem')
      ca: fs.readFileSync('./certs/ca-cert.pem'),
    },
  });
  // .release() is called automatically when leaving the scope
  await using connection = await pool.getConnection();
  // highlight-end

  // ... some query
}

:::tip 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. :::

</TabItem> <TabItem value='using'>
ts
const mysql = require('mysql2');

{
  // highlight-next-line
  using pool = mysql.createPool({
    // ...
    ssl: {
      // key: fs.readFileSync('./certs/client-key.pem'),
      // cert: fs.readFileSync('./certs/client-cert.pem')
      ca: fs.readFileSync('./certs/ca-cert.pem'),
    },
  });

  pool.getConnection(function (err, _connection) {
    if (err instanceof Error) {
      console.log(err);
      return;
    }

    // highlight-start
    // .release() is called automatically when leaving the scope
    using connection = _connection;
    // highlight-end

    // ... some query
  });
}

:::tip 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. :::

</TabItem> <TabItem value='certs/ca-cert.pem'> <ExternalCodeEmbed language='plan' url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/test/fixtures/ssl/certs/ca.pem' />
- See [ssl/certs](https://github.com/sidorares/node-mysql2/tree/master/test/fixtures/ssl/certs).
</TabItem> </Tabs> <hr />

createPool(config) — RDS SSL

createPool(config: PoolOptions)

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:

sh
npm install --save aws-ssl-profiles
<Tabs> <TabItem value='promise.js' default>
js
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';

try {
  // highlight-start
  const pool = mysql.createPool({
    // ...
    host: 'db.id.ap-southeast-2.rds.amazonaws.com',
    ssl: awsCaBundle,
  });
  const connection = await pool.getConnection();
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  await pool.end();
} catch (err) {
  console.log(err);
}

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

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

:::tip Testing

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

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

:::

</TabItem> <TabItem value='callback.js'>
js
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');

// highlight-start
const pool = mysql.createPool({
  // ...
  host: 'db.id.ap-southeast-2.rds.amazonaws.com',
  ssl: awsCaBundle,
});
// highlight-end

pool.getConnection(function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  // highlight-next-line
  connection.release();

  // Close the pool
  pool.end();
});

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

:::warning

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

:::

:::tip Testing

js
connectionquery('SHOW `status` LIKE "Ssl_cipher"', function (err, res) {
  pool.end();

  if (err instanceof Error) {
    console.log(err);
    return;
  }

  console.log(res);
});

:::

</TabItem> <TabItem value='await using'>
ts
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';

{
  // highlight-start
  await using pool = mysql.createPool({
    // ...
    host: 'db.id.ap-southeast-2.rds.amazonaws.com',
    ssl: awsCaBundle,
  });
  // .release() is called automatically when leaving the scope
  await using connection = await pool.getConnection();
  // highlight-end

  // ... some query
}

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

:::tip 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. :::

</TabItem> <TabItem value='using'>
ts
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');

{
  // highlight-next-line
  using pool = mysql.createPool({
    // ...
    host: 'db.id.ap-southeast-2.rds.amazonaws.com',
    ssl: awsCaBundle,
  });

  pool.getConnection(function (err, _connection) {
    if (err instanceof Error) {
      console.log(err);
      return;
    }

    // highlight-start
    // .release() is called automatically when leaving the scope
    using connection = _connection;
    // highlight-end

    // ... some query
  });
}

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

:::tip 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. :::

</TabItem> </Tabs> <hr />

createPool(config) — Socks

createPool(config: PoolOptions)

<Tabs> <TabItem value='A.js'>
js
const mysql = require('mysql2');
const SocksConnection = require('socksjs');

const socksProxy = new SocksConnection({ port: 3306 });
// highlight-start
const pool = mysql.createPool({
  stream: socksProxy,
});
// highlight-end

// Close the pool
pool.end();
</TabItem> <TabItem value='B.js'>
js
const mysql = require('mysql2');
const SocksConnection = require('socksjs');

// highlight-start
const pool = mysql.createPool({
  debug: 1,
  stream: function () {
    return new SocksConnection({ port: 3306 });
  },
});
// highlight-end

// Close the pool
pool.end();
</TabItem> </Tabs>

:::tip Testing

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

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

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

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

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

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

:::

<hr />

Glossary

PoolOptions

<blockquote> **PoolOptions** extends all options from **ConnectionOptions**: <FAQ title='ConnectionOptions Specification'> <ExternalCodeEmbed language='ts' url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/typings/mysql/lib/Connection.d.ts' extractMethod='ConnectionOptions' methodType='interface' /> </FAQ> </blockquote> <FAQ title='PoolOptions Specification'> <ExternalCodeEmbed language='ts' url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/typings/mysql/lib/Pool.d.ts' extractMethod='PoolOptions' methodType='interface' /> </FAQ>