java.net.ConnectException: Operation timed out (Connection t



我试图使用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",我得到一个连接拒绝错误。

请指出我的错误。

在网络方面,一些dockerized进程运行在与未dockerized进程不同的主机上。因此,连接到容器内的localhost127.0.0.1将连接回端口3306上没有侦听的相同容器-您正确地获得connection refused

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

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

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

另一个选择是在主机网络上运行容器。这样就不需要公开端口了(反正它是公开的),连接到localhost应该会得到预期的效果。

你可能想更熟悉Docker的网络。

编辑:到目前为止,我试图解释为什么你认为你提到的结果。一切都按照设计进行。现在让我们弄清楚你需要做什么来获得连接:
  • 注意127.0.0.1不能工作
  • 配置mysql在容器内监听0.0.0.0:3306
  • 使用-p 3306:3306运行容器
  • 打开主机防火墙,允许3306
  • 运行您的客户端应用程序并尝试连接到:3306

相关内容

  • 没有找到相关文章

最新更新