对于js和节点,我有一个app.js,它将查询一个数据库,我使用一个池来查询它,所以我有一个名为db.js的文件,如下所示
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,}但是,在终端中运行了docker-组合-构建之后,我遇到了这个错误。
/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 | }
不需要数据库打开的请求可以很好地打开,例如http://127.0.0.1:3000/,但是查询数据库的请求就会产生这个错误。我可以看到错误源于模块中的一个文件,但我不知道如何解决这个问题。我在app.js文件中请求的代码如下所示
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);});})我的index.js文件看起来像这样
"use strict";
console.log("entrypoint");
const app = require("./app/app.js");我注意到错误源于一个模块文件,但由于我缺乏经验,我不想篡改它。
发布于 2022-07-31 21:00:31
当您在应用程序和mysql服务器上使用docker容器时,在您共享的错误中编写:
| Error: connect ECONNREFUSED 127.0.0.1:3308 在我看来,您的对接者试图连接到应用程序容器中的端口3308,而不是连接到mysql容器。
这似乎是一个码头-作曲家配置问题。
https://stackoverflow.com/questions/71560419
复制相似问题