title: How do I access a remote aws lightsail mongodb over ssh tunnel
body: <p>I have a Lightsail AWS instance up and running with a MEAN stack. I have an existing MEAN stack running on a different network. At the moment the node server.js connects to localhost for the mongo bit (on same machine) and all I want to do is replace the localhost with a connection to my mongo running on my AWS remote server.</p>
  
  <p>I understand, that for security reasons, it is best to ssh tunnel this connection, which I think I am familiar with.</p>
  
  <p>What I have done so far is this:</p>
  
  <p>In a console on the machine hosting the node server (remote to the db) I have run:</p>
  
  <pre><code>ssh -L 8181:127.0.0.1:80 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami@31.16.56.125 -N
  </code></pre>
  
  <p>I can then browse to the RockMongo UI from the local machine using localhost:8181/rockmongo ...yay.</p>
  
  <p>If I then run the following: </p>
  
  <pre><code>ssh -L 8181:127.0.0.1:27017 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami@31.16.56.125 -N
  </code></pre>
  
  <p>(27017 being the mongo port)</p>
  
  <p>Then try and access the db from my remote machine using:</p>
  
  <pre><code>mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>~]
    2017-12-28T22:11:09.791+0000 Error: couldn't connect to server 31.16.56.125:8181 (31.16.56.125), connection attempt failed at src/mongo/shell/mongo.js:148
    exception: connect failed</p>
  </blockquote>
  
  <p>Am I doing this wrong? i.e. is the tunnel only for http connections and not mongo command line use? Do I need to test the connection some other way?</p>
  
  <p>I've Googled all over the place for this and not had much luck (a lot of the AWS docs suggest punching a hole in the firewall - which one can no longer do!)</p>