[docker-compose]springboot 连接不到 mysql

223 天前
 RedBeanIce

问题:如题目所示

1 ,docker-compose_mysql.yml

version: '1'
services:
  jeecg_boot_mysql:
    build:
      context: ./db
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_ROOT_HOST: '%'
      TZ: Asia/Shanghai
    restart: always
    hostname: jeecg_boot_mysql
    container_name: jeecg_boot_mysql
    image: "mysql:5.7.44"
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --bind-address=0.0.0.0
    ports:
      - "3366:3306"
    networks:
      - szdz_network

networks:
  szdz_network:
    name: szdz_network


首先运行 mysql

2 ,然后往数据库导入数据

3 ,docker-compose.yml

version: '1'
services:
  jeecg_boot_mysql:
    build:
      context: ./db
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_ROOT_HOST: '%'
      TZ: Asia/Shanghai
    restart: always
    hostname: jeecg_boot_mysql
    container_name: jeecg_boot_mysql
    image: "mysql:5.7.44"
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --bind-address=0.0.0.0
    ports:
      - "3366:3306"
    networks:
      - szdz_network


  jeecg_boot_redis:
    image: "redis:7.2.4"
    ports:
      - "9379:6379"
    restart: always
    hostname: jeecg_boot_redis
    container_name: jeecg_boot_redis
    networks:
      - szdz_network

  jeecg_boot_system:
    build:
      context: ./jeecg-module-system/jeecg-system-start
    restart: on-failure
    depends_on:
      - jeecg_boot_redis
      - jeecg_boot_mysql
    container_name: jeecg_boot_system
    image: "registry.cn-hangzhou.aliyuncs.com/xuegao_zn_data/zn:20240515_1044"
    environment:
      "jeecg_boot_redis_host": "jeecg_boot_redis"
      "jeecg_boot_redis_port": "9379"
      "jeecg_boot_mysql_host": "jeecg_boot_mysql"
      "jeecg_boot_mysql_port": "3366"
      "jeecg_boot_mysql_database": "jeecg_boot"
      "jeecg_boot_mysql_username": "root"
      "jeecg_boot_mysql_password": "123456"
    hostname: jeecg_boot_system
    ports:
      - "8080:8080"
    networks:
      - szdz_network

networks:
  szdz_network:
    name: szdz_network

4 ,sprinboot

      datasource:
        master:
          url: jdbc:mysql://${jeecg_boot_mysql_host}:${jeecg_boot_mysql_port}/${jeecg_boot_mysql_database}?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
          username: ${jeecg_boot_mysql_username}
          password: ${jeecg_boot_mysql_password}
          driver-class-name: com.mysql.cj.jdbc.Driver

  redis:
    database: 0
    host: ${jeecg_boot_redis_host}
    port: ${jeecg_boot_redis_port}
    password: ''

遇到问题: redis 可以正常的链接,但是 mysql 死活连不上

报错:

2024-05-15 18:35:53 2024-05-15 10:35:53.371[jeecg-system] [8082] [main] [ERROR] [,] [com.alibaba.druid.pool.DruidDataSource:943] - init datasource error, url: jdbc:mysql://jeecg_boot_mysql:3366/jeecg_boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
2024-05-15 18:35:53 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2024-05-15 18:35:53 
2024-05-15 18:35:53 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-05-15 18:35:53 at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
2024-05-15 18:35:53 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)

我自己排查问题 1 ,我查询过 Google 和 openai ,但是没有找到解决办法 2 ,我查看过 docker network inspect szdz_network ,发现确实是在一个网络环境中。

779 次点击
所在节点    Docker
6 条回复
q0000001
223 天前
你走容器内网络就不需要 3366 的端口,走 3306 就行。3366 是映射到宿主机的
RedBeanIce
223 天前
@q0000001 感谢!!我去试试!
q0000001
223 天前
@RedBeanIce 但是你说 redis 能连,我持怀疑态度。等你反馈
RedBeanIce
223 天前
@q0000001 果然是大佬所言,

我将 3366 改为 3306 ,整个部署就正常了。
keakon
223 天前
提醒一下,如果你不需要在外部访问 mysql ,只需要 jeecg_boot_system 能访问到 jeecg_boot_mysql ,那么不需要配置 ports ,同一个 network 下可以互相访问。暴露数据库是有风险的。
RedBeanIce
223 天前
@keakon 感谢指点。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1041098

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX