MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl much more
Table of contents
MySQL2 project is a continuation of MySQL-Native. Protocol parser code was rewritten from scratch and api changed to match popular mysqljs/mysql. MySQL2 team is working together with mysqljs/mysql team to factor out shared code and move it under mysqljs organisation.
MySQL2 is mostly API compatible with mysqljs and supports majority of features. MySQL2 also offers these additional features
MySQL2 is free from native bindings and can be installed on Linux, Mac OS or Windows without any issues.
npm install --save mysql2
// get the clientvar mysql = ;// create the connection to databasevar connection = mysql;// simple queryconnection;// with placeholderconnection;
With MySQL2 you also get the prepared statements. With prepared statements MySQL doesn't have to prepare plan for same query everytime, this results in better performance. If you don't know why they are important, please check these discussions
execute helper which will prepare and query the statement. You can also manually prepare / unprepare statement with
// get the clientvar mysql = ;// create the connection to databasevar connection = mysql;// execute will internally call prepare and queryconnection;
MySQL2 also support Promise API. Which works very well with ES7 async await.
// get the clientlet mysql = ;// create the connectionlet connection = await mysql;// query databaselet rows fields = await connection;
MySQL2 use default
Promise object available in scope. But you can choose which
Promise implementation you want to use
// get the clientlet mysql = ;// get the promise implementation, we will use bluebirdlet bluebird = ;// create the connection, specify bluebird as Promiselet connection = await mysql;// query databaselet rows fields = await connection;
MySQL2 is mostly API compatible with Node MySQL. You should check their API documentation to see all available API options.
If you find any incompatibility with Node MySQL, Please report via Issue tracker. We will fix reported incompatibility on priority basis.
ConnectionConfigclass taken from node-mysql
Want to improve something in
node-mysql2. Please check Contributing.md for detailed instruction on how to get started.