Install MySQL on Node.js

Add mysql dependency in the package.json file in the root of your project. Example of a package.json file:


{
  "name": "Dribbit",
  "description": "My default description",
  "private": true,
  "dependencies": {
    "mysql": "~2.0.0"
  },
  "repository": {
    "type": "git",
    "url": "git@bitbucket.org:UserName/repo.git"
  }
}

After saving package.json, run

npm install 

from command line to install the node MySQL package.

You can now connect to MySQL, the following example can be implemented in the node webserver. This is a very simple example but it should be enough to understand the basics.


	var mysql = require('mysql');
	var connection = mysql.createConnection({
		host: '127.0.0.1',
		user: 'USER',
		password: 'PASS',
		database: 'DATABASE'
	});
	
	connection.query('SHOW TABLES',function (error, rows, fields) {
		
		if (error) 
			return console.log(error);
		
		console.log(rows);

	});
	
	connection.end();