Authentication With Motor

To use authentication, you must start mongod with --auth or, for replica sets or sharded clusters, --keyFile. Create an admin user and optionally normal users or read-only users.

Synchronous Authentication

To create an authenticated MotorClient before starting the IOLoop, use a MongoDB connection URI:

admin_uri = "mongodb://admin:pass@localhost:27017"
client = motor.MotorClient(admin_uri).open_sync()

normal_uri = "mongodb://user:pass@localhost:27017/database_name"
client = motor.MotorClient(normal_uri).open_sync()

Asynchronous Authentication

Use the non-blocking authenticate() to log in after starting the IOLoop:

client = motor.MotorClient('localhost', 27017).open_sync()

@gen.coroutine
def login(c):
    yield c.my_database.authenticate("user", "pass")

After you’ve logged in to a database with a given MotorClient or MotorReplicaSetClient, all further operations on that database using that client will already be authenticated until you call logout().

Table Of Contents

Previous topic

Motor Tailable Cursor Example

Next topic

Changelog

This Page