简体   繁体   English

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

[英]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但是,如果我尝试创建一个到数据库的连接,它说连接被拒绝到 127.0.0.1 : 3036

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

I am using XAMMP :我正在使用 XAMMP :

Click here -> MySQL / Apache单击此处 -> 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当我尝试访问 phpMyAdmin 时,我也收到此错误:链接到错误

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对于 phpMyAdmin,您需要在 phpMyAdmin 的顶级目录中找到 config.inc.php 并编辑它所说的行

$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:127.0.0.1而不是 localhost,因为:

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'].如果使用 localhost 作为主机名,MySQL 会忽略此端口号并使用套接字连接,因此如果要连接到与默认端口不同的端口,请使用 127.0.0.1 或 $cfg['Servers'] 中的真实主机名[$i]['主机']。

Documentation on phpMyAdmin phpMyAdmin 上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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