Caitiff Caitiff -4 years ago 180
Javascript Question

Node.js with mySQL -> Connection refused to 127.0.0.1 : 3306

First, I have this code here for the server, which works perfectly

var app = require('express')();
var mysql = require('mysql');


app.get('/', function (req, res) {
res.sendFile(__dirname + "/start.html");
});

app.listen(3000);


But, if I try to create a connection to a database, it says that the connection is refused to 127.0.0.1 : 3036

var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'user_data'
});
db.connect();


I am using XAMMP :

Click here -> MySQL / Apache

So what is the problem? I don't understand. I also get this error when I try to access phpMyAdmin :
Link to the error

Answer Source

You need to specify a port as your database is listening on a non standard port

var db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'user_data',
    port: 3036
});
db.connect();

For phpMyAdmin, you need to find config.inc.php in phpMyAdmin's top level directory and edit the line where it says

$cfg['Servers'][$i]['port']

to the port you are using. And you need to change

$cfg['Servers'][$i]['host']

to 127.0.0.1 instead of localhost, because:

If you use localhost as the hostname, MySQL ignores this port number and connects with the socket, so if you want to connect to a port different from the default port, use 127.0.0.1 or the real hostname in $cfg['Servers'][$i]['host'].

Documentation on phpMyAdmin

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download