To configure an application to use the newly created database user, proceed as follows:
- We start by creating a simple application that reads the name and address for the first user in the biglittle database:
import pprint
from pymongo import MongoClient
client = MongoClient('localhost');
find_result = client.biglittle.users.find_one({},{"name":1,"address":1})
pprint.pprint(find_result)
- As the database is now configured to require authentication, we expect this application to fail, as seen in the following screenshot:
- We then modify the initial client connection to include a reference to the database user with least privileges:
import pprint
from pymongo import MongoClient
client = MongoClient(
'localhost',
username='biglittle_reader',
password='password',
authSource='biglittle',
authMechanism='SCRAM-SHA-256');
find_result = client.biglittle.users...