5-5.MySQLモニタの実行結果をファイルに出力する
MySQLモニタでデータベースを操作した経過をファイルに出力したいといった事があると思います。
コマンド画面をマウスでドラッグしてテキストエディタに貼り付けて保存するのもありですが、この場合に便利なコマンドがあるのでご紹介します。
1.【方法1】リダイレクションで一括実行、一括出力
こちらの記事で紹介した「書き方3」で「MySQLモニタ」でSQLファイルを一括処理した結果をファイルにリダイレクションします。
mysqlモニタの起動時にリダイレクションを指定した場合は画面上に実行結果が表示されませんので注意してください。
【記述方法】
コマンドプロンプトより実行します
mysql -u ユーザー名 -p < SQL記述ファイル名 > 実行結果出力ファイル名
【実行結果】
c:\TEMP>mysql -u root -p < c:\temp\sample01.sql > c:\temp\sample01.log Enter password: ************* c:\TEMP>
【ファイル内容】
c:\TEMP>type c:\temp\sample01.log now() 2019-05-11 13:42:12 user host ROLE01 % SCOTT localhost USER01 localhost USER02 localhost mysql.infoschema localhost mysql.session localhost mysql.sys localhost root localhost c:\TEMP>
スポンサードサーチ
2.操作の経過をファイルに残す。
Oracleを使っている方にはお馴染みのやり方です。
Oracleのsqlplusの「spool」コマンドに該当するコマンドがmysqlモニタにも存在します。
コマンドは「tee」です。
記録開始が「tee」で記録終了が「notee」です。
「tee」から「notee」までの間の操作(画面表示内容)がファイルにも出力されます。
【記述方法】
mysqlモニタのセッション内で実行します。
・記録開始
tee ファイル名
・記録終了
notee
【実行結果】
c:\TEMP>mysql -u root -p 【mysqlモニタのセッション開始】 Enter password: ************* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 87 Server version: 8.0.15 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | studydb | | sys | | testdb | | world | +--------------------+ 8 rows in set (0.16 sec) mysql> tee c:\temp\operation.log 【teeコマンド実行。】 Logging to file 'c:\temp\operation.log' mysql> select user(); 【ここから記録開始】 +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> source c:\temp\sample01.sql +---------------------+ | now() | +---------------------+ | 2019-05-11 13:48:06 | +---------------------+ 1 row in set (0.00 sec) +------------------+-----------+ | user | host | +------------------+-----------+ | ROLE01 | % | | SCOTT | localhost | | USER01 | localhost | | USER02 | localhost | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 8 rows in set (0.00 sec) mysql> notee 【noteeコマンド実行。記録はここまで】 Outfile disabled. mysql> exit Bye c:\TEMP>
【ファイル内容】
c:\TEMP>type c:\temp\operation.log mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> source c:\temp\sample01.sql +---------------------+ | now() | +---------------------+ | 2019-05-11 13:48:06 | +---------------------+ 1 row in set (0.00 sec) +------------------+-----------+ | user | host | +------------------+-----------+ | ROLE01 | % | | SCOTT | localhost | | USER01 | localhost | | USER02 | localhost | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 8 rows in set (0.00 sec) mysql> notee c:\TEMP>
スポンサードサーチ
3.おわりに
このように実行結果をファイルに残すことで作業の実行記録をエビデンスとして残したり、バッチ実行した結果をログとして残すことが出来ます。
是非活用してみてください。


