2

I have been trying to establish connection between Node.js application and mysql and have tried everything and couldn't succeed.

I'm able to connect through PHP application. My port is default where 3306

Can anyone help me to resolve this?

var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root'
  });

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

    console.log('connected as id ' + connection.threadId);
  });   
[This is the error message I got,I can able to connect through my php application][1]

This is the error message I got,I can able to connect through my php application

This is the error message I got,I can able to connect through my php application

Micho
  • 3,929
  • 13
  • 37
  • 40

2 Answers2

2

Uncomment "bind-address" and assign bind-address="0.0.0.0". For More Information please refer this, Solving a "communications link failure" with JDBC and MySQL

harish_sng
  • 433
  • 2
  • 13
1

A better approach to connect to DB is to use pools to connect to DB.

Copying from here

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

pool.getConnection(function(err, connection) {
  // connected! (unless `err` is set)
  connection.release();
});

This also allow you to make parallel calls to your database.

AbhinavD
  • 6,892
  • 5
  • 30
  • 40
  • I used above code but still not got succeeded . var pool = mysql.createPool({ host : 'localhost', user : 'root', password : 'root', database: 'promotool' }); pool.getConnection(function(err, connection) { // connected! (unless `err` is set) console.log(err); connection.release(); });Got Error { Error: Handshake inactivity timeout F:\projects\upload_promotool\node_modules\express\lib\router\index.js:281:22 code: 'PROTOCOL_SEQUENCE_TIMEOUT', fatal: true, timeout: 10000 } – Senthil Kumar Apr 09 '18 at 07:05
  • This is weird. Can you try and specify the timeout manually pool = require('mysql').createPool({ connectionLimit : 1000, connectTimeout : 60 * 60 * 1000, aquireTimeout : 60 * 60 * 1000, timeout : 60 * 60 * 1000, – AbhinavD Apr 09 '18 at 07:56
  • Yeah Its quite wierd stuff,Still got this error code: 'PROTOCOL_SEQUENCE_TIMEOUT', fatal: true, timeout: 10000 } F:\projects\upload_promotool\app.js:23 connection.release(); ^ TypeError: Cannot read property 'release' of undefined – Senthil Kumar Apr 09 '18 at 08:53
  • Is you got `'release' of undefined`, it means your connection object is undefined. make sure you use the same variables as you have used in the callback function – AbhinavD Apr 09 '18 at 08:55
  • pool = require('mysql').createPool({ connectionLimit : 1000, connectTimeout : 60 * 60 * 1000, aquireTimeout : 60 * 60 * 1000, timeout : 60 * 60 * 1000, host : 'localhost', user : 'root', password : 'root', database: 'promotool' }); pool.getConnection(function(err, connection) { // connected! (unless `err` is set) console.log(err); connection.release(); }); – Senthil Kumar Apr 09 '18 at 08:58