Ubutu 10.04, mysql, Java, localhost = WTF?
10 lipiec 2010 22:05
Today I killed 2 hours of my life for very studip problem.
I just got new notebook (IBM Thnkpad T400) with 4Gb, so, to be
able to use whole memory, installed Ubuntu 10.04 64 bit. Then
installed mysql-server, sun java, eclipse, some other staff and
started to work. mysql nd sun java was installed via apt with standard settings.
Created DB, successfully connected via MySQL Admin and Query Browser,
and, then, trying to connect from my app: failed
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Checking JDBC Url - everything is correct, checking username/password
- same, I can connect to localhost DB with using this
username/password with using standard mysql tools. But not from java
via JDBC.
WTF???
After spending 2 hours, found this
forum. After commenting bind-address in my.cnf
and changing jdbc url to use my computer IP address (like
192.168.0.157) instead of localhost it finally started to connect.
Why? Why it is not working with localhost out-of-box? Why I should
spent 2 hours for such stupid problems?
Problem resolved
Ok, today (after sleeping a little bit) I realized the source of
problem. It is not ubuntu, java, or mysql. It is (of course) - I'm.
Yesterday I changed by /etc/hosts - so, localhost was defined
incorrectly (by some reason). But, MySQL clients, in case localhost
server used - not used TCP at all - then connected to mysql directly.
JDBC driver does not use it - it always (looks like) tried to use TCP
and resolve hostname. It is why it connection worked in mysql tools,
but did not worked in java.
Ubuntu == cool!
Java == cool!
I'm == dummy developer :)