Connecting from Windows EC2 server to a Ubuntu MongoDB EC2 server via SSH and port forwarding?

I have two AWS EC2 instances:

  1. Windows Server which will handle user requests for data.
  2. Ubuntu server with MongoDB database on it to be accessed and manipulated by Windows Server.

I am attempting to write a program in C# .NET that will be able to connect to the Ubuntu server and its MongoDB when run from my local computer or from the Windows Server.

I am using Renci SSH.NET and the standard MongoDB driver. For networking, I found one old guide here which helped me get part way. I have so far:

string SSHServerUserName = "ubuntu";
string SSHServerHost = ""; //public IP of ubuntu server as per AWS
PrivateKeyFile keyFile = new PrivateKeyFile(@"H:\MongoDB.pem", "filePass"); 
PrivateKeyAuthenticationMethod authenticationMethod = new PrivateKeyAuthenticationMethod(SSHServerUserName, keyFile);

ConnectionInfo connectionInfo = new ConnectionInfo(SSHServerHost, SSHServerUserName, authenticationMethod); //uses DefaultPort = 22

SshClient sshClient = new SshClient(connectionInfo);
sshClient.ErrorOccurred += delegate { Debug.WriteLine("SSH ERROR OCCURRED"); };
sshClient.HostKeyReceived += delegate { Debug.WriteLine("SSH HOST KEY RECEIVED"); };

if (sshClient.IsConnected) {

    string MongoDBHost = "xx.xx.xx.xx"; // **PRIVATE IP OF UBUNTU AWS EC2? 
    uint MongoDBPort = 27017;

    ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal("", 5477, MongoDBHost, MongoDBPort);

    forwardedPortLocal.Exception += delegate { Debug.WriteLine("FORWARDED PORT LOCAL EXCEPTION"); };
    forwardedPortLocal.RequestReceived += delegate { Debug.WriteLine("FORWARDED PORT REQUEST RECEIVED"); };


    MongoClientSettings mongoSettings = new MongoClientSettings();
    mongoSettings.Server = new MongoServerAddress("localhost", 5477); //IS THIS RIGHT?
    MongoClient mongoClient = new MongoClient(mongoSettings);

    var iMongoDatabase = mongoClient.GetDatabase("test");

    var profiles = iMongoDatabase.GetCollection<IConvertibleToBsonDocument>("profiles");

    Debug.WriteLine("GOT THE COLLECTION:"); //debugs out okay...

    var document = new BsonDocument {
        { "name", "userName" },
        { "type", "newUser" },
        { "count", 1 },
        { "info", new BsonDocument
            { "x", 203 },
            { "y", 102 }
        } }


    Debug.Write("ATTEMPTED TO INSERT ONE"); //does not debug out

I am getting sshClient.IsConnected() as true. However, I do not know if my forwarded port is working correctly or to what extent it is or isn’t connecting to the Mongo Database. I can see from MongoDB Compass (GUI database navigator) nothing is being inserted, and I can’t get the ATTEMPTED TO INSERT ONE debug code to write so it’s certainly breaking somewhere before that point.


  • How do I test the Forwarded Local Port? I see FORWARDED PORT REQUEST RECEIVED outputs but don’t know if they’re working.
  • Is the MongoDB server code (IPs, ports, “localhost”) correct for this configuration? How do I test to see if it has connected properly to the database?
  • What is going wrong in the above?

This is my first database and EC2 configuration. Thanks for any help.

I see a thread here where someone says they are trying to do the same thing and they were told it is impossible. Similarly, here they were told it’s impossible.

However that was 8 years ago. Is it still impossible to connect to a MongoDB by C#? I can get an SSH connection to my EC2 instance as shown. However, I can’t see any method to connect to the MongoClient. I have TLS/SSL disabled and no username or password when I connect in Compass.

If we are not able to connect to a MongoDB by C#, how are we supposed to connect? C# is my preferred language but if I have to learn another language to use MongoDB I will grudgingly do so.

Any solution or ideas? How do you set up a server or program that can insert documents or manipulate a MongoDB?

Hello @MikeM ,

Welcome to The MongoDB Community Forums! :wave:

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, then you can try achieving your goal using SSH tunnel which could be setup outside the script instead of trying to do so programatically via C#. You can set up a forward tunnel from the Windows machine to the Ubuntu machine, or a reverse tunnel from the other direction. These can be set up using the ssh command in Ubuntu (for a reverse tunnel), or Putty in Windows (for a forward tunnel).


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.