MySQL数据库无法连接到使用Docker的Node应用程序。

huangapple go评论62阅读模式
英文:

MySQL DB can't connect to Node app with Docker

问题

I'm sorry for this question but I'm very newbie with Docker and I just want to connect a MySQL DB to my Node API, and I'm actually struggling with connection errors.

First my "docker-compose.yml" (sorry I can't just paste it):

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: {}

Error from API container:

PORT : undefined
2023-06-25 12:57:46 ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
...
I don't think I really need Apache? Already see some example without.

So now I struggle to connect my API with the database.

If someone can help me a bit, thanks a lot.

Log from phpmyadmin container:

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.250677 2023] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

Note: It seems you mentioned not needing Apache in your setup, so you can consider removing it from your configuration if it's not required for your project.

英文:

I'm sorry for this question but I'm very newbie with Docker and I just want to connect a MySQL DB to my Node API, and I'm actually struggling with connection errors.

First my "docker-compose.yml" (sorry I can't just paste it) :

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: {}

Error from API container

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 }

Log from phpmyadmin container

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'

I don't think I really need Apach ? Already see some exemple without.

So now I struggle to connect my API with the database.

If someone can help me a bit, thanks a lot.

答案1

得分: 0

你正在尝试从一个 Docker 容器连接到另一个 Docker 容器。
使用 localhost 来解析数据库的主机/域名将不起作用。

请使用数据库容器的名称作为连接的主机。
所以如果你有一个数据库配置,它应该看起来像这样:

{
  "development": {
    "username": "some_username",
    "password": "some_password",
    "database": "some_database",
    "host": "mysql_db", <<----- 这应该是数据库组合服务或容器的名称
    "port": 3306,
    "dialect": "mysql"
  }
}
英文:

You're trying to connect from a docker container to another docker container.
Using localhost to resolve the host/domain of the database will not work.

Use the db container name as the host to connect.
So if you have a database configuration, it should look like this:

{
  &quot;development&quot;: {
    &quot;username&quot;: &quot;some_username&quot;,
    &quot;password&quot;: &quot;some_password&quot;,
    &quot;database&quot;: &quot;some_database&quot;,
    &quot;host&quot;: &quot;mysql_db&quot;, &lt;&lt;----- this should be the name of the database compose service or container
    &quot;port&quot;: 3306,
    &quot;dialect&quot;: &quot;mysql&quot;
  }
}

huangapple
  • 本文由 发表于 2023年6月25日 19:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定