天天看點

使用mysqladmin ext了解MySQL運作狀态【轉】

摘要:

      mysqladmin是 MySQL一個重要的用戶端,最常見的是使用它來關閉資料庫,除此,該指令還可以了解MySQL運作狀态、程序資訊、程序殺死等。本文介紹一下如何使用 mysqladmin extended-status(因為沒有"歧義",是以可以使用ext代替)了解MySQL的運作狀态。

1. 使用-r/-i參數

      使用mysqladmin extended-status指令可以獲得所有MySQL性能名額,即show global status的輸出,不過,因為多數這些名額都是累計值,如果想了解目前的狀态,則需要進行一次內插補點計算,這就是mysqladmin extended-status的一個額外功能,非常實用。預設的,使用extended-status,看到也是累計值,但是,加上參數 -r(--relative),就可以看到各個名額的內插補點,配合參數-i(--sleep)就可以指定重新整理的頻率,那麼就有如下指令:

mysqladmin --no-defaults -uroot -r -i 1 -p123456 extended-status

+---------------------------------------+----------+
| Variable_name                         | Value    |
+---------------------------------------+----------+
| Aborted_clients                       | 0        |
| Aborted_connects                      | 0        |
| Binlog_cache_disk_use                 | 0        |
| Binlog_cache_use                      | 0        |
| Bytes_received                        | 35       |
| Bytes_sent                            | 7372     |
……………………
| Threads_running                       | 0        |
| Uptime                                | 1        |
| Uptime_since_flush_status             | 1        |
+---------------------------------------+----------+      

2. 配合grep使用

配合grep使用,我們就有:

mysqladmin --no-defaults -uroot -p123456 -r -i 1 extended-status |grep "Questions\|Queries\|Innodb_rows\|Com_select \|Com_insert \|Com_update \|Com_delete "

| Com_delete                            | 0        |
| Com_insert                            | 0        |
| Com_select                            | 0        |
| Com_update                            | 0        |
| Innodb_rows_deleted                   | 0        |
| Innodb_rows_inserted                  | 0        |
| Innodb_rows_read                      | 0        |
| Innodb_rows_updated                   | 0        |
| Queries                               | 113      |
| Questions                             | 113      |      

3. 配合簡單的awk使用

使用awk,同時輸出時間資訊:

mysqladmin --no-defaults -uroot -p123456 -P3306 -r -i 1 ext |\
awk -F"|" '{\
  if($2 ~ /Variable_name/){\
    print " <-------------    "  strftime("%H:%M:%S") "    ------------->";\
  }\
  if($2 ~ /Questions|Queries|Innodb_rows|Com_select |Com_insert |Com_update |Com_delete |Innodb_buffer_pool_read_requests/)\
    print $2 $3;\
}'

 <-------------    11:37:27    ------------->
 Com_delete                             0        
 Com_insert                             0        
 Com_select                             0        
 Com_update                             0        
 Innodb_buffer_pool_read_requests       6549     
 Innodb_rows_deleted                    0        
 Innodb_rows_inserted                   0        
 Innodb_rows_read                       0        
 Innodb_rows_updated                    0        
 Queries                                131      
 Questions                              131      
 <-------------    11:37:28    ------------->
 Com_delete                             0        
 Com_insert                             0        
 Com_select                             0        
 Com_update                             0        
 Innodb_buffer_pool_read_requests       0        
 Innodb_rows_deleted                    0        
 Innodb_rows_inserted                   0        
 Innodb_rows_read                       0        
 Innodb_rows_updated                    0        
 Queries                                1        
 Questions                              1         

4. 配合複雜一點的awk

反正也不簡單了,那就更複雜一點,這樣讓輸出結果更友好點,因為awk不支援動态變量,是以代碼看起來比較複雜:

mysqladmin --no-defaults -uroot -p123456 -r -i 1 ext |\
gawk -F"|" \
"BEGIN{ count=0; }"\
'{ if($2 ~ /Variable_name/ && ++count == 1){\
    print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";\
    print "---Time---|---QPS---|select insert update delete|  read inserted updated deleted|   logical    physical";\
}\
else if ($2 ~ /Queries/){queries=$3;}\
else if ($2 ~ /Com_select /){com_select=$3;}\
else if ($2 ~ /Com_insert /){com_insert=$3;}\
else if ($2 ~ /Com_update /){com_update=$3;}\
else if ($2 ~ /Com_delete /){com_delete=$3;}\
else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}\
else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}\
else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}\
else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}\
else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}\
else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}\
else if ($2 ~ /Uptime / && count >= 2){\
  printf(" %s |%9d",strftime("%H:%M:%S"),queries);\
  printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);\
  printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);\
  printf("|%10d %11d\n",innodb_lor,innodb_phr);\
}}'


----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --
---Time---|---QPS---|select insert update delete|  read inserted updated deleted|   logical    physical
 11:39:51 |        1|     0      0      0      0|     0        0       0       0|         0           0
 11:39:52 |        1|     0      0      0      0|     0        0       0       0|         0           0
 11:39:53 |        1|     0      0      0      0|     0        0       0       0|         0           0
 11:39:54 |        1|     0      0      0      0|     0        0       0       0|         0           0
 11:39:55 |        1|     0      0      0      0|     0        0       0       0|         0           0
 11:39:56 |        1|     0      0      0      0|     0        0       0       0|         0           0
…………
…………      

這裡注意,要是awk是mawk的話,會出現:function strftime never defined ,是以要用gawk。

本文來自:http://www.orczhou.com/index.php/2014/03/some-tricky-about-mysqladmin-extended-status/

~~~~~~~~~~~~~~~

萬物之中,希望至美

~~~~~~~~~~~~~~~