setPortForwardingL not working for MySQL

bob manc
  • bob manc

    bob manc - 2011-02-15

    This is what I am doing…
    Lets say my remote server is at "". I have MySQL and Tomcat running on it. Port 22 and 80 are open on it.

                JSch jsch = new JSch();
             String user = "root";
                String host = "";
                int sshPort = 22;
                Session session = jsch.getSession(user, host, sshPort);
                lport = 3306;
             rhost = "";
                rport = 3306;
                UserInfo ui = new MyUserInfo();
                int assigned_port = session.setPortForwardingL(lport, rhost, rport);
                if (session.isConnected())
                   System.out.println("localhost:" + assigned_port + " -> " + rhost + ":" + rport);
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:mydb"+lport, "root", "password");
                    Statement statement = con.createStatement();
                    ResultSet res = statement.executeQuery("SELECT * FROM  category");
                    while (
                        String name = res.getString("name");
                        System.out.println("name: " + name);

    The sysout says "localhost:3306 ->" so it appears all is working to that point. Then the system hangs for a while and eventually throws "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago."

    The funny thing is if I change lport and rport to 80 I can point my browser at http://localhost/ and see the page on the server.

    The only thing I can think of is port 80 and 22 are open on the server and 3306 is closed but I thought that was the whole point of port forwarding, to forward over 22. I can also access the db using the port forwarding built into SQLyog using the same configuration.

    Am I doing something wrong?


  • bob manc

    bob manc - 2011-02-15

    Sorry. The db line above should read. Still doesn't work.

                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:"+lport+"/mydb", "root", "password");

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks