If you want to install and run mysql in node js then please follow the below instruction step by step.

Step1: First run wamp or mamp or lamp its required first to run mysql in node js

Step2: https://nodejs.org/en/   download node run and install it.

Step3: After successfull install you will see below message (I am using mac machine so its mac machine related message)

Node.js was installed at

   /usr/local/bin/node

npm was installed at

   /usr/local/bin/npm

 

Step4: Open terminal

Step5: echo $PATH

ex in my laptop below command:

9844542127-mac1:node-mysql jay$ echo $PATH
 

Step6: Now you can create any folder and go to that folder through terminal

In my case I have created a folder node-mysql in my laptop inside MAMP

and I have used below command to go there:

9844542127-mac1:~ jay$ cd 9844542127/
9844542127-mac1:9844542127 jay$ cd im-mac-desktop/
9844542127-mac1:im-mac-desktop jay$ ls
moodledata    php-mamp-wamp
9844542127-mac1:im-mac-desktop jay$ cd php-mamp-wamp/
9844542127-mac1:php-mamp-wamp jay$ cd node-mysql/

Step7:  use below command to update node

npm update

Step8: Finall this command for install mysql inside node.
9844542127-mac1:node-mysql jay$sudo npm install mysql

Step9: You have to create a file app.js inside node-mysql folder and put below content

var mysql = require("mysql");

// First you need to create a connection to the db your MAMPmysql  user name and password
var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root"
});

con.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end(function(err) {
  // The connection is terminated gracefully
  // Ensures all previously enqueued queries are still
  // before sending a COM_QUIT packet to the MySQL server.
});
9844542127-mac1:node-mysql jay$ node app.js
Connection established

 

 

Thats all. Hope it will help you.
 

 

Share on Facebook