一:檢視服務所有變量
MySQL伺服器維護許多配置其操作的系統變量。每個系統變量都有一個預設值。可以使用指令行或選項檔案中的選項在伺服器啟動時設定系統變量。其中大多數都可以在運作時使用動态更改
SET
語句,這使您可以修改伺服器的操作,而無需停止并重新啟動它。您還可以在表達式中使用系統變量值。
mysqld --verbose --help
[root@qywxdb /]# mysqld --verbose --help
mysqld Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL))
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Starts the MySQL database server.
Usage: mysqld [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
The following groups are read: mysqld server mysqld-5.7
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.
View Code
備注:該指令可檢視伺服器版本,和讀取配置檔案順序
二:設定變量
SET variable = expr [, variable = expr] ...
variable: { user_var_name | param_name | local_var_name | {GLOBAL | @@GLOBAL.} system_var_name | [SESSION | @@SESSION. | @@] system_var_name }
有些情況時我們需要修改伺服器變量的預設值,例如我們修改sql_mode的值(已支援不嚴格的group by),此時我們可以使用set語句來修改系統變量。同時我們可以使用set定義一些其他的變量(使用者變量,該變量是session級别的)。
2.1 設定全局變量
SET GLOBAL max_connections = 1000;
SET @@GLOBAL.max_connections = 1000;
2.2 設定session變量
SET SESSION sql_mode = 'TRADITIONAL';
SET LOCAL sql_mode = 'TRADITIONAL';
SET @@SESSION.sql_mode = 'TRADITIONAL';
SET @@LOCAL.sql_mode = 'TRADITIONAL';
SET @@sql_mode = 'TRADITIONAL';
SET sql_mode = 'TRADITIONAL';
2.3 定義新變量
mysql> set @name='woshiwo'; //新增環境變量
mysql> select @name; //查詢定義的環境變量
| @name |
| woshiwo |
三 :檢視變量(show)
SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern' | WHERE expr]
* 伺服器變量維護在
GLOBAL_VARIABLES
與
SESSION_VARIABLES
表。//不可直接檢視
* 如果不存在修飾符,則預設為
SESSION
。
* 使用
GLOBAL
修飾符,語句顯示全局系統變量值。這些是用于初始化與MySQL的新連接配接的相應會話變量的值。如果變量沒有全局值,則不顯示任何值。
* 使用
SESSION
修飾符,語句将顯示對目前連接配接有效的系統變量值。如果變量沒有會話值,則顯示全局值。
LOCAL
是...的同義詞
SESSION
。
3.1 通過like查詢
SHOW VARIABLES LIKE 'max_join_size';
SHOW VARIABLES LIKE '%size%'; //模糊查詢
SHOW SESSION VARIABLES LIKE 'max_join_size';
SHOW GLOBAL VARIABLES LIKE '%size%';
3.2 通過where 查詢
show variables where Variable_name like 'log%' and value='ON';