简体   繁体   English

如何使我的 mysql docker 容器可以在 3306 以外的另一个端口上访问?

[英]how to make my mysql docker container accessible on another port apart 3306?

I have a docker compose which contains the following instructions:我有一个 docker 组合,其中包含以下说明:

version: '3.8'
services:
  mysql-standalone:
    container_name: marqueblanchebd
    image: marqueblanchebd:latest
    ports: 
      - "3307:3306"
    volumes:
      - data:/var/lib/mysql
        
  spring-boot:
    image: testmb
    container_name: mbwebservice
    ports:
    - "8091:8080"
    build:
      context: .                          
      dockerfile: Dockerfile 
    depends_on:
      - mysql-standalone 
    environment:
      SPRING_DATASOURCE_PASSWORD: ramses2021
      SPRING_DATASOURCE_URL: jdbc:mysql://marqueblanchebd:3307/marque_blanche?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      SPRING_DATASOURCE_USERNAME: root
    volumes:
      - ./:/uploads/deployment
volumes:
  data:

when i run docker-compose up the following error occurs from my springboot project: Caused by: java.net.ConnectException: Connection refused (Connection refused)当我运行docker-compose时,我的 springboot 项目发生以下错误: 原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

But when i put the default mysql port 3306 ("3306:3306") in docker-compose file, my springboot project connects itself successfully to my database container.但是当我将默认的 mysql 端口 3306 ("3306:3306") 放入 docker-compose 文件时,我的 springboot 项目成功连接到我的数据库容器。 Here is my mysql dockerfile:这是我的 mysql dockerfile:

FROM mysql:latest

EXPOSE 3307
ENV MYSQL_DATABASE=marque_blanche
ENV MYSQL_ROOT_PASSWORD=ramses2021
ADD marque_blanche.sql /docker-entrypoint-initdb.d

Inside the docker network the service name marqueblanchebd will resolve to the containers IP.docker network中,服务名称marqueblanchebd将解析为容器 IP。 Given the SPRING_DATASOURCE_URL=jdbc:mysql://marqueblanchebd:3307/... the connection attempt is done to the <container_ip>:3307 however the database is listening on port 3306 hence the connection is refused.鉴于SPRING_DATASOURCE_URL=jdbc:mysql://marqueblanchebd:3307/...尝试连接到<container_ip>:3307但是数据库正在侦听端口3306 ,因此连接被拒绝。 For the connection to work you'd need to configure the mysql service to listen on port 3307 instead of port 3306 .要使连接正常工作,您需要将mysql服务配置为侦听端口3307而不是端口3306 The traffic does not transit out of the docker network to the host then from the host back into the docker network .流量不会从docker network到主机,然后从主机返回到docker network


I'd suggest not exposing the database to the host, you can remove the ports mappings from the mysql-standalone service eg:我建议不要将数据库暴露给主机,您可以从mysql-standalone服务中删除ports映射,例如:

services:
  mysql-standalone:
    container_name: 'marqueblanchebd'
    image: 'marqueblanchebd:latest'
    expose: 
      - '3306'
    volumes:
      - 'data:/var/lib/mysql'

... and configure the spring-boot service to use the default mysql-standalone service port: ...并将spring-boot服务配置为使用默认的mysql-standalone服务端口:

  spring-boot:
    # ...
    environment:
      # ...
      SPRING_DATASOURCE_URL: jdbc:mysql://marqueblanchebd:3306/marque_blanche?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      # ...

This would isolate the database inside of the docker network .这将隔离docker network内部的数据库。

I will suggest a simple way to solve the problem:我会建议一个简单的方法来解决这个问题:

Just insert this line in docker compose file for mysql service:只需在 mysql 服务的 docker 组合文件中插入这一行:

environment:
  MYSQL_TCP_PORT: 3307 

As it is said in this link正如此链接中所说

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将使用 docker 创建的 MySQL 连接到另一个端口(不是端口 3306)? - How to connect to MySQL created with docker to another port (not port 3306)? 是否可以在docker mysql运行容器以及Windows 10 mysql中使用3306端口(相同的端口) - is it possible to use 3306 port (same port) in docker mysql running container as well as in windows 10 mysql 如何使我的 docker 容器可以访问我的数据库? - how to make my database accessible to my docker container? Docker 撰写 mysql 可在端口 3306 上访问 - Docker compose mysql reachable on port 3306 关闭mysql端口:3306并将其设为本地 - Close mysql port : 3306 and make it local 如何修复:MySQL 无法启动:错误“另一个 mysqld 服务器在端口 3306 上运行?” - 检查端口,它是免费的 - How to fix: MySQL can not start: Error "another mysqld server running on port: 3306?" - checked port and it is FREE 如何解决wamp上的mysql端口3306错误? - How to resolve mysql port 3306 error on wamp? 如何更改Docker Mysql容器连接端口? - How to change Docker Mysql container connection port? MySQL 端口 3306 无法远程访问。 Windows 服务器防火墙上启用的端口 - MySQL port 3306 not accessible remotely. port enabled on Windows server firewall 使用IPTABLES限制MySQL 3306端口 - Restricting MySQL 3306 port with IPTABLES
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM