MySQL 狀態變數(Server Status Variables)

來源:互聯網
上載者:User

MySQL 狀態變數(Server Status Variables)

MySQL狀態變數是當前伺服器自啟動後累計的一些系統狀態資訊,主要用於評估當前系統資源的使用方式以進一步分析系統效能而做出相應的調整決策。這些狀態變數我們可以理解為等同於Oracle資料庫的動態效能檢視。MySQL的狀態變數有很多,比如SQL執行頻率,索引的使用方式,鎖資源的使用方式等等。狀態變數可以分區全域以及會話層級的狀態變數。狀態變數不可修改,為唯讀屬性,由系統更新。本文示範了狀態變數的一些樣本,僅為拋磚引玉之用。

1、狀態變數
  反映當前mysql資料庫伺服器自當次啟動以來的累計相關狀態資訊,分為會話級與全域層級狀態資訊。
  與系統變數類似,有些狀態變數有全域和會話層級,而有些只有全域層級。如binlog_cache_disk_use僅有全域狀態,而bytes_sent兩者都有。
  可以通過show status like '%variable_name%' 或者show global status like '%variable_name%'來查看。
  在未使用Like的情形下show status會顯示全部的狀態變數。
  可以通過查詢系統資料表information_schema.global_status以及information_schema.session_status來擷取狀態變數資訊。
  可以在命令列下通過mysqladmin extended-status方式來擷取狀態變數的相關資訊。
  可以通過命令列方式mysqladmin extended-status -r -i 5或innotop持續觀察狀態變數的改變情況。
  一些狀態變數重可以用FLUSH STATUS語句重設為零值。

2、show方式查看狀態變數

--當前示範環境
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version      | 5.5.39-log |
+---------------+------------+

a、查看所有狀態變數
root@localhost[(none)]> show status;
+------------------------------------------+-------------+
| Variable_name                            | Value      |
+------------------------------------------+-------------+
| Aborted_clients                          | 0          |
| Binlog_stmt_cache_use                    | 1          |
| Bytes_received                          | 135        |
| Bytes_sent                              | 266        |
|              ................          |            |
| Threads_running                          | 1          |
| Uptime                                  | 76242      |
| Uptime_since_flush_status                | 76242      |
+------------------------------------------+-------------+
312 rows in set (0.00 sec)  --可以看出目前的版本5.5.39有312個狀態變數


b、查看僅有global的狀態變數(connections)           
--查看指定的狀態變數,以下兩個與connection相關的都為全域狀態變數
root@localhost[(none)]> show global status like 'connection%';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Connections          | 11    | --串連到MySQL伺服器的數量(包含成功或失敗的)。
+----------------------+-------+

SUSE11b:~ # mysql -ufred

fred@localhost[(none)]> show global status like '%connection%';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Connections          | 12    | --串連之後,我們看到Connections的值變為12了。
+----------------------+-------+


c、查看既有global又有session狀態的變數
--查看session狀態變數opened_tables
root@localhost[tempdb]> show session status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 0    |
+---------------+-------+

root@localhost[tempdb]> select count(*) from tb_slow;
+----------+
| count(*) |
+----------+
|  424448 |
+----------+

root@localhost[tempdb]> show session status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 1    | --值變為1
+---------------+-------+

--從information_schema.session_status表查詢狀態變數OPENED_TABLES
root@localhost[tempdb]> select * from information_schema.session_status
    -> where variable_name like 'opened_tables';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| OPENED_TABLES | 1              |
+---------------+----------------+

--查看全域狀態變數opened_tables
root@localhost[tempdb]> show global status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 54    |
+---------------+-------+

root@localhost[tempdb]> select count(*) from mysql.db;
+----------+
| count(*) |
+----------+
|        2 |
+----------+

root@localhost[tempdb]> show global status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 55    |  --值變為55
+---------------+-------+

--從information_schema.global_status表查詢狀態變數OPENED_TABLES
root@localhost[tempdb]> select * from information_schema.global_status
    -> where variable_name like 'opened_tables';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| OPENED_TABLES | 55            |
+---------------+----------------+

--清洗狀態變數統計資訊
root@localhost[tempdb]> flush status;
Query OK, 0 rows affected (0.00 sec)

--下面的查詢結果可以看出,session層級的opened_tables被重設為0
root@localhost[tempdb]> show session status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 0    |
+---------------+-------+

--Author: Leshami
--Blog  : http://blog.csdn.net/leshami

--而全域層級的opened_tables未受到任何影響
root@localhost[tempdb]> show global status like 'opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 55    |
+---------------+-------+

3、使用mysqladmin extended-status擷取狀態變數

 

suse11b:~ # mysqladmin extended-status|grep Connections
| Connections                              | 18          |
suse11b:~ # mysql

root@localhost[(none)]> system mysqladmin extended-status|grep Connections
| Connections                              | 20          |
root@localhost[(none)]> exit
Bye
suse11b:~ # mysqladmin --help |more    #mysqladmin與狀態變數有關的使用
extended-status      Gives an extended status message from the server
flush-status          Clear status variables

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二進位安裝

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.