5

TL;DR:

Every time I try to use JDBC to connect to a local MySQL server (that works on tons of other sql clients), it doesn't connect at all and I get an exception:

  • Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  • .... stack trace ....
  • Caused by: java.net.SocketException: Invalid argument: connect

More detailed info:

I've got a local MySQL server running that I can connect to with using the following info:

  • address: localhost or 127.0.0.1 (both work)
  • port: 3306
  • and my username/password/db name

Using the applications: HeidiSQL, phpmyadmin, MySQL Workbench and mysqladmin

Running mysqladmin version confirms that the server is up, working, and has tcp/ip enabled.

Doing mysqladmin variables shows that "skip_networking" is off (see below for full config).

The MySQL log shows nothing out of the ordinary (also below).

I'm using JDBC connector v5.1.15, mysql-connector-java-5.1.15-bin.jar. It's in my project class path with Eclipse.

All windows 7 (Ultimate, 64bit if it matters) firewalls are off.

Yet I still get the stack trace error as shown below. I can't for the life of me figure out what's wrong when everything else is able to connect just fine...

I've tried to include all relevant information below. I'm hoping it's just something simple and stupid that I've overlooked and can't find because I'm looking too hard...but eh who knows :/ heh.

I've tried everything I've been able to find in other similar questions online but nothing seems to work (incl. https://stackoverflow.com/a/2985169/211750 , https://stackoverflow.com/a/2103092/211750 , https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally and others)

My test code:

package sqltest;


import com.mysql.jdbc.Driver;
import java.sql.DriverManager;
import java.util.Properties;

public class main {

    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost:3306/dbname?user=user&password=****&";        

        Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();

        DriverManager.getConnection(url);
    }

}

Stack trace: http://pastebin.com/qv332y86

MySQL variables dump: http://pastebin.com/merSEDdD

My.ini: http://pastebin.com/EWZX2enz

MySQL mysqladmin version:

Server version          5.5.20-log
Protocol version        10
Connection              localhost via TCP/IP
TCP port                3306
Uptime:                 1 min 37 sec

Threads: 1  Questions: 17  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.175

MySQL log:

120314 21:37:20 [Note] Plugin 'FEDERATED' is disabled.
120314 21:37:20 InnoDB: The InnoDB memory heap is disabled
120314 21:37:20 InnoDB: Mutexes and rw_locks use Windows interlocked functions
120314 21:37:20 InnoDB: Compressed tables use zlib 1.2.3
120314 21:37:20 InnoDB: Initializing buffer pool, size = 128.0M
120314 21:37:20 InnoDB: Completed initialization of buffer pool
120314 21:37:20 InnoDB: highest supported file format is Barracuda.
120314 21:37:20  InnoDB: Waiting for the background threads to start
120314 21:37:21 InnoDB: 1.1.8 started; log sequence number 1619921
120314 21:37:21 [Note] Event Scheduler: Loaded 0 events
120314 21:37:21 [Note] wampmysqld: ready for connections.
Version: '5.5.20-log'  socket: ''  port: 3306  MySQL Community Server (GPL)
120314 21:38:02 [Note] wampmysqld: Normal shutdown

120314 21:38:02 [Note] Event Scheduler: Purging the queue. 0 events
120314 21:38:02  InnoDB: Starting shutdown...
120314 21:38:02  InnoDB: Shutdown completed; log sequence number 1619921
120314 21:38:02 [Note] wampmysqld: Shutdown complete
Community
  • 1
  • 1
Hardeep
  • 493
  • 6
  • 12
  • 1
    http://stackoverflow.com/questions/8216713/java-net-socketexception-invalid-argument-connect seems to suggest a problem with IPV6. Is that what you're using? – Jim Garrison Mar 15 '12 at 03:03
  • +1: Well written question. Also: have you tried to change your connection string to 127.0.0.1 as [this comment](http://stackoverflow.com/questions/8216713/java-net-socketexception-invalid-argument-connect#comment10343027_8347425) suggests? – Deco Mar 15 '12 at 03:24
  • Maybe your firewall blocks connections for java.exe? – a_horse_with_no_name Mar 15 '12 at 07:56
  • @JimGarrison I tried with it both enabled and disabled already, yes. Didn't seem to change anything though. I also disabled all extra network adapters in case. And last night, after posting this, I also tried it by closing every single extra process and service I could that was not required for the project or windows to run. – Hardeep Mar 15 '12 at 19:57
  • @Deco thanks :) I've tried with both localhost and 127.0.0.1 doesn't seem to matter. Note however, I can connect with either localhost or 127.0.0.1 from any of the other SQL admin tools. – Hardeep Mar 15 '12 at 19:58
  • @a_horse_with_no_name I've disabled windows firewall entirely and have no other firewall installed. I also tried disabling A/V in case. – Hardeep Mar 15 '12 at 19:58
  • Actually upon further investigation, it was indeed the IPV4/IPV6 issue from @JimGarrison ...but since I was doing this in tomcat I had to go through a round-about way of setting the system property. – Hardeep Mar 15 '12 at 22:43

2 Answers2

2

Turned out it was Java preferring IPv6 messing things up so I had to add -Djava.net.preferIPv4Stack=true, but since I was running things under tomcat I had to go through a round-about way of setting the property. I'm not sure what the standard way of doing it is (if any) but I got it working by editing Catalina.bat and adding -Djava.net.preferIPv4Stack=true to the end of the file. See:

rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:end
Hardeep
  • 493
  • 6
  • 12
0

or for tomcat, CATALINA_OPTS can be set in environment variable

Tejas
  • 6,080
  • 1
  • 17
  • 25