LoginSignup
1
1

More than 3 years have passed since last update.

[メモ] MongoDBで擬似的にエラーを発生させたい時のコマンド

Posted at

やりたいこと

DBから何かエラーが返ってきた時のエラーハンドリングが正しく動作するか試すためにMongoDBからエラーが返ってくるようにしたい

failCommandを使う

詳しくはここを読む。
The "failCommand" fail point

mongod 4.0 and mongos 4.1.5.

以上でないと下記のコマンドは使えません。

まず、このコマンドを使うには enableTestCommands を有効化しないといけないです。
/etc/mongod.conf に以下を追加して再起動してください。

setParameter:
  enableTestCommands: 1

一時的に使用したいだけであれば以下のコマンドでmongodを起動してもOKです。

mongod --setParameter enableTestCommands=1

上記が設定できたらmongo shellにログインしてコマンドを実行します。

db.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: 2, failCommands: ["find"]}
});

上記の例の場合、 find コマンドが送信された時は必ず( alwaysOn )エラーコード2を返す様に設定します。
modeに {times: N} をセットすればN回失敗、 {activationProbability: D} (Dは0~1)を設定すれば確率で失敗させられます。
エラーではなく接続を切断したい場合は以下の様に closeConnection: true を指定すればできます。

db.adminCommand({
     configureFailPoint: "failCommand",
     mode: {times: 3},
     data: {closeConnection: true, failCommands: ["find"]}
});

それ以外にも色々なオプションがあるので詳しくはWiki参照。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1