最新Liquibase Docker镜像连接MySQL报通信链路失败错误
Liquibase Docker镜像连接MySQL报Communications link failure排查方案
问题场景
正在开展POC验证工作,尝试通过Liquibase官方Docker镜像连接MySQL实例发布数据库变更,操作参考了Liquibase官方Docker使用文档、官方Docker镜像说明、公开示例项目,测试覆盖MySQL 5.7、MySQL 8.x两个主流版本,均复现相同连接错误。
报错详情
Connection could not be created to jdbc:mysql://localhost:3306/denmark with driver com.mysql.cj.jdbc.Driver. 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. liquibase.exception.CommandExecutionException: liquibase.exception.LiquibaseException: Unexpected error running Liquibase: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:mysql://localhost:3306/denmark with driver com.mysql.cj.jdbc.Driver. 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 liquibase.command.CommandScope.execute(CommandScope.java:163) at liquibase.integration.commandline.CommandRunner.call(CommandRunner.java:51) at liquibase.integration.commandline.CommandRunner.call(CommandRunner.java:21) at picocli.CommandLine.executeUserObject(CommandLine.java:1953) at picocli.CommandLine.access$1300(CommandLine.java:145) at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2358) at picocli.CommandLine$RunLast.handle(CommandLine.java:2352) at picocli.CommandLine$RunLast.handle(CommandLine.java:2314) at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179) at picocli.CommandLine$RunLast.execute(CommandLine.java:2316) at picocli.CommandLine.execute(CommandLine.java:2078) at liquibase.integration.commandline.LiquibaseCommandLine.lambda$execute$1(LiquibaseCommandLine.java:336) at liquibase.Scope.child(Scope.java:189) at liquibase.Scope.child(Scope.java:165) at liquibase.integration.commandline.LiquibaseCommandLine.execute(LiquibaseCommandLine.java:301) at liquibase.integration.commandline.LiquibaseCommandLine.main(LiquibaseCommandLine.java:90) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at liquibase.integration.commandline.LiquibaseLauncher.main(LiquibaseLauncher.java:91)
排查与解决步骤
按优先级从高到低逐一验证:
- 修正JDBC连接地址(90%以上同类问题根因)
当前JDBC地址里写的localhost是错误指向:Docker容器内的localhost是Liquibase容器自身的回环地址,不是宿主机/MySQL所在服务的地址,根据MySQL部署场景替换主机地址:- 如果MySQL部署在Docker宿主机上:
- Linux环境:将
localhost替换为172.17.0.1(Docker默认网桥的宿主机网关IP),或启动Liquibase容器时添加--network=host参数直接共享宿主机网络栈 - Mac/Windows(Docker Desktop环境):将
localhost替换为host.docker.internal,该域名是Docker Desktop内置的宿主机解析地址
- Linux环境:将
- 如果MySQL同样运行在Docker容器中:提前创建自定义Docker网络,将Liquibase容器和MySQL容器都加入该网络,JDBC地址中直接写MySQL容器的服务名/容器名作为主机地址,不要用IP或localhost
- 如果MySQL部署在Docker宿主机上:
- 验证MySQL访问权限与监听配置
- 检查MySQL配置文件中的
bind-address参数,不要绑定127.0.0.1,需配置为0.0.0.0或对应服务网卡IP,否则外部请求无法到达MySQL - 检查连接使用的数据库账号权限,确保账号允许从Liquibase所在IP段登录,不要仅限定localhost本地登录,可执行如下SQL授权:
GRANT ALL PRIVILEGES ON denmark.* TO '你的连接用户名'@'%' IDENTIFIED BY '你的连接密码'; FLUSH PRIVILEGES; - 连通性校验:先在宿主机通过
mysql -h <你配置的连接IP> -P 3306 -u 用户名 -p验证端口和账号可用,再进入Liquibase容器内部通过telnet、nc等工具验证到MySQL 3306端口的网络连通性
- 检查MySQL配置文件中的
- 补全JDBC连接必要参数
部分MySQL 8.x版本会因为SSL、公钥检索、时区配置问题导致连接失败,可将JDBC URL调整为如下格式重试:jdbc:mysql://<正确的主机地址>:3306/denmark?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - 检查网络规则
确认宿主机、云服务器安全组、本地防火墙规则已放通3306端口的入站请求,且3306端口确实由MySQL服务监听,未被其他进程占用。
内容的提问来源于stack exchange,提问作者Vibhu Banga
相关产品推荐
相关产品推荐

