Skip to content

Guide · Open source

How to use mysql: install the Node.js MySQL client

Updated · written with AI from the project's own documentation and checked against our catalogue data

mysql (mysqljs/mysql) is a Node.js library that lets JavaScript code connect to a MySQL database and run queries. It is written entirely in JavaScript and implements the MySQL protocol itself, so it does not rely on a separate native MySQL driver. This guide covers what you need before installing, how to install the package with npm, and which projects it suits.

mysqljsmysqljs/mysql · ★ 18.6k · MIT

Quick start

  1. 1Check prerequisites. Confirm Node.js is installed and that you have a running MySQL server the application can reach.
  2. 2Open your project. Change into the root directory of your Node.js project in a terminal.
  3. 3Install the package. Run npm install mysql to add the library as a dependency.
  4. 4Load the library in your code. Require or import the installed mysql package in the JavaScript file that will talk to the database.
  5. 5Connect and run a query. Use the library to open a connection to your MySQL server and run queries from your application code.

What you need before you install mysql

mysql is a client library, so it needs two things around it: a Node.js environment to run your code, and a MySQL server for it to connect to. The library does not include a database. If you do not already have a running MySQL server, you will need to set one up separately before the client can do anything useful.

  • Node.js installed on the machine where your application runs
  • A running MySQL server the application can reach
  • A Node.js project where you can install npm packages

Install mysql with npm

The package is installed through npm, the Node.js package manager. Run the command below from the root of your Node.js project. It adds mysql as a dependency so your code can require it.

npm install mysql

Why a pure JavaScript driver matters

Because mysql is written entirely in JavaScript and implements the MySQL protocol itself, it does not depend on a separate native MySQL driver. This is useful for teams that want to avoid a native compilation step when installing dependencies.

The library is licensed under MIT, so you can use it in commercial and open-source projects under that licence's terms.

  • Pure JavaScript, no native compilation step, according to the project description
  • Implements the MySQL protocol directly
  • MIT licence

What the project is best suited for

mysql fits Node.js backend developers who need to read and write data in MySQL from server applications. It is also a reasonable choice for projects that already run MySQL and want a widely used Node.js client.

  • Node.js backend developers who need to query a MySQL database
  • Teams that want a pure JavaScript driver with no native compilation step
  • Projects that already run MySQL and want a widely used Node.js client

When to pick a different option

mysql is not the right tool for every project. It is a JavaScript library, so it is not useful if your code is not written in Node.js. It is also not the right client for PostgreSQL or other databases, which need their own clients.

If you need a driver with active, recent promotion, check the project's maintenance status yourself before committing. The source material used for this guide did not confirm current maintenance status beyond the last push date.

  • Not for non-Node.js code
  • Not for PostgreSQL or other non-MySQL databases
  • Check current maintenance status before choosing it for a new project

Alternatives to compare

Several other projects work with MySQL from Node.js. Compare them against your needs before choosing one. The projects below are listed as comparable options; this guide does not benchmark them.

  • mysql2
  • Sequelize
  • Knex.js
  • Prisma

What people use it for

  • Reading and writing records in a MySQL database from a Node.js API server
  • Running queries from backend services written in JavaScript
  • Adding MySQL access to a Node.js project without compiling native modules

Frequently asked questions

How do I install mysql for Node.js?

Run npm install mysql from the root of your Node.js project. You also need Node.js installed and a running MySQL server for the library to connect to.

Is mysql free to use?

The mysql package is released under the MIT licence, according to the project information. Check the licence file in the repository for the full terms.

Does mysql need a native MySQL driver installed?

No. mysql is written entirely in JavaScript and implements the MySQL protocol itself, so it does not depend on a separate native MySQL driver.

Can mysql connect to PostgreSQL?

No. mysql is a client for MySQL databases. PostgreSQL needs a different client library.

What is the difference between mysql and mysql2?

Both are Node.js clients for MySQL. This guide does not benchmark or compare them in detail, so review each project's documentation and maintenance status before choosing.

Is mysql still maintained?

The source used for this guide did not confirm current maintenance status beyond the last push date. Check the repository's recent activity and issues before using it in a new project.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

mysql is a good fit for Node.js backend developers who want a pure JavaScript MySQL client with no native compilation step. Pick something else if your code is not in Node.js, if you use a non-MySQL database, or if you need a driver with clearly active, recent promotion, since the maintenance status could not be confirmed from the source.

Install options & downloads