【问题标题】:java.net.ConnectException: Operation timed out (Connection timed out) while connecting mysql docker from local machinejava.net.ConnectException:从本地机器连接mysql docker时操作超时(连接超时)
【发布时间】:2021-09-30 22:24:26
【问题描述】:

我正在尝试使用 scala 代码连接到在本地机器上运行的 mysql 容器。由于某种原因,我得到了操作超时(dockerIp)或连接被拒绝错误(localhost/127.0.0.1)。

docker run --name=mysql-docker -p3306:3306 -e MYSQL_ROOT_PASSWORD='root' -d mysql:latest
docker exec -it mysql-docker bash
mysql -u root -p
update mysql.user set host = ‘127.0.0.1’ where user=’root’;

我可以使用mysql -h 127.0.0.1 -P 3306 -u root -p从终端连接到 docker

以下是我的 scala 代码。

  val url = "jdbc:mysql://172.17.0.2:3006/mysql"
  val driver = "com.mysql.jdbc.Driver"
  val username = "root"
  val password = "root"
  Class.forName("com.mysql.jdbc.Driver")
  val connection = DriverManager.getConnection(url, username, password)
  val statement = connection.createStatement
  val rs = statement.executeQuery("SELECT host, user FROM user")
  connection.close

不幸的是,这段代码抛出了


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)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at largedataprocessor.LargeDataProcessor$.delayedEndpoint$largedataprocessor$LargeDataProcessor$1(LargeDataProcessor.scala:35)
        at largedataprocessor.LargeDataProcessor$delayedInit$body.apply(LargeDataProcessor.scala:9)
        at scala.Function0.apply$mcV$sp(Function0.scala:39)
        at scala.Function0.apply$mcV$sp$(Function0.scala:39)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.collection.immutable.List.foreach(List.scala:431)
        at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
        at scala.App$class.main(App.scala:76)
        at largedataprocessor.LargeDataProcessor$.main(LargeDataProcessor.scala:9)
        at largedataprocessor.LargeDataProcessor.main(LargeDataProcessor.scala)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:119)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
        ... 17 more
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:607)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:156)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
        ... 20 more

如果我使用“jdbc:mysql://127.0.0.1:3006/mysql”,则会收到连接被拒绝错误。

请指出我的错误。

【问题讨论】:

    标签: java mysql scala docker apache-spark


    【解决方案1】:

    在网络方面,一些 dockerized 进程运行在与非 dockerized 不同的主机上。因此,连接到容器内的localhost127.0.0.1 将连接回同一个容器,其中没有任何东西在端口 3306 上进行监听 - 你正确地得到了 connection refused

    如果您尝试在端口 3306 上连接到主机的 IP,您也可以

    • 如果你的 mysql 只监听 localhost:3306,则连接被拒绝
    • 如果您的主机的防火墙阻止访问,则获取超时

    确保在预期成功之前配置这两个项目。

    另一种选择是在主机网络上运行您的容器。那么就不需要暴露端口了(反正就是暴露了),连接到localhost应该可以达到预期的效果。

    您可能想更熟悉Docker networking

    编辑:到目前为止,我试图解释为什么你会看到你提到的结果。一切都按设计工作。所以现在让我们弄清楚你需要做什么来获得连接:

    • 请注意 127.0.0.1 不起作用
    • 在容器内配置mysql监听0.0.0.0:3306
    • 使用 -p 3306:3306 运行容器
    • 打开主机的防火墙以允许 :3306 上的流量
    • 运行您的客户端应用程序并尝试连接到:3306

    【讨论】:

    • 应用程序在我的本地运行,它不在容器中。而 mysql 容器 ip 是 172.17.0.2,使用它会产生超时错误。当您说主机防火墙时,是指macs防火墙吗?
    • 实际上通过dockerized应用程序我指的是你的mysql。现在进行编辑以使其更加中立。现在适用吗?
    • 好的,得到了​​那部分。第二点,主机防火墙阻止访问呢?我该如何解决?您还提到在主机网络上运行容器(我假设是 mysql),但我不是已经这样做了,我的意思是公开端口吗?
    • 今天许多操作系统都默认启用了防火墙。为了不向攻击者显示存在可以被攻击的机器,除非另有说明,否则他们会阻止“连接被拒绝”消息。您正在连接到从机器外部可见的 IP/端口 - 因此防火墙可能会捕获数据包并决定不响应。根据您运行的系统,您只需重新配置它以允许流量。
    • 如果已经可以从容器外部连接到您的数据库的 TCP 连接是可能的。在这种情况下,只需调查为什么您的 scala 应用程序不执行与 mysql 客户端相同的 TCP 连接。
    猜你喜欢
    • 2011-08-05
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2012-09-29
    • 2014-07-24
    • 2014-04-18
    相关资源
    最近更新 更多