node js and mysql localhots refused connection.

Nigel Wright 291 Reputation points
2021-04-03T00:06:40.82+00:00

I am getting "localhost refused connection" error.
With same parameters I can open the connection in workbench ok.

'use strict';  
var http = require('http');  
var port = process.env.PORT ||1137;  
  
http.createServer(function (req, res) {  
    res.writeHead(200, { 'Content-Type': 'text/plain' });  
    res.end('Hello World333333\n');  
}).listen(port);  
  
  
var mysql = require('mysql');  
  
var con = mysql.createConnection({  
    host: "localhost",  
    user: "root",  
    password: "Nigel7557@"  
});  


  
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
986 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nigel Wright 291 Reputation points
    2021-04-05T06:59:44.347+00:00

    It was a problem with cors.
    Fixed now, thanks for your help.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anurag Sharma 17,631 Reputation points
    2021-04-05T04:47:34.42+00:00

    Hi @Nigel Wright , welcome to Microsoft Q&A forum.

    Could you please try changing the localhost to '127.0.0.1', also check if the port you are connecting to in workbench is correct i.e. 3306?

    Just one last thing, are you using the MySQL on-premise or Azure MySQL.?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.