我是MySQL的新手,似乎无法从Node.js连接到服务器。另外,我在Windows而不是UNIX上。
我能够使用登录到为节点创建的用户的sqlcmd连接到服务器。我还启用了TCP / IP和命名管道。
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");
程序在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)
答案 0 :(得分:0)
MySQL的默认端口号是3306。
您的createConnection选项对象提到端口1433。这是Microsoft SQL Server的默认端口。尝试从该对象中删除端口元素。或者,如果这不起作用,请将其更改为3306。
答案 1 :(得分:0)
我意识到MicrosoftSQL和MySQL有区别。谢谢所有尝试提供帮助的人!