问题:如题目所示
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 ,发现确实是在一个网络环境中。
1 q0000001 2024-05-15 19:20:01 +08:00 via Android ![]() 你走容器内网络就不需要 3366 的端口,走 3306 就行。3366 是映射到宿主机的 |
![]() | 2 RedBeanIce OP @q0000001 感谢!!我去试试! |
3 q0000001 2024-05-15 19:22:18 +08:00 via Android @RedBeanIce 但是你说 redis 能连,我持怀疑态度。等你反馈 |
![]() | 4 RedBeanIce OP |
![]() | 5 keakon 2024-05-15 19:54:09 +08:00 ![]() 提醒一下,如果你不需要在外部访问 mysql ,只需要 jeecg_boot_system 能访问到 jeecg_boot_mysql ,那么不需要配置 ports ,同一个 network 下可以互相访问。暴露数据库是有风险的。 |
![]() | 6 RedBeanIce OP @keakon 感谢指点。 |