LoginSignup
31
26

More than 5 years have passed since last update.

MySQLのコマンドラインで個人的に使用頻度が高いコマンドや小ネタ

Posted at

 

普段MySQLを使っている時に、使用頻度の高いコマンドや小ネタを厳選してまとめました。

systemコマンド

 
MySQLを開きながら、CPU使用率やディスクI/Oの状態を見てリソースへの負荷とかを知りたい時は、このコマンド。OSシェルで実行できるコマンドをMySQLシェルから実行できます。
 

mysql> system sar -b

00時00分01秒       tps      rtps      wtps   bread/s   bwrtn/s
00時10分01秒      0.12      0.00      0.12      0.00      1.31
00時20分01秒      0.11      0.00      0.11      0.00      1.09
00時30分01秒      0.11      0.00      0.11      0.00      1.00
00時40分01秒      0.11      0.00      0.11      0.00      1.03
00時50分01秒      0.10      0.00      0.10      0.00      0.96
01時00分01秒      0.10      0.00      0.10      0.00      0.96
01時10分01秒      0.10      0.00      0.10      0.00      0.99
01時20分01秒      0.10      0.00      0.10      0.00      0.96
01時30分01秒      0.10      0.00      0.10      0.00      0.93
01時40分01秒      0.12      0.00      0.12      0.00      1.05
01時50分01秒      0.10      0.00      0.10      0.00      0.89
02時00分01秒      0.11      0.00      0.11      0.00      1.11
mysql> system uptime
 15:11:20 up 17 days, 23:36,  2 users,  load average: 0.00, 0.00, 0.00

 

pager(¥p)コマンド

 
MySQL上でページャをlessにできるので、実行結果が長ければ長い程使えるコマンド。それだけではなく、grepなどのコマンドも使えるので、使い方次第で魔法のコマンドになります。ちなみにlessの -S オプションをつけると、カラムが多くて横に広がってしまう結果も横スクロールできるようになります。便利過ぎる。
 

mysql> pager less;
PAGER set to 'less'
mysql> pager less | grep innodb_read
PAGER set to 'less | grep innodb_read'
mysql> show global variables\g
| innodb_read_ahead_threshold                              | 56                                                                                                                                                                                                                                                                                                                                               |
| innodb_read_io_threads                                   | 4                                                                                                                                                                                                                                                                                                                                                |
| innodb_read_only                                         | OFF                                                                                                                                                                                                                                                                                                                                              |
447 rows in set (0.00 sec)

mysql>

 
標準出力に戻す時は、nopagerコマンド。
 

mysql> nopager;
PAGER set to stdout

teeコマンド

 
実行結果をファイルに保存しておきたい時はteeコマンド。ファイルへの書き出しを止める時はnoteeコマンド。ファイルにはnoteeするまで全部追記されていくので、例えばInnoDBの状態やテーブル情報を残したい時に重宝します。
 

mysql> tee test.txt
Logging to file 'test.txt'
mysql> show global variables like 'innodb_buffer%';
+-------------------------------------+----------------+
| Variable_name                       | Value          |
+-------------------------------------+----------------+
| innodb_buffer_pool_dump_at_shutdown | OFF            |
| innodb_buffer_pool_dump_now         | OFF            |
| innodb_buffer_pool_dump_pct         | 100            |
| innodb_buffer_pool_filename         | ib_buffer_pool |
| innodb_buffer_pool_instances        | 1              |
| innodb_buffer_pool_load_abort       | OFF            |
| innodb_buffer_pool_load_at_startup  | OFF            |
| innodb_buffer_pool_load_now         | OFF            |
| innodb_buffer_pool_size             | 5242880        |
+-------------------------------------+----------------+
9 rows in set (0.00 sec)

mysql> notee
Outfile disabled.
mysql> \q
Bye
$ cat test.txt
mysql> show global variables like 'innodb_buffer%';
+-------------------------------------+----------------+
| Variable_name                       | Value          |
+-------------------------------------+----------------+
| innodb_buffer_pool_dump_at_shutdown | OFF            |
| innodb_buffer_pool_dump_now         | OFF            |
| innodb_buffer_pool_dump_pct         | 100            |
| innodb_buffer_pool_filename         | ib_buffer_pool |
| innodb_buffer_pool_instances        | 1              |
| innodb_buffer_pool_load_abort       | OFF            |
| innodb_buffer_pool_load_at_startup  | OFF            |
| innodb_buffer_pool_load_now         | OFF            |
| innodb_buffer_pool_size             | 5242880        |
+-------------------------------------+----------------+
9 rows in set (0.00 sec)

mysql> notee
$

 

ctrl + rの恩恵

 
このショートカットキーで、インクリメンタルサーチが発動します。
これによって、文字を入力すると、その度に結果を絞り込みながらコマンド実行履歴を
検索することができるので、かなり作業効率が上がります。
 

31
26
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
31
26