Reputation: 305
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
Upvotes: 4
Views: 22131
Reputation: 578
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
Upvotes: 2
Reputation: 10088
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
Upvotes: 1
Reputation: 777
Like this:
var cliente = new MongoClient("mongodb://usuariocualquiera:tuclave@localhost:27017/BASEDEDATOS");
and could be call
var collection = database.GetCollection<BsonDocument>("CUALQUIERCOLECCION");
Upvotes: 3
Reputation: 12187
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.
Upvotes: 7
Reputation: 1458
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.
Upvotes: 1
Reputation: 538
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
Upvotes: 1