Node MySQL 2
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
- History and Why MySQL2
- Installation
- First Query
- Using Prepared Statements
- Using connection pools
- Using Promise Wrapper
- API and Configuration
- Documentation
- Acknowledgements
- Contributing
History and Why MySQL2
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
- Faster / Better Performance
- Prepared Statements
- MySQL Binary Log Protocol
- MySQL Server
- Extended support for Encoding and Collation
- Promise Wrapper
- Compression
- SSL and Authentication Switch
- Custom Streams
- Pooling
Installation
MySQL2 is free from native bindings and can be installed on Linux, Mac OS or Windows without any issues.
npm install --save mysql2
First Query
// get the clientconst mysql = ; // create the connection to databaseconst connection = mysql; // simple queryconnection; // with placeholderconnection;
Using Prepared Statements
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
MySQL provides execute
helper which will prepare and query the statement. You can also manually prepare / unprepare statement with prepare
/ unprepare
methods.
// get the clientconst mysql = ; // create the connection to databaseconst connection = mysql; // execute will internally call prepare and queryconnection;
Using connection pools
Connection pools help reduce the time spent connecting to the MySQL server by reusing a previous connection, leaving them open instead of closing when you are done with them.
This improves the latency of queries as you avoid all of the overhead that comes with establishing a new connection.
// get the clientconst mysql = ; // Create the connection pool. The pool-specific settings are the defaultsconst pool = mysql;
The pool does not create all connections upfront but creates them on demand until the connection limit is reached.
You can use the pool in the same way as connections (using pool.query()
and pool.execute()
):
// For pool initialization, see abovepool
Alternatively, there is also the possibility of manually acquiring a connection from the pool and returning it later:
// For pool initialization, see abovepool
Using Promise Wrapper
MySQL2 also support Promise API. Which works very well with ES7 async await.
{ // get the client const mysql = ; // create the connection const connection = await mysql; // query database const 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 clientconst mysql = ; // get the promise implementation, we will use bluebirdconst bluebird = ; // create the connection, specify bluebird as Promiseconst connection = await mysql; // query databaseconst rows fields = await connection;
MySQL2 also exposes a .promise() function on Pools, so you can create a promise/non-promise connections from the same pool
{ // get the client const mysql = ; // create the pool const pool = mysql; // now get a Promise wrapped instance of that pool const promisePool = pool; // query database using promises const rowsfields = await promisePool;
MySQL2 exposes a .promise() function on Connections, to "upgrade" an existing non-promise connection to use promise
{ // get the client const mysql = ; // create the connection mysql;
API and Configuration
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.
Documentation
You can find more detailed documentation here. You should also check various code examples to understand advanced concepts.
Acknowledgements
- Internal protocol is written by @sidorares MySQL-Native
- Constants, SQL parameters interpolation, Pooling,
ConnectionConfig
class taken from node-mysql - SSL upgrade code based on @TooTallNate code
- Secure connection / compressed connection api flags compatible to MariaSQL client.
- Contributors
Contributing
Want to improve something in node-mysql2
. Please check Contributing.md for detailed instruction on how to get started.