タイトルで言いたいこと全部言いましたが、MongoDB のバックアップとかで mongodump
コマンドを使った時に、unauthorized
などというエラーが出て dump できない場合があります。
ユーザーとパスワードを設定して auth true
で動かしている場合。
% mongodump -uUSERNAME -pPASSWORD -d DBNAME
connected to: 127.0.0.1
Thu Jan 29 11:23:44.208 DATABASE: DBNAME to dump/DBNAME
Thu Jan 29 11:23:44.209 DBNAME.system.indexes to dump/DBNAME/system.indexes.bson
Thu Jan 29 11:23:44.209 14 objects
Thu Jan 29 11:23:44.209 DBNAME.system.users to dump/DBNAME/system.users.bson
assertion: 11010 count fails:{ ok: 0.0, errmsg: "unauthorized" }
ユーザーとパスワードあってるんはずなんだけどなんで?となりますよね。
mongodump
コマンド実行時に、 authenticationDatabase
オプションで、認証用のDBを指定してあげる感じですかね...
% mongodump -uUSERNAME -pPASSWORD --authenticationDatabase admin -d DBNAME
connected to: 127.0.0.1
Thu Jan 29 11:30:44.791 DATABASE: DBNAME to dump/DBNAME
Thu Jan 29 11:30:44.791 DBNAME.system.indexes to dump/DBNAME/xxx
Thu Jan 29 11:30:44.792 14 objects
Thu Jan 29 11:30:44.792 DBNAME.system.users to dump/DBNAME/xxx
Thu Jan 29 11:30:44.793 1 objects
Thu Jan 29 11:30:44.793 Metadata for DBNAME.system.users to dump/DBNAME/xxx
Thu Jan 29 11:30:44.793 DBNAME.pages to dump/DBNAME/xxx
Thu Jan 29 11:30:45.078 8511 objects
Thu Jan 29 11:30:45.080 Metadata for DBNAME.pages to dump/DBNAME/xxx
Thu Jan 29 11:30:45.080 DBNAME.revisions to dump/DBNAME/xxx
Thu Jan 29 11:30:46.116 42477 objects
Thu Jan 29 11:30:46.382 Metadata for DBNAME.revisions to dump/DBNAME/xxx
Thu Jan 29 11:30:46.387 DBNAME.users to dump/DBNAME/xxx
Thu Jan 29 11:30:46.389 52 objects
Thu Jan 29 11:30:46.390 Metadata for DBNAME.users to dump/DBNAME/xxx
Thu Jan 29 11:30:46.390 DBNAME.configs to dump/DBNAME/xxx
Thu Jan 29 11:30:46.393 13 objects
Thu Jan 29 11:30:46.393 Metadata for DBNAME.configs to dump/DBNAME/xxx
Thu Jan 29 11:30:46.394 DBNAME.bookmarks to dump/DBNAME/xxx
Thu Jan 29 11:30:46.394 31 objects
Thu Jan 29 11:30:46.394 Metadata for DBNAME.bookmarks to dump/DBNAME/xxx
あまりソースはないですが公式ドキュメントは以下。しなかった場合、のことしか書いてなくてあまり役には立ちません:
--authenticationDatabase
If you do not specify an authentication database, mongodump assumes that the database specified to export holds the user’s credentials.