I have two AWS EC2 instances:
- Windows Server which will handle user requests for data.
- 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 = "x.xx.xxx.xxx"; //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"); };
sshClient.Connect();
if (sshClient.IsConnected) {
string MongoDBHost = "xx.xx.xx.xx"; // **PRIVATE IP OF UBUNTU AWS EC2?
uint MongoDBPort = 27017;
ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal("127.0.0.1", 5477, MongoDBHost, MongoDBPort);
forwardedPortLocal.Exception += delegate { Debug.WriteLine("FORWARDED PORT LOCAL EXCEPTION"); };
forwardedPortLocal.RequestReceived += delegate { Debug.WriteLine("FORWARDED PORT REQUEST RECEIVED"); };
sshClient.AddForwardedPort(forwardedPortLocal);
forwardedPortLocal.Start();
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 }
} }
};
profiles.InsertOne(document);
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.
Questions:
- 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.