You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下Node API无法连接MySQL数据库求助

Docker下Node API连接MySQL超时问题排查求助

Docker新手,尝试将MySQL数据库与Node API连接时遇到连接超时错误,请求帮助排查。

docker-compose.yml配置

version: "3.9"

services:
  
  mysql_db:
    container_name: db_container
    image: mysql:latest
    ports:
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_DATABASE: "apirecipe"
      MYSQL_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      DB_HOSTNAME: "172.20.0.2"
    volumes:
      - mysql_db:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports: 
      - "8080:80"
    depends_on:
      - mysql_db

  api:
    build: ./api-recipe
    ports:
      - 80:80
    environment:
      - DB_SERVER=172.20.0.2
      - DB_DATABASE=apirecipe
      - DB_USERNAME=root
      - DB_PASSWORD=''
      - DB_PORT=
    depends_on:
      - mysql_db
volumes:
  mysql_db: {}

API容器报错信息

PORT : undefined
2023-06-25 12:57:46 ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
2023-06-25 12:57:46     at ConnectionManager.connect (/usr/src/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17)
2023-06-25 12:57:46     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2023-06-25 12:57:46     at async ConnectionManager._connect (/usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
2023-06-25 12:57:46     at async /usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32
2023-06-25 12:57:46     at async ConnectionManager.getConnection (/usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:197:7)
2023-06-25 12:57:46     at async /usr/src/node_modules/sequelize/lib/sequelize.js:305:26
2023-06-25 12:57:46     at async Sequelize.authenticate (/usr/src/node_modules/sequelize/lib/sequelize.js:457:5) {
2023-06-25 12:57:46   parent: Error: connect ETIMEDOUT
2023-06-25 12:57:46       at Connection._handleTimeoutError (/usr/src/node_modules/mysql2/lib/connection.js:202:17)
2023-06-25 12:57:46       at listOnTimeout (node:internal/timers:569:17)
2023-06-25 12:57:46       at process.processTimers (node:internal/timers:512:7) {
2023-06-25 12:57:46     errorno: 'ETIMEDOUT',
2023-06-25 12:57:46     code: 'ETIMEDOUT',
2023-06-25 12:57:46     syscall: 'connect',
2023-06-25 12:57:46     fatal: true
2023-06-25 12:57:46   },
2023-06-25 12:57:46   original: Error: connect ETIMEDOUT
2023-06-25 12:57:46       at Connection._handleTimeoutError (/usr/src/node_modules/mysql2/lib/connection.js:202:17)
2023-06-25 12:57:46       at listOnTimeout (node:internal/timers:569:17)
2023-06-25 12:57:46       at process.processTimers (node:internal/timers:512:7) {
2023-06-25 12:57:46     errorno: 'ETIMEDOUT',
2023-06-25 12:57:46     code: 'ETIMEDOUT',
2023-06-25 12:57:46     syscall: 'connect',
2023-06-25 12:57:46     fatal: true
2023-06-25 12:57:46   }
2023-06-25 12:57:46 }

phpMyAdmin容器日志

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.4. Set the 'ServerName' directive globally to suppress this message
2023-06-25 12:47:06 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.4. Set the 'ServerName' directive globally to suppress this message
2023-06-25 12:47:06 [Sun Jun 25 10:47:06.250596 2023] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.56 (Debian) PHP/8.1.17 configured -- resuming normal operations
2023-06-25 12:47:06 [Sun Jun 25 10:47:06.250677 2023] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

我不需要Apache,也见过不带Apache的示例,目前无法让API与数据库建立连接,恳请提供帮助。


排查建议

  • 替换固定IP为服务名:Docker Compose默认创建的网络中,容器可通过服务名直接通信,将API服务的DB_SERVER环境变量值改为mysql_db,避免固定IP变动导致连接失败。
  • 补全数据库端口:API的DB_PORT为空,MySQL默认端口是3306,修改为DB_PORT=3306。
  • 解决端口undefined问题:API日志显示PORT : undefined,需在Node项目中确保正确读取端口环境变量,或在docker-compose的api服务环境变量中添加- PORT=80。
  • 等待数据库就绪:depends_on仅保证启动顺序,不确保数据库完全就绪。可在API启动脚本中加入等待逻辑(如使用wait-for-it工具),或在代码中添加连接重试机制。
  • 移除不必要的phpMyAdmin:如果不需要数据库管理界面,直接删除docker-compose中的phpMyAdmin服务即可,其Apache警告不影响核心功能。

内容的提问来源于stack exchange,提问作者user21966032

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:07:16