Skip to content

pymongo.errors.OperationFailure: TxMongo: not authorized for query on foo.test #287

@ma-pony

Description

@ma-pony

When I connect to MongoDB using my username password, it throws an error not authorized, is there a configuration problem?

env:

docker run --name mongo-4.2.0 -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=username -e MONGO_INITDB_ROOT_PASSWORD=password --restart always -d mongo:4.2.0

Platform macOS-13.3.1-arm64-arm-64bit
Python 3.10.8

MongoDB 4.2.0

twisted==21.7.0
txmongo==23.0.0
pymongo==3.13.0 

code:

from twisted.internet import defer, reactor
from txmongo.connection import ConnectionPool


@defer.inlineCallbacks
def example():
    mongodb_uri = "mongodb://username:password@127.0.0.1:27017/?authMechanism=DEFAULT"

    mongo = yield ConnectionPool(mongodb_uri)

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    # fetch some documents
    docs = yield test.find(limit=10)
    for doc in docs:
        print(doc)


if __name__ == '__main__':
    example().addCallback(lambda ign: reactor.stop())
    example().addErrback(lambda ign: reactor.stop())
    reactor.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions