V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RedBeanIce
V2EX  ›  Docker

[docker-compose]springboot 连接不到 mysql

  •  
  •   RedBeanIce · 177 天前 · 735 次点击
    这是一个创建于 177 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题:如题目所示

    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 ,发现确实是在一个网络环境中。

    6 条回复    2024-05-15 21:00:56 +08:00
    q0000001
        1
    q0000001  
       177 天前 via Android   ❤️ 1
    你走容器内网络就不需要 3366 的端口,走 3306 就行。3366 是映射到宿主机的
    RedBeanIce
        2
    RedBeanIce  
    OP
       177 天前
    @q0000001 感谢!!我去试试!
    q0000001
        3
    q0000001  
       177 天前 via Android
    @RedBeanIce 但是你说 redis 能连,我持怀疑态度。等你反馈
    RedBeanIce
        4
    RedBeanIce  
    OP
       177 天前
    @q0000001 果然是大佬所言,

    我将 3366 改为 3306 ,整个部署就正常了。
    keakon
        5
    keakon  
       177 天前   ❤️ 2
    提醒一下,如果你不需要在外部访问 mysql ,只需要 jeecg_boot_system 能访问到 jeecg_boot_mysql ,那么不需要配置 ports ,同一个 network 下可以互相访问。暴露数据库是有风险的。
    RedBeanIce
        6
    RedBeanIce  
    OP
       177 天前
    @keakon 感谢指点。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2832 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 13:14 · PVG 21:14 · LAX 05:14 · JFK 08:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.