0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQL Shellでコマンドラインからsqlをバッチ実行する

Last updated at Posted at 2017-11-25

用途

UnitTestの初期化、終了時に、コマンドラインからSQLをバッチ実行したい。
UnitTest中は、インターラクティブな入力ができないので、コマンドラインからパスワードも指定したい。
SQLは外部ファイル化したい。

コマンド

--passwords-from-stdinオプションと--password=[password]の両方を指定します。

$ mysqlsh --host=[host_name] --port=[port] --user=[user] --password=[password] --sql --passwords-from-stdin --file=[sql_file_path]

mysqlx: [Warning] Using a password on the command line interface can be insecure.
...
(SQL実行結果)

注意事項

コマンドラインの入力情報は、漏れる可能性があるので、テスト用アカウントを作成して実行すること。

備考

ファイルの指定は、--fileオプションの代わりにリダイレクトでも可能。

$ mysqlsh --host=[host_name] --port=[port] --user=[user] --password=[password] --sql --passwords-from-stdin < [sql_file_path]

コマンドラインに直接SQLを指定する場合は--executeオプションを使う。

$ mysqlsh --host=[host_name] --port=[port] --user=[user] --password=[password] --sql --passwords-from-stdin --execute='[SQL]'

複数行のSQLだったら;で区切って指定する。

$ mysqlsh --host=[host_name] --port=[port] --user=[user] --password=[password] --sql --passwords-from-stdin --execute='[SQL];[SQL];'

SSLで通信したい場合は、Amazonからパブリックキーを取得して(https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem)、--ssl-caオプションを指定します。

$ mysqlsh --host=[host_name] --port=[port] --user=[user] --password=[password] --sql --passwords-from-stdin --ssl-ca=[pem_file_path] --file=[sql_file_path]
0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?