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

3
  • Did ur application server has the permission to access the DB?? Commented Apr 9, 2018 at 5:10
  • Yes,I do have access, In fact am root user Commented Apr 9, 2018 at 7:02
  • The error code i get is 'PROTOCOL_CONNECTION_LOST' Commented Apr 9, 2018 at 7:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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 }
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,
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
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
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(); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.