mysql - 从运行 nodejs 应用程序的 docker 容器连接到在主机上运行的 mysql 数据库
问题描述
我编写了一个小的 nodejs 应用程序,它使用以下代码行连接到 mysql 数据库。
const DATABASE = MYSQL.createPool({
host : 'localhost',
user : dbconfig.DATABASE_USER,
password: dbconfig.DATABASE_CREDENTIAL,
database: dbconfig.DATABASE_USING,
multipleStatements: true
});
当使用“yarn node server.js”运行 nodejs 应用程序时,一切正常,nodejs 应用程序通过端口3306(标准端口)连接到 mysql 数据库。
现在我想做完全相同的事情,唯一的区别是:Nodejs-App 运行在一个暴露了端口3300的 docker 容器中。每次我进行触发数据库查询的 API 调用时,都会显示以下错误:
(node:36) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.createConnection (/app/.yarn/cache/mysql2-npm-2.2.5-fb26898147-2a3c2f0748.zip/node_modules/mysql2/promise.js:241:31)
at Function.Filling.getAllFillingLiter (/app/server/models/filling.model.js:59:28)
at Object.exports.getAllFillingLiter (/app/server/controller/filling.controller.js:36:13)
at /app/server.js:145:13
at Layer.handle [as handle_request] (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/layer.js:95:5)
at next (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/layer.js:95:5)
at /app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/index.js:335:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:36) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:36) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
老实说,我无法说出我做错了什么。docker 容器应该能够连接到主机上运行的 mysql-database 吗?但我的错误是什么?
提前致谢
解决方案
推荐阅读
- python - 在 Macintosh (10.13.6) 上使用具有多种选择的 Tkinter OptionMenu() 小部件时出现奇怪的视觉行为
- video - 使用 ffmpeg 从流中提取隐藏式字幕 VTT
- java - 尝试转换时间戳字段时,oracle.sql.timestamp 无法转换为 java.util.date
- django - Django 3.0:如何在同一模板中使用不同的 Boostrap 版本
- typescript - 您如何使用扩展运算符覆盖打字稿中的属性
- angular - Angular 10 更新和 xml2js
- android - XML功能中的android字体不适用于AppWidget
- android - 受信任的 Web 活动:导航回主活动会在过渡期间显示网站 URL
- regex - 特定正则表达式模式的 Nginx 重写规则
- python - Python 字典和列表混淆