Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to MySQL on Windows 7 using JDBC, but works via phpmyadmin and other admin tools?

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
like image 357
Hardeep Avatar asked Nov 05 '22 03:11

Hardeep


1 Answers

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
like image 173
Hardeep Avatar answered Nov 07 '22 20:11

Hardeep