文系プログラマによるTIPSブログ

文系プログラマ脳の私が開発現場で学んだ事やプログラミングのTIPSをまとめています。

噂のMySQLクライアント「mycli」の挙動をGIFアニメと静止画で確認してみた

最近mycliというMySQLクライアントが流行りつつあるので、GIFアニメと静止画で挙動を確認してみようと思います。

f:id:treeapps:20150806012417p:plain

mycli

通常のログイン

標準のMySQLクライアント

パスワード有りのrootユーザでログインする場合は以下のように実行します。

[vagrant@node2 tmp]$ mysql -uroot -ppassword test
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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>

mycli

mysqlのように「-pパスワード」の構文が使えず、「--pass パスワード」となります。
f:id:treeapps:20150805224649p:plain

パスワードの指定の仕方が異なるので、単純にaliasコマンドで「mysql」を「mycli」に置き換える事はできません。

--defaults-extra-fileを使ったログイン

mysql5.6になって、コマンドラインからパスワードを入力しようとすると以下のように警告が表示されます。
f:id:treeapps:20150805224948p:plain
(mariadbではこの警告は表示されません)

これを解決するために--defaults-extra-fileを使う方法があります。

標準のMySQLクライアント

mysqlクライアントの場合は以下のように指定できます。

[vagrant@node1 ~]$ mysql --defaults-extra-file=/tmp/my.cnf

f:id:treeapps:20150805225203p:plain

mycli

[vagrant@node1 ~]$ mycli --defaults-extra-file=/tmp/my.cnf

f:id:treeapps:20150805225505p:plain
残念ながら --defaults-extra-file オプションには対応していないようです

・・・と思ったら、なんと作者の方から以下のリプライを頂きました。

どうやら私がレビュー記事を書いている時には既に実装されていたようです。
「--default-extra-files」オプションの対応ではなく、「--defaults-group-suffix」を実装して頂いたようです!!

[vagrant@node1 shared]$ mycli --help
Usage: mycli [OPTIONS] [DATABASE]

Options:
  -h, --host TEXT               Host address of the database.
  -P, --port TEXT               Port number to use for connection. Honors
                                $MYSQL_TCP_PORT
  -u, --user TEXT               User name to connect to the database.
  -S, --socket TEXT             The socket file to use for connection.
  -p, --password                Force password prompt.
  --pass TEXT                   Password to connect to the database
  -v, --version                 Version of mycli.
  -D, --database TEXT           Database to use.
  -R, --prompt TEXT             Prompt format (Default: "\t \u@\h:\d> ")
  -l, --logfile FILENAME        Log every query and its results to a file.
  --defaults-group-suffix TEXT  Read config group with the specified suffix. ←これ!!!
  --help                        Show this message and exit.

mycliの--defaults-group-suffixを使ってみる

1, ホームディレクトリに.my.cnfを作成する
cat << EOF > ~/.my.cnf
[clientGROUP1]
user=root
password=password
host=localhost
database=mysql
EOF
chmod 600 ~/.my.cnf

ホームに隠しファイル形式で作成し、パーミッションを600にしておくと、接続情報が漏れにくくセキュアになります。

2, --defaults-group-suffix でログインする
[vagrant@node1 ~]$ mycli --defaults-group-suffix GROUP1
Version: 1.2.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Anonymous
mysql root@localhost:mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
|---------------------------|
| columns_priv              |
| db                        |
・・・略・・・

こんな感じでグループ名を引数に渡すとログインできるようになります!!!.my.cnfのdatabaseで指定したmysqlがちゃんとuseされた状態になっていますね。

引数に指定した「GROUP1」ですが、.my.cnfの以下の部分で指定しています。

[clientGROUP1]

「group-suffix」→「グループのサフィックス」、つまり「client(グループ)」のサフィックス部分である「GROUP1」が、サフィックスに該当します。


なお、以下のように.my.cnfに複数グループを定義する事も可能です。

cat << EOF > ~/.my.cnf
[clientGROUP1]
user=root
password=password
host=localhost
database=mysql

[clientGROUP2]
user=root
password=password
host=localhost
database=information_schema
EOF
chmod 600 ~/.my.cnf

例えば以下のようにGROUP2を指定すると、information_schemaがuseされた状態で接続する事も可能です。

[vagrant@node1 ~]$ mycli --defaults-group-suffix GROUP2
Version: 1.2.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Anonymous
mysql root@localhost:information_schema>

カラム数が多いテーブルのselect

標準のMySQLクライアント

f:id:treeapps:20150805224345p:plain
よく見慣れたこの改行表示。カラム数がもっと多い場合、改行され過ぎて何がなんだかわからなくなりますね。

mycli

mycliの場合は改行されず、横に伸びます。代わりに十字キーで左右に移動する事ができます。
f:id:treeapps:20150805222211g:plain

大量の行をselectする

標準のMySQLクライアント

f:id:treeapps:20150805225911g:plain
これもmysqlユーザには見慣れた光景ですね。凄い勢いでコンソールがダラーっと流れていってしまいます。誤ってlimitを付け忘れてselectした日にはもう・・・

mycli

f:id:treeapps:20150805230253g:plain
標準のmysqlクライアントと違い、コンソールは流れません。代わりにviのような表示になり、vi同様「control + f」や「control + b」で行を前後で、「shift + g」で最終行に、「gg」で先頭に移動できました。

しかし、mycliのselect結果の描画は標準のmysqlクライアントより大分パフォーマンスが劣るので注意が必要です。ちょっとGIFアニメで比較してみましょう。

標準のMySQLクライアントで5万件selectする

f:id:treeapps:20150805231020g:plain
標準のmysqlクライアントの場合は一瞬でselect結果が流れてきます。

mycliで5万件selectする

f:id:treeapps:20150805230744g:plain
一方mycliの場合は大分待たされます。

dumpの出力

標準のMySQLクライアント

f:id:treeapps:20150805231600g:plain
普通にdumpできます。

mycli

mycliにはdumpコマンドはありません。

\Gで縦にselect表示する

標準のMySQLクライアント

f:id:treeapps:20150805234531p:plain
標準mysqlクライアントの場合はカラム名が右揃えという誰得仕様になっています。

mycli

f:id:treeapps:20150805235056p:plain
一方mycliの場合はカラム名は左揃えになり、区切り文字は「:」ではなく「|」になっています。

データ中に改行がある場合

使用するDDLとデータは以下の通りです。

drop table if exists multi;
create table multi (col1 varchar(100), col2 varchar(100))engine=innodb charset=utf8mb4;

insert into multi values ('今日はいいお天気です', '東a京1特許許可局'), ('あ', 'あああaああ\nああああああああ\nあaaaaaaaaaaaあああ');

select * from multi;

標準のMySQLクライアント

f:id:treeapps:20150805235354p:plain
まあ普通にセル内で改行されてこんな表示になりますよね・・

mycli

f:id:treeapps:20150805235457p:plain
残念。mycliでも同じく改行されてこんな感じになります。列幅は改行が考慮された幅になってるっぽいです。

雑感

mycliの場合はシンタックスハイライトされるのがいいですね。オートコンプリートも長いカラム名の場合は結構助かります。

5万件のselectが遅かったですが、通常そんな沢山selectしませんよね。せいぜい1000件くらいにlimitする場合が多いでしょうから、大した問題にはならなそうです。mysqldumpはそもそもシンタックスハイライトとか無いし、標準のmysqldumpだけで問題ありません。

作者の方に「--defaults-group-suffix」オプションを実装して頂いた事で、以下のようにmysqlクライアントとmycliで同じ設定ファイルを使ったログインが可能になります。

~/.my.cnfを作成する

cat << EOF > ~/.my.cnf
[clientGROUP1]
user=root
password=password
host=localhost
database=mysql
EOF
chmod 600 ~/.my.cnf

mycli用接続スクリプトを作成する

cat << EOF > ~/mycli.sh
#!/bin/sh
mycli --defaults-group-suffix \$1
EOF
chmod +x ~/mycli.sh

以下のように使用できます。

[vagrant@node1 ~]$ ./mycli.sh GROUP1
Version: 1.2.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Heath Naylor
mysql root@localhost:mysql>

mysql用接続スクリプトを作成する

cat << EOF > ~/mysql.sh
#!/bin/sh
mysql --defaults-group-suffix=\$1
EOF
chmod +x ~/mysql.sh

以下のように使用できます。

[vagrant@node1 ~]$ ./mysql.sh GROUP1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.6.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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>


やるお「最初作者さんからリプ貰った時、なんかスパム来たなウゼー、って思ったよ」


やらないお「英語でリプ来るとビックリするよな」


やるお「ということで業務で活用させて貰うよ!!」

やらないお「でもお前ニートじゃん?」