Follow up of: ECONNREFUSED 127.0.0.1:3306 error connecting to MySQL database to NodeJS express server (w/ typescript) where I have made SOME progress by narrowing the issue down to "running the app through terminal as opposed to the coderunner extension (which works but only for single files rather than scattered files which make up the dir and run together)."
This is the issue I get when running via terminal i.e. node/nodemon
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (/mnt/c/Users/abdul/Desktop/nodemysql/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/mnt/c/Users/abdul/Desktop/nodemysql/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/mnt/c/Users/abdul/Desktop/nodemysql/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/mnt/c/Users/abdul/Desktop/nodemysql/app.js:13:4)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
Below is the new code I used to connect to a MySQL db (keep in mind it works with the coderunner extension for some reason, just not node / nodemon)
const express = require("express");
const mysql = require("mysql");
// Create connection
const db = mysql.createConnection({
host: "localhost",
user: "root",
port: 3306,
password: "password",
});
db.connect((err) => {
if (err) {
throw err;
}
console.log(" ... CONNECTED ... ");
});
const app = express();
app.get("/createdb", (req, res) => {
let sql = "CREATE DATABASE nodemysql";
db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
res.send({
message: "this is the result: " + result,
});
db.end();
});
});
app.listen("3000", () => {
console.log("Server started on port 3000");
});
Any ideas?