[docker-compose]springboot 连接不到 mysql - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RedBeanIce
V2EX    Docker

[docker-compose]springboot 连接不到 mysql

  •  
  •   RedBeanIce 2024-05-15 19:15:39 +08:00 1161 次点击
    这是一个创建于 514 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题:如题目所示

    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  
       2024-05-15 19:20:01 +08:00 via Android   1
    你走容器内网络就不需要 3366 的端口,走 3306 就行。3366 是映射到宿主机的
    RedBeanIce
        2
    RedBeanIce  
    OP
       2024-05-15 19:21:16 +08:00
    @q0000001 感谢!!我去试试!
    q0000001
        3
    q0000001  
       2024-05-15 19:22:18 +08:00 via Android
    @RedBeanIce 但是你说 redis 能连,我持怀疑态度。等你反馈
    RedBeanIce
        4
    RedBeanIce  
    OP
       2024-05-15 19:31:47 +08:00
    @q0000001 果然是大佬所言,

    我将 3366 改为 3306 ,整个部署就正常了。
    keakon
        5
    keakon  
       2024-05-15 19:54:09 +08:00   2
    提醒一下,如果你不需要在外部访问 mysql ,只需要 jeecg_boot_system 能访问到 jeecg_boot_mysql ,那么不需要配置 ports ,同一个 network 下可以互相访问。暴露数据库是有风险的。
    RedBeanIce
        6
    RedBeanIce  
    OP
       2024-05-15 21:00:56 +08:00
    @keakon 感谢指点。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2208 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 00:58 PVG 08:58 LAX 17:58 JFK 20:58
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86