User ID and password authentication
- Last Updated: July 9, 2021
- 1 minute read
- DataDirect Connectors
- JDBC
- MongoDB 6.1
- Documentation
To configure the driver to use basic authentication:
- Set the ServerName property to the name or the IP address of the MongoDB server to which you want to connect. For example, myserver.
- Set the AuthenticationMethod property to
userIdPassword(the default). - Optionally, set the DatabaseName property to specify the name of the database to which you are connecting.
- Set the User property to specify your user ID.
- Set the Password property to specify your password.
- Optionally, set the AuthenticationDatabase property to specify the
database in which your user ID was created. This value allows you to explicitly select a
set of credentials and permissions when the same user ID was created in multiple databases
on the server. Note: We recommend specifying a value for the AuthenticationDatabase to ensure the correct permissions are used for your connection. If you do not specify a value for this property, the driver attempts to use your user ID with the database specified by the Databasename property. If your user ID was not created in the specified database, the driver will attempt to connect to the Admin database using your user ID and password.
- Optionally, specify values for any additional properties you want to configure.
Note: The User and Password properties are not required to be stored in the
connection string. They can also be passed separately by the application.
The following examples demonstrate a session with user ID and password authentication enabled.
For a connection URL:
Connection conn = DriverManager.getConnection
("jdbc:datadirect:mongodb://myserver:27017;AuthenticationDatabase=mydb2;
DatabaseName=mydb;user=jsmith;password=secret;");
For a data source:
MongoDBDataSource mds = new MongoDBDataSource();
mds.setDescription("My MongoDB Data Source");
mds.setAuthenticationDatabase("mydb2");
mds.setDatabaseName("mydb");
mds.setServerName("myserver");