title: Connecting to Mongo database through SSH tunnel in Java
body: <p><strong>FIXED (edited code to reflect changes I made)</strong></p>
  <p>I'm trying to connect to a Mongo database through an SSH tunnel using Java.</p>
  <p>I'm using the Mongo driver 3.0.2 and jcraft (JSch) to create an SSH tunnel.
  The idea is that I:</p>
  <ul>
  <li>connect to the machine hosting the MongoDB installation through SSH</li>
  <li>set up port forwarding from a local port to the remote MongoDB port</li>
  <li>connect to MongoDB remotely</li>
  </ul>
  <p>My code looks like this:</p>
  <pre><code>// forwarding ports
  private static final String LOCAL_HOST = &quot;localhost&quot;;
  private static final String REMOTE_HOST = &quot;127.0.0.1&quot;;
  private static final Integer LOCAL_PORT = 8988;
  private static final Integer REMOTE_PORT = 27017; // Default mongodb port
  
  // ssh connection info
  private static final String SSH_USER = &quot;&lt;username&gt;&quot;;
  private static final String SSH_PASSWORD = &quot;&lt;password&gt;&quot;;
  private static final String SSH_HOST = &quot;&lt;remote host&gt;&quot;;
  private static final Integer SSH_PORT = 22;
  
  private static Session sshSession;
  
  public static void main(String[] args) {
  try {
      java.util.Properties config = new java.util.Properties();
      config.put(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
      JSch jsch = new JSch();
      sshSession = null;
      sshSession = jsch.getSession(SSH_USER, SSH_HOST, SSH_PORT);
      sshSession.setPassword(SSH_PASSWORD);
      sshSession.setConfig(config);
      sshSession.connect();
      sshSession.setPortForwardingL(LOCAL_PORT, REMOTE_HOST, REMOTE_PORT);
  
      MongoClient mongoClient = new MongoClient(LOCAL_HOST, LOCAL_PORT);
      mongoClient.setReadPreference(ReadPreference.nearest());
      MongoCursor&lt;String&gt; dbNames = mongoClient.listDatabaseNames().iterator();
      while (dbNames.hasNext()) {
      System.out.println(dbNames.next());
      }
  } catch (Exception e) {
      e.printStackTrace();
  } finally {
      sshSession.delPortForwardingL(LOCAL_PORT);
      sshSession.disconnect();
  }
  }
  </code></pre>
  <p>This code, when run, <strike>doesn't</strike> <strong>EDIT: does</strong> work. <strike>Connecting to the SSH server works just fine, but connecting to the Mongo database behind it doesn't work and returns this error:</strike></p>
  <pre class="lang-none prettyprint-override"><code>INFO: Exception in monitor thread while connecting to server localhost:8988
  com.mongodb.MongoSocketReadException: Prematurely reached end of stream
      at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
      at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:491)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:221)
      at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
      at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
      at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
      at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
      at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  <p>I've tried doing this through command line as follows:</p>
  <pre class="lang-none prettyprint-override"><code>$ ssh &lt;user&gt;@&lt;host&gt; -p 22 -X -C
  $ &lt;enter requested password&gt;
  &lt;user&gt;@&lt;host&gt;$ mongo
  &lt;user&gt;@&lt;host&gt;$ MongoDB shell version: 2.6.10
  &lt;user&gt;@&lt;host&gt;$ connecting to: test
  </code></pre>
  <p>So this seems to work. I'm at a loss as to why the Java code (which should be doing roughly the same thing) doesn't work.</p>