gpt4 book ai didi

mysql - sqlnode.js错误10061

转载 作者:行者123 更新时间:2023-11-29 18:10:23 26 4
gpt4 key购买 nike

我正在尝试使用 node.js 创建我的第一个数据库,但无法连接到服务器。它给了我错误 1061。我在创建数据库方面绝对是新手,我不知道我的错误在哪里。有人可以吗帮帮我?
谢谢!
这是我的代码:

var mysql = require('mysql');

var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});

con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});


const http = require('http');
var qs = require('querystring');
var url = require('url');
const server = http.createServer();
server.on('request', (request, response) => {
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
console.log(query);
var body = '';
response.setHeader("Access-Control-Allow-Origin", "*");
request.on('data', (chunk) => {
body += chunk;
}).on('end', () => {
var post = qs.parse(body);
console.log(post);
if(query.command == "register") {
con.query("INSERT INTO customers (name, email, password)
VALUES(?,?,?)", [post.name, post.email, post.password], function (err, result, fields) {
if (err) throw err;
console.log(result);
});
}
response.statusCode = 200;
response.end();
});
});
server.listen(8080);

这是控制台输出:

    C:\Users\HP>node server.js
C:\Users\HP\server.js:11
if (err) throw err;
^

Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
--------------------
at Protocol._enqueue (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (C:\Users\HP\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\Users\HP\server.js:10:5)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Function.Module.runMain (module.js:682:10)

最佳答案

此错误意味着node.js无法连接到您的MySQL服务器。很可能您的 MySQL 服务器根本没有启动。

关于mysql - sqlnode.js错误10061,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com