目次
とある日
先日MySQL リリースノートでわいわい言う勉強会 8.0.28に参加してきました。
ただ、半分しか参加できなかったのでTwitterでハッシュタグ「#mysql_jp #mysql8028
」で色々とさかのぼって見つけました。
Murakami KoheiさんGJ。プロンプト指定時に \T を使うと、トランザクションの中にいることがわかるようになる。シンプルなのに便利で、今まで気づかなかったのが悔しく思うくらいの機能! #mysql_jp #mysql8028 pic.twitter.com/sfC1qSMmpG
— 坂井 恵(SAKAI Kei) (@sakaik) 2022年2月22日
初めて知ったので調べてやってみると意外と他にもできることがあるのを知ったので記録としてまとめます。
mysql プロンプトとは?
mysql プロンプトを指定の文字列に再構成します。
プロンプトはMySQLにCLIでログインした際に表示される下記のmysql>
の部分です。
ここを色々とカスタマイズできる話です。
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> select * from dual;
プロンプトカスタマイズ
設定できる一覧です。
MySQL :: MySQL 8.0 リファレンスマニュアル :: 4.5.1.2 mysql クライアントコマンド
基本的にこれ以外の文字はそのまま表示されます。
気になったやつとかを少し紹介します。
オプション | 説明 |
---|---|
\C |
現在の接続識別子 |
\c |
ステートメントを発行するたびにインクリメントするカウンタ |
\D |
現在の日付 (フルで) |
\d |
デフォルトデータベース |
\h |
サーバーホスト |
\l |
現在の区切り文字 |
\m |
現在の時間の分 |
\n |
改行文字 |
\O |
3 文字の形式の現在の月 (Jan、Feb、…) |
\o |
数字形式の現在の月 |
\P |
am/pm |
\p |
現在の TCP/IP ポートまたはソケットファイル |
\R |
現在の時間、24 時間表記 (0-23) |
\r |
現在の時間、12 時間表記 (1-12) |
\S |
セミコロン |
\s |
現在の時間の秒 |
\t |
タブ文字 |
\U |
完全な * user_name*@* host_name* アカウント名 |
\u |
ユーザー名 |
\v |
サーバーバージョン |
\w |
3 文字の形式の現在の曜日 (Mon, Tue, …) |
\Y |
現在の年 (4 桁) |
\y |
現在の年 (2 桁) |
\_ |
スペース |
\ |
スペース (バックスラッシュのあとにスペースがあります) |
\' |
単一引用符 |
" |
二重引用符 |
\\ |
リテラルの \ バックスラッシュ文字 |
\* x* |
x (上記にないすべての 「x 」) |
トランザクション(\T)
先にもとのツイート内容であったトランザクション中がわかるカスタマイズをしてみたいと思います。
一点だけ注意点です。
このトランザクションのカスタマイズは8.0.28にしか適用されていないようです。
現時点(2022年2月25日)でドキュメントを見ても記載がないです。
8.0.27で設定を行うとただの文字列として表示されます。
mysql> prompt mysql\T> PROMPT set to 'mysql\T>' mysqlT>>start transaction; Query OK, 0 rows affected (0.00 sec) mysqlT>>select version(); +-----------+ | version() | +-----------+ | 8.0.27 | +-----------+ 1 row in set (0.00 sec)
↓8.0.28でやってみる。
\T
以外の文字列は視覚的に見やすいように付け加えてます。
mysql> prompt mysql\T> PROMPT set to 'mysql\T>' mysql>start transaction; Query OK, 0 rows affected (0.00 sec) mysql*>commit; Query OK, 0 rows affected (0.00 sec) mysql>
トランザクションの間だけ「*」がつくのでわかりやすい。
ついでにautocommit切った状態でもやってみた。
mysql>SELECT @@autocommit; +--------------+ | @@autocommit | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) mysql>SET autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql>SELECT @@autocommit; +--------------+ | @@autocommit | +--------------+ | 0 | +--------------+ 1 row in set (0.00 sec) mysql>select * from test; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.01 sec) mysql*>commit; Query OK, 0 rows affected (0.00 sec) mysql>
デフォルトデータベース(\d)
現在接続しているデータベースが確認できます。
mysql> prompt \d> PROMPT set to '\d>' test_database>use mysql 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>show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_database | +--------------------+ 5 rows in set (0.00 sec) mysql>use sys 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 sys>
MariaDBと同じような見た目にできますね。
データベースにつないでないとこんな感じ。
mysql> prompt \d> PROMPT set to '\d>' (none)>
完全なアカウント名(\U)
接続ユーザが確認できます。
mysql> prompt \U> PROMPT set to '\U>' root@localhost>
俺的おすすめカスタマイズ
色々と試してこれがいいなと思ったカスタマイズをご紹介します。
- トランザクション(\T)
- 完全なアカウント名(\U)
- デフォルトデータベース(\d)
- データベースバージョン(\v)
mysql> prompt (\U) [\d] \v \T> PROMPT set to '(\U) [\d] \v \T>' (root@localhost) [(none)] 8.0.28 >use test_database; 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 (root@localhost) [test_database] 8.0.28 >
my.cnfにも設定することで接続時からプロンプトをカスタマイズできます。
エスケープが必要なので忘れるとエラーになります。
[mysql] prompt="(\\U) [\\d] \\v \\T> "
root@e702e468e759:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.28 MySQL Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. 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. (root@localhost) [(none)] 8.0.28 >
〆
今後も設定できる項目が増えそうですね。
バージョンとかは記事書く際にわざわざ記述しなくてもいいかなと思ってつけました。
ただ、一般向けではないので初めて見た人は困惑しそうですね。