LoginSignup
3
2

More than 5 years have passed since last update.

bashでMySQL操作を行う #エクスペクト・パトローナム編

Last updated at Posted at 2016-05-28


引用: dailynewsagency.com

概要

  • やりたいこと
    • 下記の内容を自動化したい
      • 複数のDBに対してSQLクエリを発行
        • 実行結果の標準出力

アプローチ

  • bashでMySQLコマンドを実行し、終了ステータスコードを取得
    • 終了ステータスコードが0以外はそこで終了
    • 終了ステータスコードが0の場合は処理続行

結果

  • エクスペクト・パトローナム!!
#!/bin/bash

command=(
"mysql -u 'user' '-ppassword' -h 'rds-host01.com' 'database01' < 'sample.sql'"
"mysql -u 'user' '-ppassword' -h 'rds-host02.com' 'database02' < 'sample.sql'"
"mysql -u 'user' '-ppassword' -h 'rds-host01.com' 'database03' < 'sample.sql'"
"mysql -u 'user' '-ppassword' -h 'rds-host02.com' 'database04' < 'sample.sql'"
"mysql -u 'user' '-ppassword' -h 'rds-host01.com' 'database05' < 'sample.sql'"
)

for i in "${command[@]}"
do
  eval ${i}
  rc=$?
  if [ $rc -ne 0 ]; then
    echo "error =======> ######## $i ###########"
    exit 1
  fi
done

echo ""
echo "##########  success  ##########"
echo ""
3
2
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
3
2