GrSrv
GrSrv

Reputation: 559

How to use Mongo --authenticationDatabase in perl

This is how I connect to mongoDB in perl

my $client = MongoDB::MongoClient->new(
    host     => 'badhost',
    port     => 37018,
    username => 'abc_user',
    password => 'abc_user',
    db_name  => 'cust_projectdb'
) or die "unable to connect to mongo\n";

when the connection string from shell is mongo cust_projectdb --port 37018 -uabc_user -pabc_user --host badhost

but how to connect when the connection string uses a different collection for authentication like this:

mongo cust_projectdb --port 37018 -uabc_user -pabc_user --host badhost --authenticationDatabase admin

Upvotes: 1

Views: 613

Answers (1)

Borodin
Borodin

Reputation: 126732

The db_name parameter sets the authentication database when you are connecting to the server, so you would want

db_name => 'admin'

to do the equivalent of your command line connection, although admin is the default here anyway so you can just omit it altogether

If you want to access the cust_projectdb database then you need to use get_database on the MongoDB::MongoClient object you just created to do that

my $db = $client->get_database('cust_projectdb')

Upvotes: 2

Related Questions