1. Home
  2. Node.js
  3. Node.js with MySQL
  4. Node.js – MySQL Connection

Node.js – MySQL Connection

Use mysql module available on npmjs repository to connect MySQL database and execute queries from Node.js application.

Install Node.js MySQL Driver

You must have MySQL module installed in your application. You can install it using the following command.

npm install mysql

Node.js Connect to MySQL

Create a JavaScript file in your application and edit it in your favorite text editor.

 vim connect_mysql.js

Add the following content. Change the MySQL connection values as per your system setup.

Test Connection

Finally, execute the connect_mysql.js file using node. On successful connection, it will display message like below.

 node connect_mysql.js

Tags ,