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

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

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):

  1. version: "3.9"
  2. services:
  3. mysql_db:
  4. container_name: db_container
  5. image: mysql:latest
  6. ports:
  7. - "3306:3306"
  8. command: --default-authentication-plugin=mysql_native_password
  9. restart: always
  10. environment:
  11. MYSQL_DATABASE: "apirecipe"
  12. MYSQL_PASSWORD: ""
  13. MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  14. DB_HOSTNAME: "172.20.0.2"
  15. volumes:
  16. - mysql_db:/var/lib/mysql
  17. phpmyadmin:
  18. image: phpmyadmin/phpmyadmin
  19. ports:
  20. - "8080:80"
  21. depends_on:
  22. - mysql_db
  23. api:
  24. build: ./api-recipe
  25. ports:
  26. - 80:80
  27. environment:
  28. - DB_SERVER=172.20.0.2
  29. - DB_DATABASE=apirecipe
  30. - DB_USERNAME=root
  31. - DB_PASSWORD=''
  32. - DB_PORT=
  33. depends_on:
  34. - mysql_db
  35. volumes:
  36. mysql_db: {}

Error from API container:

  1. PORT : undefined
  2. 2023-06-25 12:57:46 ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
  3. ...
  4. I don't think I really need Apache? Already see some example without.
  5. So now I struggle to connect my API with the database.
  6. If someone can help me a bit, thanks a lot.

Log from phpmyadmin container:

  1. 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
  2. ...
  3. 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) :

  1. version: "3.9"
  2. services:
  3. mysql_db:
  4. container_name: db_container
  5. image: mysql:latest
  6. ports:
  7. - "3306:3306"
  8. command: --default-authentication-plugin=mysql_native_password
  9. restart: always
  10. environment:
  11. MYSQL_DATABASE: "apirecipe"
  12. MYSQL_PASSWORD: ""
  13. MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  14. DB_HOSTNAME: "172.20.0.2"
  15. volumes:
  16. - mysql_db:/var/lib/mysql
  17. phpmyadmin:
  18. image: phpmyadmin/phpmyadmin
  19. ports:
  20. - "8080:80"
  21. depends_on:
  22. - mysql_db
  23. api:
  24. build: ./api-recipe
  25. ports:
  26. - 80:80
  27. environment:
  28. - DB_SERVER=172.20.0.2
  29. - DB_DATABASE=apirecipe
  30. - DB_USERNAME=root
  31. - DB_PASSWORD=''
  32. - DB_PORT=
  33. depends_on:
  34. - mysql_db
  35. volumes:
  36. mysql_db: {}

Error from API container

  1. PORT : undefined
  2. 2023-06-25 12:57:46 ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
  3. 2023-06-25 12:57:46 at ConnectionManager.connect (/usr/src/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17)
  4. 2023-06-25 12:57:46 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  5. 2023-06-25 12:57:46 at async ConnectionManager._connect (/usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
  6. 2023-06-25 12:57:46 at async /usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32
  7. 2023-06-25 12:57:46 at async ConnectionManager.getConnection (/usr/src/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:197:7)
  8. 2023-06-25 12:57:46 at async /usr/src/node_modules/sequelize/lib/sequelize.js:305:26
  9. 2023-06-25 12:57:46 at async Sequelize.authenticate (/usr/src/node_modules/sequelize/lib/sequelize.js:457:5) {
  10. 2023-06-25 12:57:46 parent: Error: connect ETIMEDOUT
  11. 2023-06-25 12:57:46 at Connection._handleTimeoutError (/usr/src/node_modules/mysql2/lib/connection.js:202:17)
  12. 2023-06-25 12:57:46 at listOnTimeout (node:internal/timers:569:17)
  13. 2023-06-25 12:57:46 at process.processTimers (node:internal/timers:512:7) {
  14. 2023-06-25 12:57:46 errorno: 'ETIMEDOUT',
  15. 2023-06-25 12:57:46 code: 'ETIMEDOUT',
  16. 2023-06-25 12:57:46 syscall: 'connect',
  17. 2023-06-25 12:57:46 fatal: true
  18. 2023-06-25 12:57:46 },
  19. 2023-06-25 12:57:46 original: Error: connect ETIMEDOUT
  20. 2023-06-25 12:57:46 at Connection._handleTimeoutError (/usr/src/node_modules/mysql2/lib/connection.js:202:17)
  21. 2023-06-25 12:57:46 at listOnTimeout (node:internal/timers:569:17)
  22. 2023-06-25 12:57:46 at process.processTimers (node:internal/timers:512:7) {
  23. 2023-06-25 12:57:46 errorno: 'ETIMEDOUT',
  24. 2023-06-25 12:57:46 code: 'ETIMEDOUT',
  25. 2023-06-25 12:57:46 syscall: 'connect',
  26. 2023-06-25 12:57:46 fatal: true
  27. 2023-06-25 12:57:46 }
  28. 2023-06-25 12:57:46 }

Log from phpmyadmin container

  1. 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
  2. 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
  3. 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
  4. 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 来解析数据库的主机/域名将不起作用。

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

  1. {
  2. "development": {
  3. "username": "some_username",
  4. "password": "some_password",
  5. "database": "some_database",
  6. "host": "mysql_db", <<----- 这应该是数据库组合服务或容器的名称
  7. "port": 3306,
  8. "dialect": "mysql"
  9. }
  10. }
英文:

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:

  1. {
  2. &quot;development&quot;: {
  3. &quot;username&quot;: &quot;some_username&quot;,
  4. &quot;password&quot;: &quot;some_password&quot;,
  5. &quot;database&quot;: &quot;some_database&quot;,
  6. &quot;host&quot;: &quot;mysql_db&quot;, &lt;&lt;----- this should be the name of the database compose service or container
  7. &quot;port&quot;: 3306,
  8. &quot;dialect&quot;: &quot;mysql&quot;
  9. }
  10. }

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:

确定