A
A
Alexander Fadeev2015-04-02 18:06:16
MySQL
Alexander Fadeev, 2015-04-02 18:06:16

Why does the application go into error when trying to connect to mysql under node.js?

when you try to execute this code, the application crashes with an error

var express = require('express');

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  database : 'db',
  user     : 'root',
  password : 'password'
  
});


var app = express();

app.get('/', function (req, res) {
    connection.connect();

connection.query('SELECT * FROM users WHERE id = 1', function(err, rows) {
  if (err) throw err;
});
});



var server = app.listen(8001, function () {
});

if (err) throw err;
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:983:19)
--------------- -----
at Protocol._enqueue (/Users/aleksandrfadeev/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/Users/aleksandrfadeev/node_modules/mysql/lib/protocol/Protocol .js:52:41)
at Connection.connect (/Users/aleksandrfadeev/node_modules/mysql/lib/Connection.js:109:18)
at /Users/aleksandrfadeev/NetBeansProjects/NodeJS/public_html/server.js:37:16
at Layer.handle [as handle_request] (/Users/aleksandrfadeev/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/aleksandrfadeev/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/Users/aleksandrfadeev/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/Users/aleksandrfadeev/node_modules/express/lib/router/layer.js:82:5
) 22
at param (/Users/aleksandrfadeev/node_modules/express/lib/router/index.js:340:14)
from under php with the same parameters connects to mysql normally

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
polozad, 2015-04-02
@polozad

mysql writes something to the log?

T
Timur Shemsedinov, 2015-04-02
@MarcusAurelius

Most likely the first option will help:

  1. Move connection.connect(); in right after the definition of var connection
  2. Try replacing 127.0.0.1 with localhost
  3. Check port 3306, you can explicitly set it in createConnection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question