Category Archives: MySql

How to access MySql database with Node?

In Node, everything goes with modules i.e., download and install module. For accessing MySql , we have module mysql@2.0.0-alpha2.

Steps to access MySql  with Node:

1. Install module:

npm install mysql@2.0.0-alpha2

This will download and install module for mysql.

2. Using module in your application:

var http = require(‘http’),
// And mysql module you’ve just installed.
mysql = require(“mysql”);

// Create the connection.
// Data is default to new mysql installation and should be changed according to your configuration.
var connection = mysql.createConnection({
user: “root”,
password: “”,
database: “db_name”

// Create the http server.
http.createServer(function (request, response) {
// Attach listener on end event.
request.on(‘end’, function () {
// Query the database.
connection.query(‘SELECT * FROM your_table;’, function (error, rows, fields) {
response.writeHead(200, {
‘Content-Type': ‘x-application/json’
// Send data as JSON string.
// Rows variable holds the result of the query.
// Listen on the 8080 port.

Querying with this library is easy as we just need to write query and a callback function.

3. Save this file as mysql.js and run it as

$node mysql.js

4. Then navigate to http://localhost:8080 in your browser, and you should be prompted to download the JSON-formatted file.

ProsperaSoft offers Node.js development solutions. You can email at to get in touch with ProsperaSoft Node.js experts and consultants.