Reputation: 15
New to js and node and I've got an app.js that will query a database and I query it using a pool so I have a file called db.js that looks like this
const mysql = require('mysql2/promise');
const config = {db: {host: 'localhost',user: 'root',password: '',database: 'school',port:'3308',waitForConnections: true,connectionLimit: 2,queueLimit: 0,},};
const pool = mysql.createPool(config.db);
async function query(sql, params) {
const [rows, fields] = await pool.execute(sql, params);
return rows;
}
module.exports = {query,}
However after running docker-compose --build in the terminal I run into this error
/src/node_modules/mysql2/promise.js:359
test-app-1 | const localErr = new Error();
test-app-1 | ^
test-app-1 |
test-app-1 | Error: connect ECONNREFUSED 127.0.0.1:3308
test-app-1 | at PromisePool.execute (/src/node_modules/mysql2/promise.js:359:22)
test-app-1 | at Object.query (/src/app/services/db.js:20:39)
test-app-1 | at /src/app/app.js:17:8
test-app-1 | at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
test-app-1 | at next (/src/node_modules/express/lib/router/route.js:137:13)
test-app-1 | at Route.dispatch (/src/node_modules/express/lib/router/route.js:112:3)
test-app-1 | at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
test-app-1 | at /src/node_modules/express/lib/router/index.js:281:22
test-app-1 | at Function.process_params (/src/node_modules/express/lib/router/index.js:341:12)
test-app-1 | at next (/src/node_modules/express/lib/router/index.js:275:10) {
test-app-1 | code: 'ECONNREFUSED',
test-app-1 | errno: -111,
test-app-1 | sql: undefined,
test-app-1 | sqlState: undefined,
test-app-1 | sqlMessage: undefined
test-app-1 | }
The requests that don't require the database open fine e.g http://127.0.0.1:3000/ but the ones that do query the database fall to this error. I can see the error stems from a file in a module but I don't know how to go about fixing this. My code for requests in my app.js file look like this
app.get("/", function(req, res) {
res.send("Main Page");});
app.get('/class', function(req,res) {
var sql = 'SELECT * FROM class ';
db.query(sql).then(results => {
console.log(results);
res.json(results);});})
And my index.js file looks like this
"use strict";
console.log("entrypoint");
const app = require("./app/app.js");
I notice the error stems from a module file but I don't want to tamper with it due to my inexperience.
Upvotes: 1
Views: 2113
Reputation: 34
As you are using docker containers for the app and mysql server, and in the error you shared is written:
| Error: connect ECONNREFUSED 127.0.0.1:3308
To me seems, your docker's trying to connect to port 3308 in your app container instead of connecting to mysql container.
It seems a docker-composer configuration problem.
Upvotes: 1