localhost on Vista
Last weekend I had to solve some problem on a Vista-computer. The problem was explained quite badly but well I thought I could give it a try and solve it… And since I’m an IT-guy, problems can always get solved using Google!
So the problem was explained as follow: “MySQL is acting strange, I can’t connect to it using Java.”. Oh well they also gave me the exception stacktrace so I could see what the problem was:
Exception in thread “main” java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:179)
Not that much to work with as you can see… So as I’m the Java freak, I first had to see that Java-code…
Socket s = new Socket("localhost", 3306);
s.close();
So first thing first, check whether MySQL is running and if it’s running on port 3306. Well, it was… So after some seconds on the Internet, I found out that Vista could have the strange behaviour to erase the “localhost” setting in the hosts file. So before I changed something on the computer I tried the above code but change “localhost” by the IP-address “127.0.0.1″. And since that was working, I was quite sure that the host-resolving from localhost to 127.0.0.1 wasn’t working properly.
So, where does Vista store the host-resolving? Well Linux-guys will be happy to notice that they are stored in some folder called “etc”. I have no clue if the Unix guys riped it from Windows or vice versa but I do notice the fact that it’s the same folder. But Vista is still Vista so that folder isn’t just under your C-drive!! It’s located at C:\Windows\System32\drivers\etc\. And there we can find the hosts file… And yes as I opened the file only one line was available: “::0 localhost“. That doesn’t seem to be that much, so to get the localhost resolved again one should add the line “127.0.0.1 localhost” to that file. And since the security is that tight on Vista one can’t just open that file using notepad and edit it… No, you should open notepad as an Administrator and edit the file. Otherwise, Vista will be so kind to tell you, that you don’t have the rights to save the file.
So, a quite long story to tell something not that complicated. But I needed to share this Vista stuff with others. So next time, someone is telling you that something related to localhost isn’t working on Vista! First check if it is working with the IP-address 127.0.0.1! Ifso, you can be quite sure that the problem is solved once you updated the hosts file!
Goodluck!