簡體   English   中英

微服務無法連接到 Docker 容器中運行的 MySQL

[英]Microservice can not connect to MySQL running in Docker container

I am trying to run my Java microservice locally on MacOs and connect to MySQL db running in Docker container but I get an error: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

我的 docker-compose.yml 看起來像這樣:

version: "3.1"
services:
  mysql:
    image: mysql
    container_name: mysql_v.0.1
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    env_file:
      - .mysql-dev-env
    ports:
      - 33060:33060

但是我能夠連接到 mysql 並通過 docker 本身創建數據庫:

docker exec -it mysql_v.0.1
mysql -u root -p

微服務有這樣的 application.dev.yml 配置:

db:
  pool.size.maximum: 30
  connection.idle.minimum: 10
  widget:
    url: jdbc:mysql://localhost:33060/dev_widget_platform?characterEncoding=UTF-8&useUnicode=yes&autoReconnect=true
    user: root
    password: pass

我正在嘗試通過 MySQL Workbench 連接到數據庫,它也沒有成功。 似乎我需要應用一些網絡技巧,但我是 docker 和 MySQl 的新手。

除非您在本地使用 SSL,否則將其添加到連接字符串的末尾

&useSSL=false

所以它變成

url: jdbc:mysql://localhost:33060/dev_widget_platform?characterEncoding=UTF-8&useUnicode=yes&autoReconnect=true&useSSL=false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM