MySQL系統變量(system variables)實際上是一些系統參數,用於初始化或設定數據庫對系統資源的占用,文件存放位置等等。這些個系統變量可以分為全局以及會話級別層面來修改,有些也可以進行動態修改。本文主要介紹了系統變量的一些概念以及如何設置查看這些系統變量。
1、什麼是系統變量
對於有關涉及到size的設置值,可以使用後綴K、M或G分別表示千字節、兆字節或gigabytes,不區分大小寫。
--當前的版本 mysql> show variables like 'version%'; +-------------------------+------------------------------+ | Variable_name | Value | +-------------------------+------------------------------+ | version | 5.5.37 | | version_comment | MySQL Community Server (GPL) | | version_compile_machine | x86_64 | | version_compile_os | Linux | +-------------------------+------------------------------+ --獲取有關set的幫助 mysql> help set Name: 'SET' Description: Syntax: SET variable_assignment [, variable_assignment] ... variable_assignment: user_var_name = expr | [GLOBAL | SESSION] system_var_name = expr | [@@global. | @@session. | @@]system_var_name = expr --查看全部系統變量 root@localhost[tempdb]> show variables; --該命令會輸出當前系統全部系統變量 --查看sort_buffer mysql> show variables like 'sort_buffer%'; +------------------+---------+ | Variable_name | Value | +------------------+---------+ | sort_buffer_size | 2097152 | +------------------+---------+ --在省略global與session關鍵字的情形下為session級別 mysql> set sort_buffer_size=1024*1024*4; --設置為4M mysql> show variables like 'sort_buffer%'; +------------------+---------+ | Variable_name | Value | +------------------+---------+ | sort_buffer_size | 4194304 | +------------------+---------+ --恢復到缺省值 mysql> set sort_buffer_size=default; mysql> show variables like 'sort_buffer%'; +------------------+---------+ | Variable_name | Value | +------------------+---------+ | sort_buffer_size | 2097152 | +------------------+---------+ 3、全局與會話級別設置示例
--如何設置隔離級別 mysql> help isolation Name: 'ISOLATION' Description: Syntax: SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL { REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED | SERIALIZABLE } --下面我們通過演示隔離級別來設置全局與session級別變量 --查看當前session級別的隔離方式 root@localhost[(none)]> show variables like '%isolation%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ --修改當前session級別的隔離方式為READ-COMMITTED root@localhost[(none)]> set session transaction isolation level read committed; root@localhost[(none)]> show variables like '%isolation%'; +---------------+----------------+ | Variable_name | Value | +---------------+----------------+ | tx_isolation | READ-COMMITTED | +---------------+----------------+ --另外的一個session , 登錄用戶為fred --當前sessioin級別繼承全局隔離級別為REPEATABLE-READ fred@localhost[(none)]> show variables like '%isolation%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ --在root會話中設置全局隔離級別為serializable root@localhost[(none)]> set global transaction isolation level serializable; --注意,在root會話中 session級別還是為READ-COMMITTED root@localhost[(none)]> show variables like '%isolation%'; +---------------+----------------+ | Variable_name | Value | +---------------+----------------+ | tx_isolation | READ-COMMITTED | +---------------+----------------+ --在root會話中我可以看到全局的值已經變為SERIALIZABLE root@localhost[(none)]> show global variables like '%isolation%'; +---------------+--------------+ | Variable_name | Value | +---------------+--------------+ | tx_isolation | SERIALIZABLE | +---------------+--------------+ --在fred中全局的也變成了SERIALIZABLE fred@localhost[(none)]> show global variables like '%isolation%'; +---------------+--------------+ | Variable_name | Value | +---------------+--------------+ | tx_isolation | SERIALIZABLE | +---------------+--------------+ --從上面的演示來說,無論global級別如何設置,不會影響到當前session級別的設置 --下面我們使用一個新用戶登錄來看看全局設置是否影響新會話 robin@SZDB:~> mysql -urobin --如下查詢新會話的隔離級別等於全局的隔離級別 robin@localhost[(none)]> show variables like '%isolation%'; +---------------+--------------+ | Variable_name | Value | +---------------+--------------+ | tx_isolation | SERIALIZABLE | +---------------+--------------+ 4、如何獲取變量值
除了通過前面演示的使用show global|session variables like 'vari_name'方式之外,我們可以通過查詢 information_schema數據中特定的表來獲得這些變量的值。 通過查詢數據information_schema的表global_variables root@localhost[information_schema]> select variable_value from global_variables where -> variable_name='tx_isolation'; +----------------+ | variable_value | +----------------+ | SERIALIZABLE | +----------------+ --Author: Leshami --Blog : http://blog.csdn.net/leshami root@localhost[information_schema]> select @@global.tx_isolation; +-----------------------+ | @@global.tx_isolation | +-----------------------+ | SERIALIZABLE | +-----------------------+ root@localhost[information_schema]> select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | READ-COMMITTED | +------------------------+ --下面查詢session_variables結果與查詢global_variables獲得的值相同,究其原因還在進一步研究中 root@localhost[information_schema]> select * from session_variables where variable_name='tx_isolation'; +---------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +---------------+----------------+ | TX_ISOLATION | SERIALIZABLE | +---------------+----------------+
5、總結
b、檢索設置
c、其他注意事項