0

I am new to MySQL and cannot seem to connect to the server from Node.js. Also, I am on windows, not UNIX.

I am able to connect to the server using sqlcmd logged into the user I created for node. I have also enabled TCP/IP and named pipes.

const express = require("express");
const mysql = require("mysql");

const app = express();
const sqlServer = mysql.createConnection({
    server: "127.0.0.1",
    port: "1433",
    user: "MeetMe",
    password: "dOI9Zham1f5xOJAvweUIvuWlc"
});

const SELECT_ALL_QUERY = "SELECT * FROM Accounts";

sqlServer.connect(function(err) {
    console.log("Connected to sql server");
    if (err) throw err;
});

app.get("/", (req, res) => {
    sqlServer.query(SELECT_ALL_QUERY, function(err, results, fields) {
        if (err) throw err;
        console.log(results);
    });
    res.send("Hello world");
});

app.listen(4000);
console.log("Server running on port 4000");

The program always throws an error during sqlServer.connect().

C:\dev\Webapps\meet-me\server.js:16
    if (err) throw err;
             ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
    --------------------
    at Protocol._enqueue (C:\dev\Webapps\meet-me\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\dev\Webapps\meet-me\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\dev\Webapps\meet-me\node_modules\mysql\lib\Connection.js:119:18)
    at Object.<anonymous> (C:\dev\Webapps\meet-me\server.js:14:11)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
  • https://stackoverflow.com/questions/22900931/mysql-giving-read-econnreset-error-after-idle-time-on-node-js-server – Avanthika May 12 '19 at 23:12
  • @Avanthika I saw this but, I don't think this is the issue unless I misunderstand the connect method. I don't get the "Connected to SQL server" logged so I don't believe the connection is ever completed so it doesn't idle. I am also not able to query with the current setup. It is as though it is trying to connect but times out after a few minutes. – Tyler Johnson May 12 '19 at 23:21

2 Answers2

0

MySQL's default port number is 3306.

Your createConnection options object mentions port 1433. That's the default port for Microsoft SQL server. Try removing the port element from that object. Or, if that doesn't work, change it to 3306.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • If I do that I get ECONNREFUSED immediately. Also if you could explain the difference that would be appreciated. I have been confused about why there are 2 different ports referenced in different things I read. – Tyler Johnson May 12 '19 at 23:28
0

I realized there is a difference in MicrosoftSQL and MySQL. Thank you for everyone that tried to help!