OperationFailure: not authorized on tracking to execute command

Viewed 16652

I did the following

-- `sudo apt-get install mongodb-org`
-- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
-- sudo mkdir /data/db
-- start without auth to create user
    `sudo mongod --port 27017 --dbpath /data/db`
-- open shell with : mongo --port 27017

```
> use admin
> db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )

```

-- Restart with auth required(ctrl+c the above mongod process): 
`sudo mongod --auth --port 27017 --dbpath /data/db'

-- To open shell(ctrl+c above mongo shell): 
`mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`

my mongoengine_settings.py

```PYTHON
from mongoengine import connect

DATABASE = 'tracking'
USERNAME = 'useradmin'
PASSWORD = 'mypassword'
HOST = 'mongodb://localhost/tracking'
PORT = 27017

connect(DATABASE, 
        username=USERNAME, 
        password=PASSWORD,
        host=HOST,
        port=PORT
        )

```

now when i try to bulk insert some data using mongoengine that works fine if i don't have --auth enabled, otherwise it throws the following error:

OperationFailure(u'command SON([(\'createIndexes\', u\'order\'), (\'indexes\', [{\'unique\': True, \'background\': False, \'sparse\': False, \'key\': SON([(\'order_id\', 1)]), \'name\': u\'order_id_1\'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)

what am i doing wrong?

2 Answers

Here's a means of connecting and authenticating with pymongo:

from pymongo import MongoClient

# MongoDB connection info
hostname = '10.20.30.40'
port = 27017
username = 'adminUserName'
password = 'secret'
databaseName = 'someDB'

# connect with authentication
client = MongoClient(hostname, port)
db = client[databaseName]
db.authenticate(username, password)
Related