简体   繁体   中英

Authentication problems with pymongo and flask

I am trying to connect with pymongo and flask. I did this:

app.config['MONGO_HOST'] = 'ds02XX.mongolab.com'
app.config['MONGO_PORT'] = '29224'
app.config['MONGO_DBNAME'] = 'myusername'
app.config['MONGO_USERNAME'] = 'myusername'
app.config['MONGO_PASSWORD'] = 'thislongpasswordthatigotfrommongolab'

However, when I call mongo=PyMongo(app) , it simply does not work. and I get this error:

pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'myusername'), ('nonce', u'somenumbers'), ('key', u'othernumbers')]) failed: auth failed

Is there anything I could do to check? I used the same data to connect via mongo shell, and it worked fine!

Thanks,

Francis

For some reason, PyMongo 3.0.3 is not working. They - Mongolab - said that the drivers should be compatible with MongoDB 3.0, and PyMongo 3.0.3 is. I reverted back to PyMongo 2.8 and it works now.

I got the same error message while using: MongoDB 2.6 and PyMongo 3.2. Tried downgrading my PyMongo to versiones 2.7 and 2.8, but was not working. Finally I got to connect successfully by updating my connection string to include the database.

Changed from:

'mongodb://'+ MONGODB[ 'username' ] + ':' + MONGODB[ 'password' ] + '@' + MONGODB['server'] + ':' + MONGODB['port'] + '/'

to

'mongodb://'+ MONGODB[ 'username' ] + ':' + MONGODB[ 'password' ] + '@' + MONGODB['server'] + ':' + MONGODB['port'] + '/' + MONGODB['database']

MongoDB version 2.6 requires the connection strings to include the database if using credentials. If database is not included, then the mongo client will try to authenticate and connect to a default admin database. It is detailed here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM