简体   繁体   English

找不到本地主机或 127.0.0.1

[英]can't find localhost or 127.0.0.1

A wired problem.有线问题。 I wrote a scala program, which would connect to a local postgresql database.我写了一个 Scala 程序,它会连接到本地的 postgresql 数据库。 This program ran fine weeks ago, but when I run it today, it throws exception:这个程序几周前运行良好,但是当我今天运行它时,它抛出异常:

Caused by: java.net.UnknownHostException: localhost
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:68)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)

I change the connection url to "127.0.0.1", it throws that我将连接 url 更改为“127.0.0.1”,它抛出

Caused by: java.net.UnknownHostException: 127.0.0.1
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:68)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    ... 16 more

In these two situations, the connection urls are:在这两种情况下,连接 url 是:

jdbc:postgresql://localhost:5432/readinglist

jdbc:postgresql://127.0.0.1:5432/readinglist

Additional info may useful:其他信息可能有用:

  1. I can connect to the local postgresql database with "--host=localhost" option in shell on my computer我可以在我的计算机上的 shell 中使用“--host=localhost”选项连接到本地 postgresql 数据库

  2. my computer is mac book air and no vm or docker is running我的电脑是 mac book air,没有 vm 或 docker 正在运行

  3. change connection url to a remote database and it runs fine将连接 url 更改为远程数据库,它运行良好

  4. I run the program in IDEA as well as in the shell, both are failed to connect to the local database我在IDEA和shell中运行程序,都无法连接到本地数据库

  5. rebooting computer not working重新启动计算机不起作用

  6. add or comment "127.0.0.1 localhost" in /etc/hosts and then rebooting not working在 /etc/hosts 中添加或评论“127.0.0.1 localhost”,然后重新启动不起作用

More info can be afford if you want.如果您愿意,可以负担更多信息。

So I want to know what will make a program can't know "localhost" or "127.0.0.1", or without knowing the real cause, what else can I do to fix it?所以我想知道什么会使程序无法知道“localhost”或“127.0.0.1”,或者不知道真正的原因,我还能做些什么来解决它?

Did you happen to add a SOCKS proxy since it last worked?自上次工作以来,您是否碰巧添加了 SOCKS 代理? Look at your stack trace:查看您的堆栈跟踪:

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

The JVM is using a SOCKS proxy and that proxy apparently can't resolve localhost. JVM 正在使用 SOCKS 代理,而该代理显然无法解析 localhost。 Try removing the SOCKS proxy from your network config.尝试从您的网络配置中删除 SOCKS 代理。 In my experience Java is a little flakey with SOCKS.根据我的经验,Java 与 SOCKS 有点关系。

It sounds like a similar problem I just solved.这听起来像我刚刚解决的类似问题。 It turns out that it's the ShadowsocksX-R blocking the connection between PostgreSQL and scala program.原来是ShadowsocksX-R阻塞了PostgreSQL和scala程序的连接。 You can see if you have ShadowsocksX-R or any VPN is on.您可以查看是否有 ShadowsocksX-R 或任何 VPN 已打开。

Here is the ShadowsocksX-R version which caused problem on my Mac: screenshot of ShadowsocksX-R version这是在我的 Mac 上引起问题的 ShadowsocksX-R 版本ShadowsocksX-R 版本的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM