LAMP 環境チューニングに便利なツール – 2

LAMP 環境チューニングに便利なツール第二弾は MySQL に関してのツールを紹介します。
その名も mytop !!
前回、apachetop をご紹介しましたが、同じように top コマンドライクとなっています。
さて、mytop では何をリアルタイムで確認できるのでしょうか ( ^ω^)ワクワク
とりあえず epel に探しに行きます。

# yum install mytop --eneblerepo=epel

普通にパッケージを発見。おもむろに mytop コマンドを実行。

[root@mytop ~]# mytop
Cannot connect to MySQL server. Please check the:

* database you specified "test" (default is "test")
* username you specified "root" (default is "root")
* password you specified "" (default is "")
* hostname you specified "localhost" (default is "localhost")
* port you specified "3306" (default is 3306)
* socket you specified "" (default is "")

The options my be specified on the command-line or in a ~/.mytop
config file. See the manual (perldoc mytop) for details.

Here's the exact error from DBI. It might help you debug:

Access denied for user 'root'@'localhost' (using password: NO)

[root@mytop ~]#

はい、もちろん認証でひっかかりますね。なにやら、設定ファイルを用意すれば認証部分を短縮できそうな感じが出てます。
では man で確認しましょう。

# man mytop

早速気になる記述を発見 ( ^ω^)b

Config File

Instead of always using bulky command-line parameters, you can also use a config file in your home directory ("~/.mytop"). If present, mytop will read it automatically. It is read before any of
your command-line arguments are processed, so your command-line arguments will override directives in the config file.

Here is a sample config file "~/.mytop" which implements the defaults described above.

user=root
pass=
host=localhost
db=test
delay=5
port=3306
socket=
batchmode=0
header=1
color=1
idle=1

Using a config file will help to ensure that your database password isn’t visible to users on the command-line. Just make sure that the permissions on "~/.mytop" are such that others cannot read
it (unless you want them to, of course).

You may have white space on either side of the "=" in lines of the config file.

これはログイン情報の入力を短縮してくれそう ( ^ω^)b
home ディレクトリに .mytop という設定ファイルを置けばアカウント情報、パスワードなど固定できるようです。
(かつパスワードがログに残るのを防ぎます。)
まずは簡単に以下を入力して保存。

# vim /root/.mytop

user=root
pass=xxxxxxxxx
host=localhost

パスワードの記載をするので、ファイル権限を変えておきましょう。

# chmod 600 /root/.mytop

そのほかの項目はデフォルト値があると信じて、ユーザー名、パスワード、接続先のみ記載して、いざ mytop !!

# mytop

 

MySQL on localhost (5.5.19-log) up 0+05:36:49 [19:18:58]
Queries: 1.8M qps: 91 Slow: 0.0 Se/In/Up/De(%): 81/01/00/00
qps now: 69 Slow qps: 0.0 Threads: 11 ( 1/ 3) 77/01/01/00
Cache Hits: 950.6k Hits/s: 48.2 Hits now: 29.6 Ratio: 65.1% Ratio now: 55.8%
Key Efficiency: 98.4% Bps in/out: 19.5k/443.7k Now in/out: 18.0k/29.7k

Id User Host/IP DB Time Cmd Query or State
-- ---- ------- -- ---- --- ----------
78886 root localhost test 0 Query show full processlist
78929 mytop.user ec2-xxx mytop_db 1 Sleep
78930 mytop.user ec2-xxx mytop_db 1 Sleep
78933 mytop.user ec2-xxx mytop_db 1 Sleep
78934 mytop.user ec2-xxx mytop_db 1 Sleep
78926 mytop.user ec2-xxx mytop_db 1 Sleep

お~!!クエリが丸見えです。右上に show full processlist と記載がありますね。
数値の詳細は下記の通り。様々な統計情報がリアルタイムで計測できます。

 

Queries: mysqld起動時からの総クエリ数
qps: mysqld起動時からの平均秒間クエリ数( Queries Per Second )
Slow: mysqld起動時からの総スロークエリ数
Se/In/Up/De(%): 各クエリの比率( Alter文やCreate文なども含めたパーセンテージのため、合計は100%になりません )
Se: Select文
In: Insert文
Up: Update文
De: Delete文
qps now: 観測時のqps *2
Slow qps: 観測時のスロークエリのqps
Threads: 実行中のスレッド数 [アクティブスレッド数] / [総スレッドキャッシュ数] Key Efficiency: key_buffer(myISAM)のヒット率 *3
Bps in/out: データ転送速度(累積平均)[Bytes/sec] Now in/out: データ転送量(観測時)[bits/sec]

 

また、コマンド実行中に表示の切り替えや、表示対象のフィルタリングも可能です。

– 制御系

?: ヘルプ情報を表示します
k: スレッドを停止します ( これ怖いですね。。)
p: 表示の一時停止をします
q: mytop コマンドを終了します
r: 一部統計情報のリセット( Queries など一部の情報はリセットされない )
s: 更新の間隔を設定します ( 秒単位 デフォルト 5秒 )

– フィルター系

d: 特定データベースへの接続のみ表示します
h: 特定ホストからの接続のみ表示します
i: 実行中のスレッドのみ表示します
o: 実行時間の多い順に表示します( もう一度 o コマンド発行すると元に戻ります )
u: 特定ユーザのスレッドのみ表示します
F: フィルタ設定を解除します

– 表示モード切替え

c: mysql コマンドの累積実行比率を表示します
m: qps 情報のみを表示します
t: デフォルトモードに戻します
H: ヘッダー部分を非表示にします
I: InnoDB の統計情報を表示します

– パフォーマンスチューニング時に使えそう

e: クエリの Explain 結果を表示します
f: 特定のスレッドにて実行されるクエリを全文で表示します


MySQL を運用するにあたり、様々な場面で使えそうなコマンドですが、注意点もあります。

・負荷状況によっては、mytop の使用は NG
・サービス影響を与える制御系コマンドもある
以上を踏まえた点で、運用に活かしていきたいですね。
簡単でしたが、mytop の紹介でした。

投稿者プロフィール

Rito
Rito
AWS認定12冠
趣味 : スプラトゥーン

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Time limit is exhausted. Please reload CAPTCHA.