【发布时间】:2021-01-14 00:09:51
【问题描述】:
我正在尝试运行 springboot 微服务 docker 映像。它从配置服务器获取数据库连接属性。但是它无法连接到容器。
错误:
JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
例外:
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
Caused by :
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_242]
2020-09-28 11:39:32.782 [ : ] WARN [task-1] o.h.e.j.s.SqlExceptionHelper - SQL Error: 0, SQLState: 08S01
1 --- [ task-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08S01
2020-09-28 11:39:32.791 [ : ] ERROR [task-1] o.h.e.j.s.SqlExceptionHelper - Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
1 --- [ task-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
位于配置服务器的属性
shopping-service.datasource.url: jdbc:mysql://A.B.C.D:3306/shoppingCartDB
shopping-service.datasource.username: root
shopping-service.datasource.password: root
A.B.C.D 是我的 docker 主机 IP。
application.yaml
datasource:
url: ${shopping-service.datasource.url}
username: ${shopping-service.datasource.username}
password: ${shopping-service.datasource.password}
#driver-class-name: com.mysql.jdbc.Driver
jpa:
generate-ddl: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
show_sql: true
ddl-auto: create-drop
profiles:
active: dev
我已经从 docker hub 拉取了 MySQL 8.0 docker 镜像。
docker pull mysql/mysql-server:8.0
docker run --name=mysql-container -d mysql/mysql-server:8.0
将密码更改为“root”。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.02 sec)
创建数据库:
mysql> create DATABASE shoppingCartDB;
Query OK, 1 row affected (0.00 sec)
mysql> exit
Docker 运行
docker run -p 5000:5000 shoppingms:latest --env shopping-service.configserverurl=http://A.B.C.D:8888 --env shopping-service.eureka.url=http://A.B.C.D:4444/eureka
它能够从配置服务器获取属性。我检查了日志。错误来自这一行:
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
在我的本地环境中,我可以使用具有相同连接参数的本地安装的 MySQL Db。但是在docker中我得到了例外。我哪里出错了,谁能帮帮我。
更新:
将 MySQL 容器暴露到 3306 端口后,错误出现在现在:
2020-09-28 12:07:43.942 [ : ] WARN [task-1] o.h.e.j.e.i.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : null, message from server: "Host 'Some-IP' is not allowed to connect to this MySQL server"
1 --- [ task-1] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : null, message from server: "Host 'Some-IP' is not allowed to connect to this MySQL server"
2020-09-28 12:07:49.219 [ : ] WARN [task-1] o.h.e.j.s.SqlExceptionHelper - SQL Error: 1130, SQLState: HY000
1 --- [ task-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1130, SQLState: HY000
2020-09-28 12:07:49.225 [ : ] ERROR [task-1] o.h.e.j.s.SqlExceptionHelper - null, message from server: "Host 'Some-IP' is not allowed to connect to this MySQL server"
1 --- [ task-1] o.h.engine.jdbc.spi.SqlExceptionHelper : null, message from server: "Host 'Some-IP' is not allowed to connect to this MySQL server"
【问题讨论】:
-
你应该暴露端口。
docker run -p 3306:3306 --name=mysql-container -d mysql/mysql-server:8.0 -
@omer : 好吧,让我试一次。
-
您是否在 MySql 容器旁边的容器中运行您的应用程序并希望它们在内部进行通信?
-
@user626201 : 是的,这不是可能的,还是我必须先创建一个 docker 网络才能进行通信。
-
@Som 这取决于您的 docker 网络拓扑,但是,是的,您可以让它们通过 docker DNS 在内部进行通信,是的,如果您有多个应用程序,通常需要配置 docker 网络,否则一般的docker网络应该没问题
标签: java mysql spring-boot docker microservices