hereのようにMongoDBにauthを設定しました。最初は、私のプロジェクトで私は、単一のデータベースにアクセスしたマングースからfirstdb
がusername
とmypassword
を持つユーザーがfirstdb
それをreadWrite
パーマを与えること自体に作成されたmongooseを使用して複数のデータベースに接続中に認証します
let url = "mongodb://localhost:27017/firstdb";
let options = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username1",
pass:"mypassword1"
};
mongoose.connect(url,options,callback);
を使用して、と言います。私は管理ユーザーと一緒にログインしていました。
スムーズに動作していました。次に、2番目のデータベースに接続する必要がありました。だから私は、このような
let url1 = "mongodb://localhost:27017/firstdb";
let options1 = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username1",
pass:"mypassword1",
auth:{
authdb:"firstdb"
}
};
let connection1 = mongoose.createConnection(url1,options1);
let url2 = "mongodb://localhost:27017/seconddb";
let options2 = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username2",
pass:"mypassword2",
auth:{
authdb:"seconddb"
}
};
let connection2 = mongoose.createConnection(url2,options2);
私は、ユーザーusername2
seconddb
データベースで同じように作成したこの時間として私のコードを変更しました。しかし今では、マングースは何も操作することができず、Not authorized to execute command
で失敗しています。私はmongoシェルを介してDBにアクセスすることができます。私はmongodb authを有効にしていないローカルシステムのコードをスピンアップし、そこではうまく動作します。助けてください