Sping Boot + docker-compose + MySQL无法连接

ql3eal8s  于 2023-02-15  发布在  Mysql
关注(0)|答案(1)|浏览(134)

我正在尝试使用spring boot + docker-compose + mysql的示例应用程序。下面是我的docker-compose,yml file

mysql:
  image: mysql:latest
  container_name: mysql-db
  restart: always
  command: --default-authentication-plugin=mysql_native_password
  ports:
    - "33061:3306"
  networks:
    - spring-boot-mysql-net
  environment:
    MYSQL_DATABASE: practice_db
    MYSQL_ROOT_PASSWORD: root
  volumes:
    - ./database_storage:/docker-entrypoint-initdb.d

practice-service:
  container_name: practice-service
  build:
    context: ./
    dockerfile: Dockerfile
  ports:
    - "8080:8080"
  depends_on:
    - mysql
  networks:
    - spring-boot-mysql-net
  restart: on-failure
  command: sh -c './wait-for mysql:3306 -- npm start'

phpMyAdmin:
  image: phpMyAdmin/phpMyAdmin
  container_name: phpMyAdmin
  restart: always
  depends_on:
    - mysql
  environment:
    PMA_HOST: database
    PMA_PORT: 3306
  ports:
    - "9091:80"

networks:
  spring-boot-mysql-net:
    driver: bridge

应用程序。属性

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://mysql:3306/practice_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.hibernate.naming.physical- 
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

但我的 Spring Boot 应用程序在连接到MySQLDB时出错

2023-02-11 16:31:10 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2023-02-11 16:31:10 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2023-02-11 16:31:10 Caused by: java.net.ConnectException: Connection refused

另外,我试图运行phpMyAdmin,但也没有连接到MySQL。我不确定MySQL容器是否启动,因为我可以看到它的日志-

2023-02-11 16:31:42 2023-02-12T00:31:42.117825Z 0 [System] [MY-010931] [Server] 
/usr/sbin/mysqld: ready for connections. Version: '8.0.32'  socket: ' 
/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2023-02-11 16:31:30 2023-02-12 00:31:30+00:00 [Note] [Entrypoint]: Temporary server started.
2023-02-11 16:31:30 '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-02-11 16:31:36 2023-02-12 00:31:36+00:00 [Note] [Entrypoint]: Creating database 
practice-db
2023-02-11 16:31:36 
2023-02-11 16:31:36 2023-02-12 00:31:36+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-02-11 16:31:40 2023-02-12 00:31:40+00:00 [Note] [Entrypoint]: Temporary server stopped
2023-02-11 16:31:40 
2023-02-11 16:31:40 2023-02-12 00:31:40+00:00 [Note] [Entrypoint]: MySQL init process done. 
Ready for start up.
oxiaedzo

oxiaedzo1#

我有一个在github中工作的例子:
https://github.com/armdev/docker-mysql
我的 Docker 作曲:

version: '3'
services: 
  mysqlnode:
    image: mysqlnode
    build: ./mysqlnode
    container_name: "mysqlnode"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=root
      - MYSQL_DATABASE=flownet
    volumes:
      - /opt/mysql/logs/:/opt/mysql/logs 
      - /opt/mysql/data:/var/lib/mysql    
    ports:
      - 3306:3306  
    networks:
     - flownet
     
       
  phpmyadmin:
   image: phpmyadmin/phpmyadmin
   container_name: "phpmyadmin"
   links:
    - mysqlnode
   ports:
     - 9191:80
   environment:
     MYSQL_USERNAME: root
     MYSQL_ROOT_PASSWORD: root
     PMA_HOST: mysqlnode
   networks:
     - flownet

  
networks:
  flownet:
    driver: bridge

MySQL用户/密码root:root

相关问题