tags:

views:

3930

answers:

6

Pretty straightforward stuff, here -- I'm just not good enough with mysql to understand what it wants from me.

I've got a short java test-case that opens a connection on mysql on my dev system but, when I try to put it to my server, it fails.

Any help in tracking this down would be most appreciated.

Thanks!

Test Code

import  java.util.*;
import  java.sql.*;

public  class mysqltest {

    static  public  void  getDBConnection() {
        System.out.println ("Start of getDBConnection.");

        Connection  conn        = null;
        String      url         = "jdbc:mysql://localhost:3306/";
        String      dbName      = "magnets_development";
        String      driver      = "com.mysql.jdbc.Driver";
        String      userName    = "*****";  // blanked for publication
        String      password    = "*****";

        try {
            Class.forName (driver).newInstance();
            System.out.println ("driver.newInstance gotten.");
            conn = DriverManager.getConnection (url+dbName, userName, password);
            System.out.println ("Connection gotten: " + conn + ".");
            Statement sql     = conn.createStatement ();
            ResultSet results = sql.executeQuery ("use " + dbName + ";");
        }
        catch (Exception ex) {
            System.out.println ("*** Got exception.");
            ex.printStackTrace();
        }
    }

    public static void main(String args[]) {
        System.out.println ("Main started.");
        mysqltest.getDBConnection();
    }
}

Dev System Output (Expected/correct response)

olie$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
Connection gotten: com.mysql.jdbc.Connection@c980c9.
olie$

Server Output (Error I'm trying to track-down) (Some blank lines removed.)

mini$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
*** Got exception.
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused

STACKTRACE:

java.net.ConnectException: Connection refused
    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:432)
    at java.net.Socket.connect(Socket.java:520)
    at java.net.Socket.connect(Socket.java:470)
    at java.net.Socket.<init>(Socket.java:367)
    at java.net.Socket.<init>(Socket.java:209)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:525)
    at java.sql.DriverManager.getConnection(DriverManager.java:140)
    at mysqltest.getDBConnection(mysqltest.java:34)
    at mysqltest.main(mysqltest.java:49)

** END NESTED EXCEPTION **

Last packet sent to the server was 3 ms ago.
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:525)
    at java.sql.DriverManager.getConnection(DriverManager.java:140)
    at mysqltest.getDBConnection(mysqltest.java:34)
    at mysqltest.main(mysqltest.java:49)
mini$
A: 

Have you verified that you can connect to the database on the server, with the name and password.

Also if you are connecting via a url you should not need the following line.

properties.setProperty("socket", "/Applications/MAMP/tmp/mysql/mysql.sock");

The above property seems specific to Os X. Are you running the server on os X?

Milhous
Olie
A: 

Your URL has localhost in it. Are you trying to connect to this server on the network? Your URL may be incorrect.

MetroidFan2002
+2  A: 

Also, make sure that your MySQL server's user account for the database user allows connections from other addresses but localhost. For security reasons in smaller servers it is possible (and possibly default?) to limit connections to localhost, so that only the local web application can access the database using the magic-numbered user/password combination.

Uri
+1  A: 

There's only one reason for "connection refused" message, never mind if it's mysql or any other service. The server isn't listening. Try "netstat -an" and grep for port 3306 to validate it.

Yoni Roit
Ok netstat -an | grep 3306returns nothing -- so that's a step in the right direction. Uh... mysql is clearly running. And I can log-in from command line. I take it there's some configure thing to turn on 3306-listening...?
Olie
Are you logging in from the command line on the same machine or a different machine?
Uri
A: 

Here is my code that works on mysql on os X

public SQL(String host, String port, String database, String userid, String password)
 {
  queryType = QUERYTYPE.Single;
  String driver = "org.gjt.mm.mysql.Driver";
  String url = "jdbc:mysql://" + host;
  if (port != null)
   {
    url += ":" + port;
   }
  else
   {
    url += ":" + defaultPort;
   }
  url += "/" + database;
  try
   {
    Class.forName(driver);

    connection = DriverManager.getConnection(url, userid, password);
   }
  catch (Exception e)
   {
    e.printStackTrace();
   }
 }
Milhous
+1  A: 

Bah! My apologies -- I just had "local connections only" set up. I was confused by the fact that the java app was running locally (so seemed a "local connection" but, because it is connecting via TCP, it's a "remote" connection, even though it originates on localhost.

I will eventually learn how to administer these things. In the mean time, I hope someone else can learn from my mistakes.

Thank you to all who put time into trying to help me. I have since allowed "remote" connections, but all of my user-accounts are 'username'@'localhost" only (i.e., there is no 'username'@'%', which would allow any machine to connect.) Hopefully, I got that part right, at least.

Thanks again!

Olie
And thanks to uzhin (honorable mention to Uri) for the hints I needed to eventually figure this out. + votes to both of you.
Olie
No prob. I fell for that thing a long time ago. I was using DB2 and switched to MySQL
Uri