You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hasura Cloud连接本地MySQL报错咨询:本地库及密码疑问

问题描述

我使用Hasura Cloud环境,通过MySQL Workbench创建了本地MySQL数据库,服务运行在3306端口。尝试用JDBC连接串jdbc:mysql://localhost:3306/employees?user=admin&password=password@123接入Hasura Cloud时,出现通信链路失败错误,错误信息如下:

[
    {
        "definition": "employees",
        "message": {
            "stacktrace": "com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.\n\tat com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)\n\tat com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)\n\tat com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)\n\tat com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)\n\tat com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)\n\tat com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\n\tat io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:226)\n\tat io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:536)\n\tat io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:517)\n\tat java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)\n\tat io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:75)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\n\tat java.base/java.lang.Thread.run(Thread.java:833)\nCaused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.\n\tat jdk.internal.reflect.GeneratedConstructorAccessor53.newInstance(Unknown Source)\n\tat java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\n\tat java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)\n\tat java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)\n\tat com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)\n\tat com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)\n\tat com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)\n\tat com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)\n\tat com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)\n\tat com.mysql.cj.NativeSession.connect(NativeSession.java:120)\n\tat com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)\n\tat com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)\n\t... 11 more\nCaused by: java.net.ConnectException: Connection refused\n\tat java.base/sun.nio.ch.Net.connect0(Native Method)\n\tat java.base/sun.nio.ch.Net.connect(Net.java:579)\n\tat java.base/sun.nio.ch.Net.connect(Net.java:568)\n\tat java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:593)\n\tat java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)\n\tat java.base/java.net.Socket.connect(Socket.java:633)\n\tat com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)\n\tat com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)\n\t... 14 more\n"
        },
        "name": "source employees",
        "reason": "Inconsistent object: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.",
        "type": "source"
    }
]

疑问

  • 云环境是否允许使用本地SQL数据库,还是必须使用云端MySQL?
  • 密码中包含@符号是否会引发问题?

解决方案与疑问解答

通信链路失败的核心原因及修复

问题本质

Hasura Cloud运行在云端,你用localhost:3306作为连接地址时,Hasura会尝试访问它自身服务器的3306端口,而非你的本地数据库,这直接导致了Connection refused错误。

修复步骤

  1. 配置本地MySQL允许外部访问

    • 修改MySQL配置文件(my.cnf或my.ini),将bind-address设置为0.0.0.0,保存后重启MySQL服务。
    • 在本地服务器的防火墙中,允许3306端口的入站连接。
  2. 将本地数据库暴露到公网

    • 在路由器后台设置端口转发,把外部端口(建议用非3306的自定义端口提高安全性)映射到本地MySQL服务器的3306端口。
    • 获取你的公网IP(可以通过终端执行curl ifconfig.me,或浏览器搜索「我的公网IP」)。
  3. 更新JDBC连接串
    把连接串中的localhost替换成你的公网IP,同时修正密码的编码问题(后面会讲),示例:

    jdbc:mysql://你的公网IP:3306/employees?user=admin&password=password%40123
    

疑问解答

  1. Hasura Cloud支持本地数据库吗?
    支持,但前提是本地数据库能被公网访问。如果不想直接暴露公网,也可以用ngrok这类隧道工具建立反向代理,让Hasura通过隧道连接你的本地数据库。

  2. 密码含@符号会有问题吗?
    会。JDBC连接串里@是用来分隔用户信息和主机地址的特殊字符,不编码的话会导致连接串解析错误。解决方法是对@进行URL编码,替换成%40,比如密码password@123要写成password%40123。


内容的提问来源于stack exchange,提问作者dinesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 14:15:54