Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MongoDB driver copydb admin login

Tags:

c#

mongodb

I'm trying to use command copydb in mongodb.

When I do so I get the following exception:

Command 'copydb' failed: access denied; use admin db (response: { "errmsg" : "access denied; use         
admin db", "ok" : 0.0 })

I have tried to log in as admin but failed because I'm not using username & password.

How do I log in as admin without username & password? Why do I need to log in as admin if I already have privileges to do drop?

Thanks in advance

m_mongoDatabase.RunCommand(new CommandDocument(new BsonElement("copydb", (BsonValue) 1),
    new BsonElement("fromdb", (BsonValue) from),
    new BsonElement("fromhost", (BsonValue) fromHost),
    new BsonElement("todb", (BsonValue) to)));
like image 695
stackoverflowNewB Avatar asked Dec 09 '25 20:12

stackoverflowNewB


1 Answers

I think the problem is that you are not connecting to the "admin" database on the target instance first.

I can perform the operation you require using the following code:

var client = new MongoClient(MongoUrl.Create("mongodb://localhost:27018"));
        var m_mongoDatabase = client.GetServer().GetDatabase("admin");
        var result = m_mongoDatabase.RunCommand(
            new CommandDocument(new BsonElement("copydb", 1),
                new BsonElement("fromhost", "localhost"),
                new BsonElement("fromdb", "sourcedb"),
                new BsonElement("todb", "targetdb")));

Notice that I acquire a ref to the admin database first. When I didn't do that and used another regular database I got the same error as you.

So to recap, use the admin database for the value of "m_mongoDatabase"

Hope this helps

Edit:This is the same behaviour when using the mongodb shell, so it doesn't appear to be an issue with the csharpdriver

like image 146
JB. Avatar answered Dec 12 '25 12:12

JB.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!