About the problem of mySQL connection

first of all, the system uses the manjaro (linux) system. I installed mySQL, in the project directory, but when I node timetrack_server.js the file, I made the following error:

/home/dema/codeSpace/studyNode/testMySQL/timetrack_server.js:49
        if (err) throw err;
                 ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1121:14)
    --------------------
    at Protocol._enqueue (/home/dema/codeSpace/studyNode/testMySQL/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/dema/codeSpace/studyNode/testMySQL/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/home/dema/codeSpace/studyNode/testMySQL/node_modules/mysql/lib/Connection.js:118:18)
    at Connection._implyConnect (/home/dema/codeSpace/studyNode/testMySQL/node_modules/mysql/lib/Connection.js:453:10)
    at Connection.query (/home/dema/codeSpace/studyNode/testMySQL/node_modules/mysql/lib/Connection.js:198:8)
    at Object.<anonymous> (/home/dema/codeSpace/studyNode/testMySQL/timetrack_server.js:40:4)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
The code for

timetrack_server.js is as follows:

var http = require("http");
var work = require("./lib/timetrack");
var mysql = require("mysql"); //MySQL API

var db = mysql.createConnection({  //MySQL
    host: "127.0.0.1",
    user: "myuser",
    password: "mypassword",
    database: "timetrack"
});

var server = http.createServer(function (req, res) {
    switch (req.method) {
        case "POST":  //HTTP POST
            switch (req.url) {
                case "/":
                    work.add(db, req, res);
                    break;
                case "/archive":
                    work.archive(db, req, res);
                    break;
                case "/delete":
                    work.delete(db, req, res);
                    break;
            }
            break;
        case "GET":  //HTTP GET
            switch (req.url) {
                case "/":
                    work.show(db, res);
                    break;
                case "/archived":
                    work.showArchived(db, res);
            }
            break;
    }
});

db.query(
    "CREATE TABLE IF NOT EXISTS work ("  //SQL
    + "id INT(10) NOT NULL AUTO_INCREMENT, "
    + "hours DECIMAL(5,2) DEFAULT 0, "
    + "date DATE, "
    + "archived INT(1) DEFAULT 0, "
    + "description LONGTEXT,"
    + "PRIMARY KEY(id))",
    function (err) {
        if (err) throw err;
        console.log("Server started...");
        server.listen(3000, "127.0.0.1");   //HTTP
    }
);

A vegetable chicken at the front end. I use the database for the first time. I don"t know where the problem is. I hope you can help me solve it

.
Jan.31,2022

do you have this database and corresponding access settings in your database service?


connect ECONNREFUSED 127.0.0.1 connect ECONNREFUSED 3306 is not connected ~ verify whether it is connected before you do the operation.


telnet check 3306 to see if the mysql is enabled

Menu