Gayathri
Gayathri

Reputation: 45

nodejs to mysql connection error

I'm getting error : error:Error: Handshake inactivity timeout

Upvotes: 0

Views: 13065

Answers (3)

Manish
Manish

Reputation: 13557

I connected mysql to node by like this.

Install mysql

npm install mysql

var mysql = require('mysql');

let connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    port: '8888',  /* port on which phpmyadmin run */
    password: 'root',
    database: 'dbname',
    socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock' //for mac and linux
});

connection.connect(function(err) {
    if (err) {
      return console.error('error: ' + err.message);
    }

    console.log('Connected to the MySQL server.');
  });

Upvotes: 1

Subburaj
Subburaj

Reputation: 5192

Use Connection Pool:

var mysql = require('mysql');
var pool  = mysql.createPool({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

pool.getConnection(function(err, connection) {
  // Use the connection
  connection.query( 'SELECT something FROM sometable', function(err, rows) {
    // And done with the connection.
    connection.release();

    // Don't use the connection here, it has been returned to the pool.
  });
});

Upvotes: 0

Bik
Bik

Reputation: 553

You can use node-mysql, It very easy to use. I have used once, Here is the example :-

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ', rows[0].solution);
});

connection.end();

Upvotes: 0

Related Questions