[英]Still getting error connect ECONNREFUSED using node.js MySQL
我仍然收到錯誤:ECONNREFUSED myip+port,我嘗試添加端口,但沒有幫助。 你知道有什么可以幫助的嗎?
這是我的代碼,
//importing mysql and csvtojson packages
const csvtojson = require('csvtojson');
const mysql = require("mysql");
//database definition
const hostname = "127.0.0.1",
username = "root",
password = "root",
databsename = "csvtomysql",
port = "8889"
//connection to the database
let con = mysql.createConnection({
host: hostname,
user: username,
password: password,
database: databsename,
port: port,
});
con.connect((err) => {
if (err) return console.error(
'error: ' + err.message);
con.query("DROP TABLE sample",
(err, drop) => {
// Query to create table "data"
var createStatament =
"CREATE TABLE data(Favorites char(10), " +
"From URL char(1000),To URL char(1000),Anchor Text char(2000),Link Status char(400), Type char(500), BLdom float, DomPop float, Power int, Trust int, Power*Trust float, Alexa float, IP char(100), CNTRY char(5))"
// Creating table "data"
con.query(createStatament, (err, drop) => {
if (err)
console.log("ERROR: ", err);
});
});
});
// CSV file name
const fileName = "testData.csv";
csvtojson().fromFile(fileName).then(source => {
// Fetching the data from each row
// and inserting to the table "sample"
for (var i = 0; i < source.length; i++) {
var Favorites = source[i]["Favorites"],
FromUrl = source[i]["From URL"],
ToUrl = source[i]["To URL"],
AnchorText = source[i]["Anchor Text"],
LinkStatus = source[i]["Link Status"],
Type = source[i]["Type"],
BLdom = source[i]["BLdom"],
DomPop = source[i]["DomPop"],
Power = source[i]["Power"],
Trust = source[i]["Trust"],
PowerTrust = source[i]["Power*Trust"],
Alexa = source[i]["Alexa"],
Ip = source[i]["IP"],
Cntry = source[i]["CNTRY"]
var insertStatement =
`INSERT INTO sample values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
var items = [Favorites, FromUrl, ToUrl, AnchorText, LinkStatus, Type, BLdom, DomPop, Power, Trust, PowerTrust, Alexa, Ip, Cntry];
// Inserting data of current row
// into database
con.query(insertStatement, items,
(err, results, fields) => {
if (err) {
console.log(
"Unable to insert item at row ", i + 1);
return console.log(err);
}
});
}
console.log(
"All items stored into database successfully");
});
ECONNREFUSED 是由於一些錯誤配置而導致的錯誤(顧名思義)
您確定 mysql 服務器在 node.js 后端的同一主機上運行嗎?
Mysql 服務器已啟動並正在運行(嘗試在 Mysql Workbench 上與 DBeaver 等客戶端連接)?
mysql 的標准端口是 3306(不是 8889)
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=csvtomysql -p 8889:3306 -d mysql
你的應用程序應該可以正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.