Node.js with MySQL Examples

Node.js is an popular programming language like PHP & JAVA for web applications. Also MySQL is most popular database used for storing values. MySQL database driver for Node.js is available under NPM repository. In this tutorial you will learn to how to connect MySQL database using node.js and do INSERT, UDATE, SELECT and DELETE operations on MySQL database table.

mysql> GRANT ALL on mydb.* to 'myusr'@'localhost' identified by '123456';
mysql> FLUSH PRIVILEGES;

2. Install Node.js MySQL Module

MySQL driver for node.js is available under node package manager (NPM). Use the following command to install it.

$ sudo npm install mysql

3. Simple App to Connect MySQL

Below is sample node.js program which will connect node.js application with MySQL server. It will show success and error messages according to connections results and close the connection at the end of program. Create a JavaScript file app.js.