SETvariable
=expr
[,variable
=expr
] ...variable
: {user_var_name
|param_name
|local_var_name
| {GLOBAL | @@GLOBAL.}system_var_name
| {PERSIST | @@PERSIST.}system_var_name
| {PERSIST_ONLY | @@PERSIST_ONLY.}system_var_name
| [SESSION | @@SESSION. | @@]system_var_name
}
SET
syntax for variable assignment enables you to assign values to different types of variables that affect the operation of the server or clients:变量分配语法使您能够将值分配给影响服务器或客户端操作的不同类型的变量:
User-defined variables. 用户定义的变量。See Section 9.4, “User-Defined Variables”.请参阅第9.4节,“用户定义的变量”。
Stored procedure and function parameters, and stored program local variables. 存储过程和函数参数,以及存储程序局部变量。See Section 13.6.4, “Variables in Stored Programs”.请参阅第13.6.4节,“存储程序中的变量”。
System variables. 系统变量。See Section 5.1.8, “Server System Variables”. 请参阅第5.1.8节,“服务器系统变量”。System variables also can be set at server startup, as described in Section 5.1.9, “Using System Variables”.系统变量也可以在服务器启动时设置,如第5.1.9节,“使用系统变量”所述。
A 分配变量值的SET
statement that assigns variable values is not written to the binary log, so in replication scenarios it affects only the host on which you execute it. SET
语句不会写入二进制日志,因此在复制场景中,它只影响执行它的主机。To affect all replication hosts, execute the statement on each host.要影响所有复制主机,请在每个主机上执行该语句。
The following sections describe 以下各节介绍设置变量的SET
syntax for setting variables. SET
语法。They use the 它们使用=
assignment operator, but the :=
assignment operator is also permitted for this purpose.=
赋值运算符,但也允许使用:=
赋值运算符。
User-defined variables are created locally within a session and exist only within the context of that session; see Section 9.4, “User-Defined Variables”.用户定义的变量在会话中本地创建,并且仅存在于该会话的上下文中;请参阅第9.4节,“用户定义变量”。
A user-defined variable is written as 用户定义的变量写为@
and is assigned an expression value as follows:var_name
@var_name
,并分配一个表达式值,如下所示:
SET @var_name
=expr
;
Examples:示例:
SET @name = 43; SET @total_tax = (SELECT SUM(tax) FROM taxable_transactions);
As demonstrated by those statements, 如这些语句所示,expr
can range from simple (a literal value) to more complex (the value returned by a scalar subquery).expr
的范围可以从简单的(文字值)到更复杂的(标量子查询返回的值)。
The Performance Schema 性能架构user_variables_by_thread
table contains information about user-defined variables. user_variables_by_thread
表包含有关用户定义变量的信息。See Section 27.12.10, “Performance Schema User-Defined Variable Tables”.请参阅第27.12.10节,“性能架构用户定义变量表”。
SET
applies to parameters and local variables in the context of the stored object within which they are defined. SET
应用于定义参数和局部变量的存储对象上下文中的参数和局部变量。The following procedure uses the 以下过程使用increment
procedure parameter and counter
local variable:increment
过程参数和counter
局部变量:
CREATE PROCEDURE p(increment INT) BEGIN DECLARE counter INT DEFAULT 0; WHILE counter < 10 DO -- ... do work ... SET counter = counter + increment; END WHILE; END;
The MySQL server maintains system variables that configure its operation. MySQL服务器维护配置其操作的系统变量。A system variable can have a global value that affects server operation as a whole, a session value that affects the current session, or both. 系统变量可以具有影响整个服务器操作的全局值,也可以具有影响当前会话的会话值,或者两者兼有。Many system variables are dynamic and can be changed at runtime using the 许多系统变量是动态的,可以在运行时使用SET
statement to affect operation of the current server instance. SET
语句更改,以影响当前服务器实例的操作。SET
can also be used to persist certain system variables to the mysqld-auto.cnf
file in the data directory, to affect server operation for subsequent startups.SET
还可用于将某些系统变量持久化到数据目录中的mysqld-auto.cnf
文件中,以影响后续启动的服务器操作。
If you change a session system variable, the value remains in effect within your session until you change the variable to a different value or the session ends. 如果更改会话系统变量,该值将在会话中保持有效,直到将该变量更改为其他值或会话结束。The change has no effect on other sessions.此更改对其他会话没有影响。
If you change a global system variable, the value is remembered and used to initialize the session value for new sessions until you change the variable to a different value or the server exits. 如果更改全局系统变量,将记住该值并用于初始化新会话的会话值,直到将该变量更改为其他值或服务器退出。The change is visible to any client that accesses the global value. 任何访问全局值的客户端都可以看到该更改。However, the change affects the corresponding session value only for clients that connect after the change. 但是,更改仅影响更改后连接的客户端的相应会话值。The global variable change does not affect the session value for any current client sessions (not even the session within which the global value change occurs).全局变量更改不会影响任何当前客户端会话的会话值(甚至不会影响发生全局值更改的会话)。
To make a global system variable setting permanent so that it applies across server restarts, you can persist it to the 要使全局系统变量设置永久化,以便在服务器重新启动时应用,可以将其持久化到数据目录中的mysqld-auto.cnf
file in the data directory. mysqld-auto.cnf
文件。It is also possible to make persistent configuration changes by manually modifying a 也可以通过手动修改my.cnf
option file, but that is more cumbersome, and an error in a manually entered setting might not be discovered until much later. my.cnf
选项文件来进行持久的配置更改,但这样做更麻烦,而且手动输入的设置中的错误可能要过很久才能发现。保留系统变量的SET
statements that persist system variables are more convenient and avoid the possibility of malformed settings because settings with syntax errors do not succeed and do not change server configuration. SET
语句更方便,并且避免了设置格式错误的可能性,因为带有语法错误的设置不会成功,也不会更改服务器配置。For more information about persisting system variables and the 有关持久化系统变量和mysqld-auto.cnf
file, see Section 5.1.9.3, “Persisted System Variables”.mysqld-auto.cnf
文件的更多信息,请参阅第5.1.9.3节,“持久化系统变量”。
Setting or persisting a global system variable value always requires special privileges. 设置或持久化全局系统变量值始终需要特殊权限。Setting a session system variable value normally requires no special privileges and can be done by any user, although there are exceptions. 设置会话系统变量值通常不需要特殊权限,任何用户都可以进行设置,但也有例外。For more information, see Section 5.1.9.1, “System Variable Privileges”.有关更多信息,请参阅第5.1.9.1节,“系统变量权限”。
The following discussion describes the syntax options for setting and persisting system variables:以下讨论介绍了设置和持久化系统变量的语法选项:
To assign a value to a global system variable, precede the variable name by the 若要为全局系统变量赋值,请在变量名称前加上GLOBAL
keyword or the @@GLOBAL.
qualifier:GLOBAL
关键字或@global.
限定符:
SET GLOBAL max_connections = 1000; SET @@GLOBAL.max_connections = 1000;
To assign a value to a session system variable, precede the variable name by the 若要为会话系统变量赋值,请在变量名称前面加上SESSION
or LOCAL
keyword, by the @@SESSION.
, @@LOCAL.
, or @@
qualifier, or by no keyword or no modifier at all:SESSION
或LOCAL
关键字、@@SESSION.
、@@@LOCAL.
或@@
限定符,或不加关键字或任何修饰符:
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';
A client can change its own session variables, but not those of any other client.客户端可以更改自己的会话变量,但不能更改任何其他客户端的会话变量。
To persist a global system variable to the 若要将全局系统变量持久化到数据目录中的mysqld-auto.cnf
option file in the data directory, precede the variable name by the PERSIST
keyword or the @@PERSIST.
qualifier:mysqld-auto.cnf
选项文件中,请在变量名称前面加上PERSIST
关键字或@@PERSIST.
限定符:
SET PERSIST max_connections = 1000; SET @@PERSIST.max_connections = 1000;
This 此SET
syntax enables you to make configuration changes at runtime that also persist across server restarts. SET
语法使您能够在运行时进行配置更改,这些更改也会在服务器重新启动时保持不变。Like 与SET GLOBAL
, SET PERSIST
sets the global variable runtime value, but also writes the variable setting to the mysqld-auto.cnf
file (replacing any existing variable setting if there is one).SET GLOBAL
类似,SET PERSIST
设置全局变量运行时值,但也将变量设置写入mysqld-auto.cnf
文件(如果存在任何现有变量设置,则替换)。
To persist a global system variable to the 若要在不设置全局变量运行时值的情况下将全局系统变量持久化到mysqld-auto.cnf
file without setting the global variable runtime value, precede the variable name by the PERSIST_ONLY
keyword or the @@PERSIST_ONLY.
qualifier:mysqld-auto.cnf
文件,请在变量名称前面加上PERSIST_ONLY
关键字或@@PERSIST_ONLY.
限定符:
SET PERSIST_ONLY back_log = 100; SET @@PERSIST_ONLY.back_log = 100;
Like 与PERSIST
, PERSIST_ONLY
writes the variable setting to mysqld-auto.cnf
. PERSIST
类似,PERSIST_ONLY
将变量设置写入mysqld-auto.cnf
。However, unlike 但是,与PERSIST
, PERSIST_ONLY
does not modify the global variable runtime value. PERSIST
不同,PERSIST_ONLY
修改全局变量运行时值。This makes 这使得PERSIST_仅适用于配置只读系统变量,这些变量只能在服务器启动时设置。PERSIST_ONLY
suitable for configuring read-only system variables that can be set only at server startup.
To set a global system variable value to the compiled-in MySQL default value or a session system variable to the current corresponding global value, set the variable to the value 要将全局系统变量值设置为在MySQL中编译的默认值,或将会话系统变量设置为当前对应的全局值,请将该变量设置为值DEFAULT
. DEFAULT
。For example, the following two statements are identical in setting the session value of 例如,以下两条语句在将max_join_size
to the current global value:max_join_size
的会话值设置为当前全局值时是相同的:
SET @@SESSION.max_join_size = DEFAULT; SET @@SESSION.max_join_size = @@GLOBAL.max_join_size;
Using 使用SET
to persist a global system variable to a value of DEFAULT
or to its literal default value assigns the variable its default value and adds a setting for the variable to mysqld-auto.cnf
. SET
将全局系统变量持久化为DEFAULT
或其文字默认值将为该变量指定默认值,并将该变量的设置添加到mysqld-auto.cnf
。To remove the variable from the file, use 若要从文件中删除变量,请使用RESET PERSIST
.RESET PERSIST
。
Some system variables cannot be persisted or are persist-restricted. 某些系统变量无法持久化或受到持久化限制。See Section 5.1.9.4, “Nonpersistible and Persist-Restricted System Variables”.请参阅第5.1.9.4节,“不可逆和持续受限系统变量”。
A system variable implemented by a plugin can be persisted if the plugin is installed when the 如果在执行SET
statement is executed. SET
语句时安装了插件,则可以持久化插件实现的系统变量。Assignment of the persisted plugin variable takes effect for subsequent server restarts if the plugin is still installed. 如果仍然安装了该插件,则持久化插件变量的分配对后续服务器重启生效。If the plugin is no longer installed, the plugin variable no longer exists when the server reads the 如果不再安装插件,则当服务器读取mysqld-auto.cnf
file. mysqld-auto.cnf
文件时,插件变量不再存在。In this case, the server writes a warning to the error log and continues:在这种情况下,服务器会将警告写入错误日志并继续:
currently unknown variable 'var_name
'
was read from the persisted config file
To display system variable names and values:要显示系统变量名称和值,请执行以下操作:
Use the 使用SHOW VARIABLES
statement; see Section 13.7.7.41, “SHOW VARIABLES Statement”.SHOW VARIABLES
语句;请参阅第13.7.7.41节,“SHOW VARIABLES语句”。
Several Performance Schema tables provide system variable information. 几个性能模式表提供系统变量信息。See Section 27.12.14, “Performance Schema System Variable Tables”.请参阅第27.12.14节,“性能模式系统变量表”。
The Performance Schema 性能架构variables_info
table contains information showing when and by which user each system variable was most recently set. variables_info
表包含显示最近设置每个系统变量的时间和用户的信息。See Section 27.12.14.2, “Performance Schema variables_info Table”.请参阅第27.12.14.2节,“性能模式变量信息表”。
The Performance Schema 性能架构persisted_variables
table provides an SQL interface to the mysqld-auto.cnf
file, enabling its contents to be inspected at runtime using SELECT
statements. persistend_variables
表为mysqld-auto.cnf
文件提供了一个SQL接口,允许在运行时使用SELECT
语句检查其内容。See Section 27.12.14.1, “Performance Schema persisted_variables Table”.请参阅第27.12.14.1节,“性能模式变量表”。
If any variable assignment in a 如果SET
statement fails, the entire statement fails and no variables are changed, nor is the mysqld-auto.cnf
file changed.SET
语句中的任何变量赋值失败,则整个语句将失败,并且不会更改任何变量,mysqld-auto.cnf
文件也不会更改。
在这里描述的情况下,SET
produces an error under the circumstances described here. SET
会产生一个错误。Most of the examples show 大多数示例都显示了使用关键字语法的SET
statements that use keyword syntax (for example, GLOBAL
or SESSION
), but the principles are also true for statements that use the corresponding modifiers (for example, @@GLOBAL.
or @@SESSION.
).SET
语句(例如,GLOBAL
或SESSION
),但对于使用相应修饰符的语句(例如,@@GLOBAL.
或@@SESSION
),原则也是正确的。
Use of 使用SET
(any variant) to set a read-only variable:SET
(任何变量)设置只读变量:
mysql> SET GLOBAL version = 'abc';
ERROR 1238 (HY000): Variable 'version' is a read only variable
Use of 使用GLOBAL
, PERSIST
, or PERSIST_ONLY
to set a variable that has only a session value:GLOBAL
、PERSIST
或PERSIST_ONLY
设置只有会话值的变量:
mysql> SET GLOBAL sql_log_bin = ON;
ERROR 1228 (HY000): Variable 'sql_log_bin' is a SESSION
variable and can't be used with SET GLOBAL
Use of 使用SESSION
to set a variable that has only a global value:SESSION
设置只有全局值的变量:
mysql> SET SESSION max_connections = 1000;
ERROR 1229 (HY000): Variable 'max_connections' is a
GLOBAL variable and should be set with SET GLOBAL
Omission of 省略GLOBAL
, PERSIST
, or PERSIST_ONLY
to set a variable that has only a global value:GLOBAL
、PERSIST
或PERSIST_ONLY
用于设置只有全局值的变量:
mysql> SET max_connections = 1000;
ERROR 1229 (HY000): Variable 'max_connections' is a
GLOBAL variable and should be set with SET GLOBAL
Use of 使用PERSIST
or PERSIST_ONLY
to set a variable that cannot be persisted:PERSIST
或PERSIST_ONLY
设置无法持久化的变量:
mysql>SET PERSIST port = 3307;
ERROR 1238 (HY000): Variable 'port' is a read only variable mysql>SET PERSIST_ONLY port = 3307;
ERROR 1238 (HY000): Variable 'port' is a non persistent read only variable
The @@GLOBAL.
, @@PERSIST.
, @@PERSIST_ONLY.
, @@SESSION.
, and @@
modifiers apply only to system variables. @@GLOBAL.
、@@PERSIST.
、@@PERSIST_ONLY.
、@@SESSION.
和@@
修饰符仅适用于系统变量。An error occurs for attempts to apply them to user-defined variables, stored procedure or function parameters, or stored program local variables.尝试将它们应用于用户定义变量、存储过程或函数参数或存储程序局部变量时出错。
Not all system variables can be set to 并非所有系统变量都可以设置为DEFAULT
. DEFAULT
。In such cases, assigning 在这种情况下,指定DEFAULT
results in an error.DEFAULT
会导致错误。
An error occurs for attempts to assign 尝试将DEFAULT
to user-defined variables, stored procedure or function parameters, or stored program local variables.DEFAULT
指定给用户定义变量、存储过程或函数参数或存储程序局部变量时出错。
A SET
statement can contain multiple variable assignments, separated by commas. SET
语句可以包含多个变量赋值,用逗号分隔。This statement assigns values to a user-defined variable and a system variable:此语句为用户定义变量和系统变量赋值:
SET @x = 1, SESSION sql_mode = '';
If you set multiple system variables in a single statement, the most recent 如果在一条语句中设置多个系统变量,则语句中最近的GLOBAL
, PERSIST
, PERSIST_ONLY
, or SESSION
keyword in the statement is used for following assignments that have no keyword specified.GLOBAL
、PERSIST
、PERSIST_ONLY
或SESSION
关键字将用于以下未指定关键字的赋值。
Examples of multiple-variable assignment:多变量赋值示例:
SET GLOBAL sort_buffer_size = 1000000, SESSION sort_buffer_size = 1000000; SET @@GLOBAL.sort_buffer_size = 1000000, @@LOCAL.sort_buffer_size = 1000000; SET GLOBAL max_connections = 1000, sort_buffer_size = 1000000;
The @@GLOBAL.
, @@PERSIST.
, @@PERSIST_ONLY.
, @@SESSION.
, and @@
modifiers apply only to the immediately following system variable, not any remaining system variables. @@GLOBAL.
、@@PERSIST.
、@@PERSIST_ONLY.
、@@SESSION.
和@@
修饰符仅适用于紧跟其后的系统变量,而不适用于任何剩余的系统变量。This statement sets the 此语句将sort_buffer_size
global value to 50000 and the session value to 1000000:sort_buffer_size
全局值设置为50000,将会话值设置为1000000:
SET @@GLOBAL.sort_buffer_size = 50000, sort_buffer_size = 1000000;
To refer to the value of a system variable in expressions, use one of the 若要在表达式中引用系统变量的值,请使用其中一个@@
-modifiers (except @@PERSIST.
and @@PERSIST_ONLY.
, which are not permitted in expressions). @@
-修饰符(除了@@PERSIST.
和@@PERSIST_ONLY.
,这在表达式中是不允许的)。For example, you can retrieve system variable values in a 例如,您可以在SELECT
statement like this:SELECT
语句中检索系统变量值,如下所示:
SELECT @@GLOBAL.sql_mode, @@SESSION.sql_mode, @@sql_mode;
A reference to a system variable in an expression as 表达式中对系统变量的引用为@@
(with var_name
@@
rather than @@GLOBAL.
or @@SESSION.
) returns the session value if it exists and the global value otherwise. @@
(使用var_name
@@
而不是@@GLOBAL.
或@@SESSION.
)返回会话值(如果存在),否则返回全局值。This differs from 这SET @@
, which always refers to the session value.var_name
= expr
SET @@
不同,后者总是引用会话值。var_name
= expr