- 横杠与下划线:-、_:At runtime, system variable names must be written using underscores, not dashes
- 前缀 :--maximum-
- 大小写:
- 布尔值:0、1、off、on、false、true:某些布尔值配置,在启动命令选项或选项文件内,只能用{0|1},不能用{off|on}
- 后缀:k、m、g、K、M、G:可以用在启动命令选项或选项文件内;不可用于set语句
- 数值表达式:1024*1024:可以用在set语句;不可以用在启动命令选项或选项文件内'
- 这4个东西,大部分同时存在,互有对应;而很多都是没有一一对应的,不要想当然!
- 4种数据类型:字符串、数字、布尔、枚举(索引号从0开始,而不是1开始)
- 对于布尔值配置项的特殊处理:--disable-、--skip-、--loose-
The following description of option file syntax applies to files that you edit manually. This excludes .mylogin.cnf
, which is created using mysql_config_editor and is encrypted.
Any long option that may be given on the command line when running a MySQL program can be given in an option file as well. To get the list of available options for a program, run it with the --help
option. (For mysqld, use --verbose
and --help
.)
The syntax for specifying options in an option file is similar to command-line syntax (see Section 4.2.2.1, “Using Options on the Command Line”). However, in an option file, you omit the leading two dashes from the option name and you specify only one option per line. For example, --quick
and --host=localhost
on the command line should be specified as quick
and host=localhost
on separate lines in an option file. To specify an option of the form --loose-
in an option file, write it as opt_name
loose-
.opt_name
Empty lines in option files are ignored. Nonempty lines can take any of the following forms:
-
#
,comment
;
comment
Comment lines start with {
#
|;}
A#
comment can start in the middle of a line as well. -
[
group
]group
is the name of the program or group for which you want to set options. After a group line, any option-setting lines apply to the named group until the end of the option file or another group line is given. Option group names are not case-sensitive. -
opt_name
This is equivalent to
--
on the command line.opt_name
-
opt_name
=value
This is equivalent to
--
on the command line. In an option file, you can have spaces around theopt_name
=value
=
character, something that is not true on the command line. The value optionally can be enclosed within single quotation marks or double quotation marks, which is useful if the value contains a#
comment character.
Leading and trailing spaces are automatically deleted from option names and values.
You can use the escape sequences ,
,
,
, \
, and s
in option values to represent the backspace, tab, newline, carriage return, backslash, and space characters. In option files, these escaping rules apply:
-
A backslash followed by a valid escape sequence character is converted to the character represented by the sequence. For example,
s
is converted to a space. -
A backslash not followed by a valid escape sequence character remains unchanged. For example,
S
is retained as is.
The preceding rules mean that a literal backslash can be given as \
, or as if it is not followed by a valid escape sequence character.
The rules for escape sequences in option files differ slightly from the rules for escape sequences in string literals in SQL statements. In the latter context, if “x
” is not a valid escape sequence character,
becomes “x
x
” rather than
. See Section 9.1.1, “String Literals”. x
The escaping rules for option file values are especially pertinent for Windows path names, which use as a path name separator. A separator in a Windows path name must be written as
\
if it is followed by an escape sequence character. It can be written as \
or if it is not. Alternatively,
/
may be used in Windows path names and will be treated as . Suppose that you want to specify a base directory of
C:Program FilesMySQLMySQL Server 5.7
in an option file. This can be done several ways. Some examples:
basedir="C:Program FilesMySQLMySQL Server 5.7" basedir="C:\Program Files\MySQL\MySQL Server 5.7" basedir="C:/Program Files/MySQL/MySQL Server 5.7" basedir=C:\ProgramsFiles\MySQL\MySQLsServers5.7
If an option group name is the same as a program name, options in the group apply specifically to that program. For example, the [mysqld]
and [mysql]
groups apply to the mysqld server and the mysql client program, respectively.
The [client]
option group is read by all client programs provided in MySQL distributions (but not by mysqld). To understand how third-party client programs that use the C API can use option files, see the C API documentation at Section 27.7.6.50, “mysql_options()”.
The [client]
group enables you to specify options that apply to all clients. For example, [client]
is the appropriate group to use to specify the password for connecting to the server. (But make sure that the option file is accessible only by yourself, so that other people cannot discover your password.) Be sure not to put an option in the [client]
group unless it is recognized by all client programs that you use. Programs that do not understand the option quit after displaying an error message if you try to run them.
List more general option groups first and more specific groups later. For example, a [client]
group is more general because it is read by all client programs, whereas a [mysqldump]
group is read only by mysqldump. Options specified later override options specified earlier, so putting the option groups in the order [client]
, [mysqldump]
enables mysqldump-specific options to override [client]
options.
Here is a typical global option file:
[client] port=3306 socket=/tmp/mysql.sock [mysqld] port=3306 socket=/tmp/mysql.sock key_buffer_size=16M max_allowed_packet=8M [mysqldump] quick
Here is a typical user option file:
[client] # The following password will be sent to all standard MySQL clients password="my password" [mysql] no-auto-rehash connect_timeout=2
To create option groups to be read only by mysqld servers from specific MySQL release series, use groups with names of [mysqld-5.6]
, [mysqld-5.7]
, and so forth. The following group indicates that the sql_mode
setting should be used only by MySQL servers with 5.7.x version numbers:
[mysqld-5.7] sql_mode=TRADITIONAL
It is possible to use !include
directives in option files to include other option files and !includedir
to search specific directories for option files. For example, to include the /home/mydir/myopt.cnf
file, use the following directive:
!include /home/mydir/myopt.cnf
To search the /home/mydir
directory and read option files found there, use this directive:
!includedir /home/mydir
MySQL makes no guarantee about the order in which option files in the directory are read.
Any files to be found and included using the !includedir
directive on Unix operating systems must have file names ending in .cnf
. On Windows, this directive checks for files with the .ini
or .cnf
extension.
Write the contents of an included option file like any other option file. That is, it should contain groups of options, each preceded by a [
line that indicates the program to which the options apply.group
]
While an included file is being processed, only those options in groups that the current program is looking for are used. Other groups are ignored. Suppose that a my.cnf
file contains this line:
!include /home/mydir/myopt.cnf
And suppose that /home/mydir/myopt.cnf
looks like this:
[mysqladmin] force [mysqld] key_buffer_size=16M
If my.cnf
is processed by mysqld, only the [mysqld]
group in /home/mydir/myopt.cnf
is used. If the file is processed by mysqladmin, only the [mysqladmin]
group is used. If the file is processed by any other program, no options in /home/mydir/myopt.cnf
are used.
The !includedir
directive is processed similarly except that all option files in the named directory are read.
If an option file contains !include
or !includedir
directives, files named by those directives are processed whenever the option file is processed, no matter where they appear in the file.
For inclusion directives to work, the file path should not be specified within quotes and should have no escape sequences. For example, the following statements provided in my.ini
will read the option file myopts.ini
:
!include C:/ProgramData/MySQL/MySQL Server/myopts.ini !include C:ProgramDataMySQLMySQL Servermyopts.ini !include C:\ProgramData\MySQL\MySQL Server\myopts.ini
On Windows, if !include
is the last line in the file, make sure that a newline is appended at the end or the line will be ignored./path/to/extra.ini
Most MySQL programs that support option files handle the following options. Because these options affect option-file handling, they must be given on the command line and not in an option file. To work properly, each of these options must be given before other options, with these exceptions:
-
--print-defaults
may be used immediately after--defaults-file
,--defaults-extra-file
, or--login-path
. -
On Windows, if the server is started with the
--defaults-file
and--install
options,--install
must be first. See Section 2.3.4.8, “Starting MySQL as a Windows Service”.
When specifying file names as option values, avoid the use of the ~
shell metacharacter because it might not be interpreted as you expect.
-
--defaults-extra-file=
file_name
Read this option file after the global option file but (on Unix) before the user option file and (on all platforms) before the login path file. (For information about the order in which option files are used, see Section 4.2.2.2, “Using Option Files”.) If the file does not exist or is otherwise inaccessible, an error occurs.
file_name
is interpreted relative to the current directory if given as a relative path name rather than a full path name.See the introduction to this section regarding constraints on the position in which this option may be specified.
-
Read only the given option file. If the file does not exist or is otherwise inaccessible, an error occurs.
file_name
is interpreted relative to the current directory if given as a relative path name rather than a full path name.Exception: Even with
--defaults-file
, client programs read.mylogin.cnf
.See the introduction to this section regarding constraints on the position in which this option may be specified.
-
Read not only the usual option groups, but also groups with the usual names and a suffix of
str
. For example, the mysql client normally reads the[client]
and[mysql]
groups. If the--defaults-group-suffix=_other
option is given, mysql also reads the[client_other]
and[mysql_other]
groups. -
Read options from the named login path in the
.mylogin.cnf
login path file. A “login path” is an option group containing options that specify which MySQL server to connect to and which account to authenticate as. To create or modify a login path file, use the mysql_config_editor utility. See Section 4.6.6, “mysql_config_editor — MySQL Configuration Utility”.A client program reads the option group corresponding to the named login path, in addition to option groups that the program reads by default. Consider this command:
mysql --login-path=mypath
By default, the mysql client reads the
[client]
and[mysql]
option groups. So for the command shown, mysql reads[client]
and[mysql]
from other option files, and[client]
,[mysql]
, and[mypath]
from the login path file.Client programs read the login path file even when the
--no-defaults
option is used.To specify an alternate login path file name, set the
MYSQL_TEST_LOGIN_FILE
environment variable.See the introduction to this section regarding constraints on the position in which this option may be specified.
-
Do not read any option files. If program startup fails due to reading unknown options from an option file,
--no-defaults
can be used to prevent them from being read.The exception is that client programs read the
.mylogin.cnf
login path file, if it exists, even when--no-defaults
is used. This permits passwords to be specified in a safer way than on the command line even if--no-defaults
is present. (.mylogin.cnf
is created by the mysql_config_editor utility. See Section 4.6.6, “mysql_config_editor — MySQL Configuration Utility”.) -
Print the program name and all options that it gets from option files. Password values are masked.
See the introduction to this section regarding constraints on the position in which this option may be specified.
Some options are “boolean” and control behavior that can be turned on or off. For example, the mysql client supports a --column-names
option that determines whether or not to display a row of column names at the beginning of query results. By default, this option is enabled. However, you may want to disable it in some instances, such as when sending the output of mysql into another program that expects to see only data and not an initial header line.
To disable column names, you can specify the option using any of these forms:
--disable-column-names --skip-column-names --column-names=0
The --disable
and --skip
prefixes and the =0
suffix all have the same effect: They turn the option off.
The “enabled” form of the option may be specified in any of these ways:
--column-names --enable-column-names --column-names=1
The values ON
, TRUE
, OFF
, and FALSE
are also recognized for boolean options (not case-sensitive).
If an option is prefixed by --loose
, a program does not exit with an error if it does not recognize the option, but instead issues only a warning:
shell> mysql --loose-no-such-option
mysql: WARNING: unknown option '--loose-no-such-option'
The --loose
prefix can be useful when you run programs from multiple installations of MySQL on the same machine and list options in an option file. An option that may not be recognized by all versions of a program can be given using the --loose
prefix (or loose
in an option file). Versions of the program that recognize the option process it normally, and versions that do not recognize it issue a warning and ignore it.
The --maximum
prefix is available for mysqld only and permits a limit to be placed on how large client programs can set session system variables. To do this, use a --maximum
prefix with the variable name. For example, --maximum-max_heap_table_size=32M
prevents any client from making the heap table size limit larger than 32M.
The --maximum
prefix is intended for use with system variables that have a session value. If applied to a system variable that has only a global value, an error occurs. For example, with --maximum-back_log=200
, the server produces this error:
Maximum value of 'back_log' cannot be set
Many MySQL programs have internal variables that can be set at runtime using the SET
statement. See Section 13.7.4.1, “SET Syntax for Variable Assignment”, and Section 5.1.8, “Using System Variables”.
Most of these program variables also can be set at server startup by using the same syntax that applies to specifying program options. For example, mysql has a max_allowed_packet
variable that controls the maximum size of its communication buffer. To set the max_allowed_packet
variable for mysql to a value of 16MB, use either of the following commands:
mysql --max_allowed_packet=16777216 mysql --max_allowed_packet=16M
The first command specifies the value in bytes. The second specifies the value in megabytes. For variables that take a numeric value, the value can be given with a suffix of K
, M
, or G
(either uppercase or lowercase) to indicate a multiplier of 1024, 10242 or 10243. (For example, when used to set max_allowed_packet
, the suffixes indicate units of kilobytes, megabytes, or gigabytes.)
In an option file, variable settings are given without the leading dashes:
[mysql] max_allowed_packet=16777216
Or:
[mysql] max_allowed_packet=16M
If you like, underscores in a variable name can be specified as dashes. The following option groups are equivalent. Both set the size of the server's key buffer to 512MB:
[mysqld] key_buffer_size=512M [mysqld] key-buffer-size=512M
A variable can be specified by writing it in full or as any unambiguous prefix. For example, the max_allowed_packet
variable can be set for mysql as --max_a
, but not as --max
because the latter is ambiguous:
shell> mysql --max=1000000
mysql: ambiguous option '--max=1000000' (max_allowed_packet, max_join_size)
Be aware that the use of variable prefixes can cause problems in the event that new variables are implemented for a program. A prefix that is unambiguous now might become ambiguous in the future.
Suffixes for specifying a value multiplier can be used when setting a variable at program invocation time, but not to set the value with SET
at runtime. On the other hand, with SET
, you can assign a variable's value using an expression, which is not true when you set a variable at server startup. For example, the first of the following lines is legal at program invocation time, but the second is not:
shell>mysql --max_allowed_packet=16M
shell>mysql --max_allowed_packet=16*1024*1024
Conversely, the second of the following lines is legal at runtime, but the first is not:
mysql>SET GLOBAL max_allowed_packet=16M;
mysql>SET GLOBAL max_allowed_packet=16*1024*1024;
By convention, long forms of options that assign a value are written with an equals (=
) sign, like this:
mysql --host=tonfisk --user=jon
For options that require a value (that is, not having a default value), the equal sign is not required, and so the following is also valid:
mysql --host tonfisk --user jon
In both cases, the mysql client attempts to connect to a MySQL server running on the host named “tonfisk” using an account with the user name “jon”.
Due to this behavior, problems can occasionally arise when no value is provided for an option that expects one. Consider the following example, where a user connects to a MySQL server running on host tonfisk
as user jon
:
shell>mysql --host 85.224.35.45 --user jon
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.7.30 Source distribution Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>SELECT CURRENT_USER();
+----------------+ | CURRENT_USER() | +----------------+ | jon@% | +----------------+ 1 row in set (0.00 sec)
Omitting the required value for one of these option yields an error, such as the one shown here:
shell> mysql --host 85.224.35.45 --user
mysql: option '--user' requires an argument
In this case, mysql was unable to find a value following the --user
option because nothing came after it on the command line. However, if you omit the value for an option that is not the last option to be used, you obtain a different error that you may not be expecting:
shell> mysql --host --user jon
ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)
Because mysql assumes that any string following --host
on the command line is a host name, --host
--user
is interpreted as --host=--user
, and the client attempts to connect to a MySQL server running on a host named “--user”.
Options having default values always require an equal sign when assigning a value; failing to do so causes an error. For example, the MySQL server --log-error
option has the default value
, where host_name
.errhost_name
is the name of the host on which MySQL is running. Assume that you are running MySQL on a computer whose host name is “tonfisk”, and consider the following invocation of mysqld_safe:
shell> mysqld_safe &
[1] 11699
shell> 080112 12:53:40 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080112 12:53:40 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
shell>
After shutting down the server, restart it as follows:
shell> mysqld_safe --log-error &
[1] 11699
shell> 080112 12:53:40 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080112 12:53:40 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
shell>
The result is the same, since --log-error
is not followed by anything else on the command line, and it supplies its own default value. (The &
character tells the operating system to run MySQL in the background; it is ignored by MySQL itself.) Now suppose that you wish to log errors to a file named my-errors.err
. You might try starting the server with --log-error my-errors
, but this does not have the intended effect, as shown here:
shell> mysqld_safe --log-error my-errors &
[1] 31357
shell> 080111 22:53:31 mysqld_safe Logging to '/usr/local/mysql/var/tonfisk.err'.
080111 22:53:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
080111 22:53:34 mysqld_safe mysqld from pid file /usr/local/mysql/var/tonfisk.pid ended
[1]+ Done ./mysqld_safe --log-error my-errors
The server attempted to start using /usr/local/mysql/var/tonfisk.err
as the error log, but then shut down. Examining the last few lines of this file shows the reason:
shell> tail /usr/local/mysql/var/tonfisk.err
2013-09-24T15:36:22.278034Z 0 [ERROR] Too many arguments (first extra is 'my-errors').
2013-09-24T15:36:22.278059Z 0 [Note] Use --verbose --help to get a list of available options!
2013-09-24T15:36:22.278076Z 0 [ERROR] Aborting
2013-09-24T15:36:22.279704Z 0 [Note] InnoDB: Starting shutdown...
2013-09-24T15:36:23.777471Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2319086
2013-09-24T15:36:23.780134Z 0 [Note] mysqld: Shutdown complete
Because the --log-error
option supplies a default value, you must use an equal sign to assign a different value to it, as shown here:
shell> mysqld_safe --log-error=my-errors &
[1] 31437
shell> 080111 22:54:15 mysqld_safe Logging to '/usr/local/mysql/var/my-errors.err'.
080111 22:54:15 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
shell>
Now the server has been started successfully, and is logging errors to the file /usr/local/mysql/var/my-errors.err
.
Similar issues can arise when specifying option values in option files. For example, consider a my.cnf
file that contains the following:
[mysql] host user
When the mysql client reads this file, these entries are parsed as --host
--user
or --host=--user
, with the result shown here:
shell> mysql
ERROR 2005 (HY000): Unknown MySQL server host '--user' (1)
However, in option files, an equal sign is not assumed. Suppose the my.cnf
file is as shown here:
[mysql] user jon
Trying to start mysql in this case causes a different error:
shell> mysql
mysql: unknown option '--user jon'
A similar error would occur if you were to write host tonfisk
in the option file rather than host=tonfisk
. Instead, you must use the equal sign:
[mysql] user=jon
Now the login attempt succeeds:
shell>mysql
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 Server version: 5.7.30 Source distribution Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>SELECT USER();
+---------------+ | USER() | +---------------+ | jon@localhost | +---------------+ 1 row in set (0.00 sec)
This is not the same behavior as with the command line, where the equal sign is not required:
shell>mysql --user jon --host tonfisk
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 6 Server version: 5.7.30 Source distribution Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>SELECT USER();
+---------------+ | USER() | +---------------+ | jon@tonfisk | +---------------+ 1 row in set (0.00 sec)
Specifying an option requiring a value without a value in an option file causes the server to abort with an error.
###############----------------------
MySQL uses algorithms that are very scalable, so you can usually run with very little memory. However, normally better performance results from giving MySQL more memory.
When tuning a MySQL server, the two most important variables to configure are key_buffer_size
and table_open_cache
. You should first feel confident that you have these set appropriately before trying to change any other variables. If you are performing GROUP BY
or ORDER BY
operations on tables that are much larger than your available memory, increase the value of read_rnd_buffer_size
to speed up the reading of rows following sorting operations.
The MySQL server has many operating parameters, which you can change at server startup using command-line options or configuration files (option files). It is also possible to change many parameters at runtime. For general instructions on setting parameters at startup or runtime, see Section 5.1.6, “Server Command Options”, and Section 5.1.7, “Server System Variables”.
On Windows, MySQL Installer interacts with the user and creates a file named my.ini
in the base installation directory as the default option file. If you install on Windows from a Zip archive, you can copy the my-default.ini
template file in the base installation directory to my.ini
and use the latter as the default option file.
As of MySQL 5.7.18, my-default.ini
is no longer included in or installed by distribution packages.
On Windows, the .ini
or .cnf
option file extension might not be displayed.
After completing the installation process, you can edit the default option file at any time to modify the parameters used by the server. For example, to use a parameter setting in the file that is commented with a #
character at the beginning of the line, remove the #
, and modify the parameter value if necessary. To disable a setting, either add a #
to the beginning of the line or remove it.
For non-Windows platforms, no default option file is created during either the server installation or the data directory initialization process. Create your option file by following the instructions given in Section 4.2.2.2, “Using Option Files”. Without an option file, the server just starts with its default settings—see Section 5.1.2, “Server Configuration Defaults” on how to check those settings.
For additional information about option file format and syntax, see Section 4.2.2.2, “Using Option Files”.
When you start the mysqld server, you can specify program options using any of the methods described in Section 4.2.2, “Specifying Program Options”. The most common methods are to provide options in an option file or on the command line. However, in most cases it is desirable to make sure that the server uses the same options each time it runs. The best way to ensure this is to list them in an option file. See Section 4.2.2.2, “Using Option Files”. That section also describes option file format and syntax.
mysqld reads options from the [mysqld]
and [server]
groups. mysqld_safe reads options from the [mysqld]
, [server]
, [mysqld_safe]
, and [safe_mysqld]
groups. mysql.server reads options from the [mysqld]
and [mysql.server]
groups.
An embedded MySQL server usually reads options from the [server]
, [embedded]
, and [
groups, where xxxxx
_SERVER]xxxxx
is the name of the application into which the server is embedded.
mysqld accepts many command options. For a brief summary, execute this command:
mysqld --help
To see the full list, use this command:
mysqld --verbose --help
Some of the items in the list are actually system variables that can be set at server startup. These can be displayed at runtime using the SHOW VARIABLES
statement. Some items displayed by the preceding mysqld command do not appear in SHOW VARIABLES
output; this is because they are options only and not system variables.
The MySQL server maintains many system variables that configure its operation. Each system variable has a default value. System variables can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically at runtime using the SET
statement, which enables you to modify operation of the server without having to stop and restart it. You can also use system variable values in expressions.
At runtime, setting a global system variable value requires the SUPER
privilege. 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.8.1, “System Variable Privileges”
There are several ways to see the names and values of system variables:
-
To see the values that a server will use based on its compiled-in defaults and any option files that it reads, use this command:
mysqld --verbose --help
-
To see the values that a server will use based only on its compiled-in defaults, ignoring the settings in any option files, use this command:
mysqld --no-defaults --verbose --help
-
To see the current values used by a running server, use the
SHOW VARIABLES
statement or the Performance Schema system variable tables. See Section 25.12.13, “Performance Schema System Variable Tables”.
The MySQL server maintains many system variables that configure its operation. Section 5.1.7, “Server System Variables”, describes the meaning of these variables. Each system variable has a default value. System variables can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically while the server is running by means of the SET
statement, which enables you to modify operation of the server without having to stop and restart it. You can also use system variable values in expressions.
Many system variables are built in. System variables implemented by a server plugin are exposed when the plugin is installed and have names that begin with the plugin name. For example, the audit_log
plugin implements a system variable named audit_log_policy
.
There are two scopes in which system variables exist. Global variables affect the overall operation of the server. Session variables affect its operation for individual client connections. A given system variable can have both a global and a session value. Global and session system variables are related as follows:
-
When the server starts, it initializes each global variable to its default value. These defaults can be changed by options specified on the command line or in an option file. (See Section 4.2.2, “Specifying Program Options”.)
-
The server also maintains a set of session variables for each client that connects. The client's session variables are initialized at connect time using the current values of the corresponding global variables. For example, a client's SQL mode is controlled by the session
sql_mode
value, which is initialized when the client connects to the value of the globalsql_mode
value.For some system variables, the session value is not initialized from the corresponding global value; if so, that is indicated in the variable description.
System variable values can be set globally at server startup by using options on the command line or in an option file. At startup, the syntax for system variables is the same as for command options, so within variable names, dashes and underscores may be used interchangeably. For example, --general_log=ON
and --general-log=ON
are equivalent.
When you use a startup option to set a variable that takes a numeric value, the value can be given with a suffix of K
, M
, or G
(either uppercase or lowercase) to indicate a multiplier of 1024, 10242 or 10243; that is, units of kilobytes, megabytes, or gigabytes, respectively. Thus, the following command starts the server with an InnoDB
log file size of 16 megabytes and a maximum packet size of one gigabyte:
mysqld --innodb-log-file-size=16M --max-allowed-packet=1G
Within an option file, those variables are set like this:
[mysqld] innodb_log_file_size=16M max_allowed_packet=1G
The lettercase of suffix letters does not matter; 16M
and 16m
are equivalent, as are 1G
and 1g
.
To restrict the maximum value to which a system variable can be set at runtime with the SET
statement, specify this maximum by using an option of the form --maximum-
at server startup. For example, to prevent the value of var_name
=value
innodb_log_file_size
from being increased to more than 32MB at runtime, use the option --maximum-innodb-log-file-size=32M
.
Many system variables are dynamic and can be changed at runtime by using the SET
statement. For a list, see Section 5.1.8.2, “Dynamic System Variables”. To change a system variable with SET
, refer to it by name, optionally preceded by a modifier. At runtime, system variable names must be written using underscores, not dashes. The following examples briefly illustrate this syntax:
-
Set a global system variable:
SET GLOBAL max_connections = 1000; SET @@GLOBAL.max_connections = 1000;
-
Set a session system variable:
SET SESSION sql_mode = 'TRADITIONAL'; SET @@SESSION.sql_mode = 'TRADITIONAL'; SET @@sql_mode = 'TRADITIONAL';
For complete details about SET
syntax, see Section 13.7.4.1, “SET Syntax for Variable Assignment”. For a description of the privilege requirements for setting system variables, see Section 5.1.8.1, “System Variable Privileges”
Suffixes for specifying a value multiplier can be used when setting a variable at server startup, but not to set the value with SET
at runtime. On the other hand, with SET
you can assign a variable's value using an expression, which is not true when you set a variable at server startup. For example, the first of the following lines is legal at server startup, but the second is not:
shell>mysql --max_allowed_packet=16M
shell>mysql --max_allowed_packet=16*1024*1024
Conversely, the second of the following lines is legal at runtime, but the first is not:
mysql>SET GLOBAL max_allowed_packet=16M;
mysql>SET GLOBAL max_allowed_packet=16*1024*1024;
Some system variables can be enabled with the SET
statement by setting them to ON
or 1
, or disabled by setting them to OFF
or 0
. However, to set such a variable on the command line or in an option file, you must set it to 1
or 0
; setting it to ON
or OFF
will not work. For example, on the command line, --delay_key_write=1
works but --delay_key_write=ON
does not.
To display system variable names and values, use the SHOW VARIABLES
statement:
mysql> SHOW VARIABLES;
With a LIKE
clause, the statement displays only those variables that match the pattern. To obtain a specific variable name, use a LIKE
clause as shown:
SHOW VARIABLES LIKE 'max_join_size'; SHOW SESSION VARIABLES LIKE 'max_join_size';
To get a list of variables whose name match a pattern, use the %
wildcard character in a LIKE
clause:
SHOW VARIABLES LIKE '%size%'; SHOW GLOBAL VARIABLES LIKE '%size%';
Wildcard characters can be used in any position within the pattern to be matched. Strictly speaking, because _
is a wildcard that matches any single character, you should escape it as \_
to match it literally. In practice, this is rarely necessary.
For SHOW VARIABLES
, if you specify neither GLOBAL
nor SESSION
, MySQL returns SESSION
values.
The reason for requiring the GLOBAL
keyword when setting GLOBAL
-only variables but not when retrieving them is to prevent problems in the future:
-
Were a
SESSION
variable to be removed that has the same name as aGLOBAL
variable, a client with privileges sufficient to modify global variables might accidentally change theGLOBAL
variable rather than just theSESSION
variable for its own session. -
Were a
SESSION
variable to be added with the same name as aGLOBAL
variable, a client that intends to change theGLOBAL
variable might find only its ownSESSION
variable changed.
A system variable can have a global value that affects server operation as a whole, a session value that affects only the current session, or both. To modify system variable runtime values, use the SET
statement. See Section 13.7.4.1, “SET Syntax for Variable Assignment”. This section describes the privileges required to assign values to system variables at runtime.
Setting a global system variable runtime value requires the SUPER
privilege.
To set a session system variable runtime value, use the SET SESSION
statement. In contrast to setting global runtime values, setting session runtime values normally requires no special privileges and can be done by any user to affect the current session. For some system variables, setting the session value may have effects outside the current session and thus is a restricted operation that can be done only by users who have the SUPER
privilege. If a session system variable is restricted in this way, the variable description indicates that restriction. Examples include binlog_format
and sql_log_bin
. Setting the session value of these variables affects binary logging for the current session, but may also have wider implications for the integrity of server replication and backups.
Many server system variables are dynamic and can be set at runtime. See Section 13.7.4.1, “SET Syntax for Variable Assignment”. For a description of the privilege requirements for setting system variables, see Section 5.1.8.1, “System Variable Privileges”
The following table lists all dynamic system variables applicable within mysqld
.
The table lists each variable's data type and scope. The last column indicates whether the scope for each variable is Global, Session, or both. Please see the corresponding item descriptions for details on setting and using the variables. Where appropriate, direct links to further information about the items are provided.
Variables that have a type of “string” take a string value. Variables that have a type of “numeric” take a numeric value. Variables that have a type of “boolean” can be set to 0, 1, ON
or OFF
. Variables that are marked as “enumeration” normally should be set to one of the available values for the variable, but can also be set to the number that corresponds to the desired enumeration value. For enumerated system variables, the first enumeration value corresponds to 0. This differs from the ENUM
data type used for table columns
The MySQL server maintains many status variables that provide information about its operation. You can view these variables and their values by using the SHOW [GLOBAL | SESSION] STATUS
statement (see Section 13.7.5.35, “SHOW STATUS Statement”). The optional GLOBAL
keyword aggregates the values over all connections, and SESSION
shows the values for the current connection.
mysql> SHOW GLOBAL STATUS;
+-----------------------------------+------------+
| Variable_name | Value |
+-----------------------------------+------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
...
+-----------------------------------+------------+
Many status variables are reset to 0 by the FLUSH STATUS
statement.