VPS(centOS)にMongoDBとClowd9を導入しpythonで接続したいのですが、
MongoDBにユーザー認証の設定をしているためか、下記コードだけでは動きません。
ユーザー認証設定をoffにすれば動くようです。
ユーザー認証のコードを追記する必要があると思われますがどのように書いたらよいでしょうか。
python
1import pymongo 2from pymongo import MongoClient 3 4mongo = pymongo.MongoClient() 5mongo["test"]["test"].insert_one({"foo": "bar"}) 6for doc in mongo["test"]["test"].find(): 7 print(doc)
エラー内容
raise OperationFailure(errmsg, code, response, max_wire_version) pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
MongoDBユーザー認証設定でやったこと
root権限を与えたユーザーを作成
db.createUser({user:"user_name", pwd:"user_pass", roles:["root"]})
mongod.confの修正
net: port: 27017 bindIp: 0.0.0.0 security: authorization: enabled
設定ファイル変更の反映
sudo systemctl restart mongod
MongoDBは正しく起動し、ユーザー認証も機能していることを確認しております。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。