最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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

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

Share Improve this question edited Mar 23, 2017 at 7:26 Caitiff asked Mar 23, 2017 at 7:09 CaitiffCaitiff 573 silver badges11 bronze badges 5
  • Check if your database is running on port 3306 – LVK Commented Mar 23, 2017 at 7:12
  • I am just curious why it doesn't work. I have everything set up, the database is supposed to run on that port and if it doesn't then, well idk – Caitiff Commented Mar 23, 2017 at 7:28
  • Okay, I found the 75% of the answer myself. I have the database on specific port 3036, and in the code, it is trying to connect to the default port so that is an error on my part. Still, phpmyadmin refuses to work. Why? – Caitiff Commented Mar 23, 2017 at 7:35
  • Because phpMyAdmin also expects the database to be running on port 3306, you need to edit this too – baao Commented Mar 23, 2017 at 7:36
  • Wow, yeah correct. Okay thank you! – Caitiff Commented Mar 23, 2017 at 7:38
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论