The connection string for Mongo process has /database option . What does it mean? Does it mean it authenticates the particular database on mongo server.Thanks in advance
-
http://stackoverflow.com/questions/10101506/mongodb-authentication – Amol M Kulkarni Mar 25 '13 at 12:53
6 Answers
With the C# driver you typically would not use the option of putting a database name on the connection string. It is partially supported to provide some level of compatibility with other drivers.
MongoServer.Create ignores the database name. Any credentials (username/password) on the connection string are used as default credentials for all databases.
The database name is only used by MongoDatabase.Create, which calls MongoServer.Create and then just calls GetDatabase for you.
So:
var connectionString = "mongodb://localhost/database";
var database = MongoDatabase.Create(connectionString);
is just a shortcut for:
var connectionString = "mongodb://localhost";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("database");
No authentication actually happens until you first try to use a database.

- 12,039
- 2
- 39
- 36
-
Thank you... but i actually don't understand that connection strings with username and password does the authentication process or it is just a way of connecting to server. And also when we provide a username and password in connection string and then call getdatabase method then it says invalid credentials for database? Why so? – Himani Talesara Apr 16 '12 at 16:55
-
The connection string merely provides the default credentials, it doesn't actually authenticate. The authentication happens later when you actually try to use a database. GetDatabase should not throw an authentication exception if the credentials aren't valid for that database. The exception will come when you actually try to do something with the database. – Robert Stam Apr 16 '12 at 20:13
-
Ok.Then when does authentication takes place? And what does default credentials do? Are Default credentials provided by us ? If you have any idea on authentication in terms of lines of code then please reply in terms of some code .Please. – Himani Talesara Apr 17 '12 at 04:21
-
Authentication happens when you actually try to use a database. That would be any operation: Insert, Find, Update, etc... Default credentials are used when you call GetDatabase and don't provide any credentials. If you are using different credentials for different databases you need to pass the credentials as the 2nd argument to GetDatabase: server.GetDatabase("database", credentials). Default credentials are only useful when you are using the same credentials everywhere (which is not always the case). – Robert Stam Apr 17 '12 at 04:54
Like this:
var cliente = new MongoClient("mongodb://usuariocualquiera:tuclave@localhost:27017/BASEDEDATOS");
and could be call
var collection = database.GetCollection<BsonDocument>("CUALQUIERCOLECCION");

- 8,165
- 14
- 74
- 101

- 747
- 5
- 5
It will connect to the named database. If the database is not present it will make a connection and upon creating a new object it will instantiate the database

- 528
- 3
- 20
-
It means connection string only provides a connection or it does the authentication also. – Himani Talesara Apr 16 '12 at 05:42
-
string connectionString = "mongodb://mongodb:mongodb@localhost:27017"; var server = MongoServer.Create(connectionString); server.Connect(); What does it results – Himani Talesara Apr 16 '12 at 05:43
You will create/return existing instance of the mongod process with user mongodb created in the admin database and password mongodb on localhost:27017. You shouldn't need to call Connect() - the driver will do this automatically as required.

- 1,458
- 9
- 9
-
It means we have to explicitly add mongo user from interactive shell or we have to specify it in the connection string – Himani Talesara Apr 16 '12 at 17:03
-
Is it necessary to add a user to particular database before we authenticate? – Himani Talesara Apr 17 '12 at 04:23
Assuming user account was created in the admin database, and assuming you are using the command line interface (CLI) program called "mongo" you can connect to a 3 node replicaset with username and password with the following:
Syntax:
mongo --host "<replicaset name>/<host 1 resolvable name>:<host 1 port>,<host 2 resolvable name>:<host 2 port>,<host 3 resolvable name>:<host 3 port>" --username <username> --password <password> --authenticationDatabase <database name>
Example:
mongo --host "replset1/ip-172-31-48-110.eu-west-1.compute.internal:27017,ip-172-31-116-186.eu-west-1.compute.internal:27017,ip-172-31-29-140.eu-west-1.compute.internal:27017" --username barry --password supersecretpassword --authenticationDatabase admin

- 9,740
- 11
- 65
- 79
I am sharing my experience on the matter which worked for me. So basically the composition is the following for the string:
"mongodb://[user]:[password]@[host]:[port]"
and some example:
"mongodb://yourusername:yourpassword@localhost:27017"
I have used as refference the documentation here

- 510
- 7
- 15