2014년 12월 6일 토요일

[mongodb-csharp] Re: Connect to MongDB on Amazon EC2

I know this is an incredibly old post... But I'm running into this very same issue. 

Is there still no clean way to connect to an EC2 instance of MongoDB via C#? Using PuTTY the one parameter I see missing from the Mongo ConnectionString is any reference to the ppk, EC2 requires to connect in.

Even when I wrap the Mongo connection stuff within an SSH tunnel in C#, it doesn't appear the Mongo driver picks up on this.  Code below still yields the same:

Additional information: Unable to connect to server bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22: A non-recoverable error occurred during a database lookup.

                // Connect through SSH Client
                using (var client = new SshClient(con))
                {
                    client.Connect();
                    var cmd = client.RunCommand("ls");
                    var output = cmd.Result;
                    MessageBox.Show(output.ToString());

                    // Upload to MongoDB (ec2 server)
                    var connectionString = "mongodb://USERNAME:PASSWORD@bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22";
                    var mongoEC2 = new MongoClient(connectionString);
                    var server = mongoEC2.GetServer();
                    var database = server.GetDatabase("DATABASE");
                    var collection = database.GetCollection<MetadataTemplate>("COLLECTION");

                    // Insert Record into DB
                    collection.Insert(metaUpload);
                }

...

This is only happening when uploading into EC2. Same code applied to local databases are not creating this problem.

Any new thoughts? 

On Tuesday, May 3, 2011 10:08:03 AM UTC-5, Robert Stam wrote:
Sorry, I don't have any experience with EC2 either, but I'm told this
is standard EC2 stuff and that you have two options:

1. create an ssh tunnel
2. open a port on the firewall

Once you have that in place, you can use the mongo shell to test the
connection to MongoDB. If the mongo shell can connect, then the C#
driver will also be able to connect. There is nothing different you
would do in the C# driver to support this (except to make sure that
the connection string has the right hostname or IP address and port
number).

On May 3, 10:45 am, Nucleon Software <off...@nucleonsoftware.com>
wrote:
> Hello Robert,
> one of your customer use mongodb on the ec2 and they all connect using
> secure keys (with putty and use pagent to host the file). I do now
> know really how the login/connection works, since we have no
> experience with amazon ec2.
>
> Kind Regards
> Alparslan Candan
>
> 2011/5/3 Robert Stam <rstam10...@gmail.com>:
>
> > The same way as you connect to MongoDB no matter where it is hosted?
>
> > var connectionString = "mongodb://hostname";
> > var server = MongoServer.Create(connectionString);
>
> > I'm probably not understanding your question...
>
> > On May 3, 6:06 am, Nucleon Software <nucso...@gmail.com> wrote:
> >> Hello,
> >> how can i connect a Mongodb on the Amazon EC2 using C# Driver?
>
> >> --
> >> Best Regards,
> >> Alparslan Candan
>
> --
> Best Regards,
> Nucleon Software
> --------------------------------------------------------------------------------
> E-Mail:  off...@nucleonsoftware.com
> Web:    http://www.nucleonsoftware.com
> Twitter:  http://twitter.com/nucleonsoftware
> --------------------------------------------------------------------------------





Hello, you check open port on your server:http://www.portchecktool.com . If close you set open firewall for this port. After, go to security group on EC2 and add rule for group:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

Good luck!

I'm a Developer



Thanks for the suggestion Nasa, but I don't think that my issue is in connecting to EC2. 

I can successfully use my .Net application to send commands to the EC2 terminal using SSH.Net. But what I can't do is use any of the existing C# Mongo driver commands.

Thus, I the issue is that I can't relay Mongo commands to the mongo database sitting in the EC2 from my .NET application that's running locally.

                    client.Connect();
                    var cmd = client.RunCommand("ls");
                    var output = cmd.Result;
                    MessageBox.Show(output.ToString()); // This works

                    // Upload to MongoDB (ec2 server)
                    var connectionString = "mongodb://USERNAME:PASSWORD@bitnami@ec2-54-174-43-69.compute-1.amazonaws.com:22";
                    var mongoEC2 = new MongoClient(connectionString);
                    var server = mongoEC2.GetServer();
                    var database = server.GetDatabase("DATABASE");
                    var collection = database.GetCollection<MetadataTemplate>("COLLECTION");

                    // Insert Record into DB
                    collection.Insert(metaUpload); // This DOES NOT work. 

Can someone at least confirm whether or not they've been able to use the C# driver to an EC2 instance of Mongo?

It'd be great to know if its a problem on my end or a limitation of the driver.

- Ahmed 

On Wednesday, December 3, 2014 8:19:17 AM UTC-6, NASAVIETNAM wrote:

Hello, you check open port on your server:http://www.portchecktool.com . If close you set open firewall for this port. After, go to security group on EC2 and add rule for group:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html



Can you share your error exception. I'll try to debug for you.



Thanks a ton for the offer Nasa!

I've now created a separate thread specifically for the issue of connecting to EC2 using the C-Sharp Driver for MongoDB.

The thread contains a way for you to replicate the issue I'm seeing exactly and to hopefully help me find a solution.

Connect to MongoDB in EC2 from .Net application (With Test Case for Debugging)


댓글 없음:

댓글 쓰기