Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mysql2

Package Overview
Dependencies
Maintainers
3
Versions
186
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysql2

fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS

  • 3.11.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.4M
decreased by-12.7%
Maintainers
3
Weekly downloads
 
Created

What is mysql2?

The mysql2 npm package is a fast and efficient MySQL client for Node.js that provides an easy-to-use API for interacting with MySQL databases. It supports promises and async/await, connection pooling, prepared statements, and more.

What are mysql2's main functionalities?

Basic Connection

This feature allows you to create a basic connection to a MySQL database and execute a query.

const mysql = require('mysql2');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', database: 'test' });
connection.query('SELECT * FROM `table` WHERE `name` = "Page"', function(err, results) { console.log(results); });
connection.end();

Promise Wrapper

This feature provides a promise-based API for working with MySQL, which allows for the use of async/await for better asynchronous control flow.

const mysql = require('mysql2/promise');
async function queryDatabase() {
  const connection = await mysql.createConnection({ host: 'localhost', user: 'root', database: 'test' });
  const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ?', ['Page']);
  console.log(rows);
  connection.end();
}
queryDatabase();

Connection Pooling

This feature allows you to create a pool of connections that can be reused, which is more efficient than creating a new connection for every query.

const mysql = require('mysql2');
const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'root', database: 'test', waitForConnections: true, connectionLimit: 10, queueLimit: 0 });
pool.query('SELECT * FROM `table`', function(err, results, fields) { console.log(results); });
pool.end();

Prepared Statements

This feature allows you to use prepared statements, which can improve performance and security by pre-compiling SQL queries and avoiding SQL injection.

const mysql = require('mysql2');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', database: 'test' });
const statement = connection.prepare('SELECT * FROM `table` WHERE `id` = ?');
statement.execute([1], function(err, results) { console.log(results); });
statement.close();
connection.end();

Other packages similar to mysql2

Keywords

FAQs

Package last updated on 05 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc