简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM