MySQL コマンドの履歴と結果をファイルに記述する

  • 作成日 2022.03.29
  • 更新日 2022.10.17
  • mysql
MySQL コマンドの履歴と結果をファイルに記述する

MySQLで、コマンドの履歴と結果をファイルに記述する手順を記述してます。

環境

  • OS ubuntu21.10
  • MySQL Ver 8.0.27-0ubuntu0.21.10.1 for Linux on x86_64 (Ubuntu)

手順

コマンドの履歴と結果をファイルに記述するには、「tee」を使用します。

tee ファイル名

実際に、使用してみます。

mysql> tee result.txt

実行後に以下のクエリを実行します。

mysql> select * from tbl1;
ERROR 1046 (3D000): No database selected

mysql> use foo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tbl1;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | taro   |   10 |
|  2 | jiro   |   20 |
|  3 | saburo |   50 |
|  4 | jiro   |   20 |
|  5 | taro   |   50 |
+----+--------+------+
5 rows in set (0.00 sec)

ファイルに記述されたかを確認してみます。

mysql> system cat result.txt

mysql> select * from tbl1;
ERROR 1046 (3D000): No database selected

mysql> use foo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tbl1;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | taro   |   10 |
|  2 | jiro   |   20 |
|  3 | saburo |   50 |
|  4 | jiro   |   20 |
|  5 | taro   |   50 |
+----+--------+------+
5 rows in set (0.00 sec)

コマンド上で行った全ての結果が表示されていることが確認できます。

終了する場合は、「notee」を実行します。
※noteeを実行したところまで記録されます。

mysql> notee