Most MySQL programs can read startup options from option files (sometimes called configuration files). 大多数MySQL程序都可以从选项文件(有时称为配置文件)中读取启动选项。Option files provide a convenient way to specify commonly used options so that they need not be entered on the command line each time you run a program.选项文件提供了一种方便的方法来指定常用的选项,以便在每次运行程序时不必在命令行中输入这些选项。
To determine whether a program reads option files, invoke it with the 要确定程序是否读取选项文件,请使用--help
option. --help
选项调用它。(For mysqld, use (对于mysqld,请使用--verbose
and --help
.) --verbose
和--help
。)If the program reads option files, the help message indicates which files it looks for and which option groups it recognizes.如果程序读取选项文件,则帮助消息会指示它要查找哪些文件以及可以识别哪些选项组。
A MySQL program started with the 以--no-defaults
option reads no option files other than .mylogin.cnf
.--no-defaults
选项启动的MySQL程序不读取.mylogin.cnf
以外的任何选项文件。
A server started with the 在禁用persisted_globals_load
system variable disabled does not read mysqld-auto.cnf
.persisted_globals_load
系统变量的情况下启动的服务器不会读取mysqld-auto.cnf
。
Many option files are plain text files, created using any text editor. The exceptions are:许多选项文件都是使用任何文本编辑器创建的纯文本文件。例外情况是:
The 包含登录路径选项的.mylogin.cnf
file that contains login path options. .mylogin.cnf
文件。This is an encrypted file created by the mysql_config_editor utility. 这是一个由mysql_config_editor实用程序创建的加密文件。See Section 4.6.7, “mysql_config_editor — MySQL Configuration Utility”. 请参阅第4.6.7节,“mysql_config_editor-mysql配置实用程序”。A “login path” is an option group that permits only certain options: “登录路径”是一个选项组,只允许某些选项:host
, user
, password
, port
and socket
. host
、user
、password
、port
和socket
。Client programs specify which login path to read from 客户端程序使用.mylogin.cnf
using the --login-path
option.--login-path
选项指定要从.mylogin.cnf
读取的登录路径。
To specify an alternative login path file name, set the 要指定备用登录路径文件名,请设置MYSQL_TEST_LOGIN_FILE
environment variable. MYSQL_TEST_LOGIN_FILE
环境变量。This variable is used by the mysql-test-run.pl testing utility, but also is recognized by mysql_config_editor and by MySQL clients such as mysql, mysqladmin, and so forth.此变量由mysql-test-run.pl
测试实用程序使用,但也由mysql_config_editor
和mysql客户端(如mysql、mysqladmin等)识别。
The 数据目录中的mysqld-auto.cnf
file in the data directory. mysqld-auto.cnf
文件。This JSON-format file contains persisted system variable settings. 此JSON格式文件包含持久化的系统变量设置。It is created by the server upon execution of 它是由服务器在执行SET PERSIST
or SET PERSIST_ONLY
statements. SET PERSIST
或SET PERSIST_ONLY
语句时创建的。See Section 5.1.9.3, “Persisted System Variables”. Management of 请参阅第5.1.9.3节,“持久化系统变量”。mysqld-auto.cnf
should be left to the server and not performed manually.mysqld-auto.cnf
的管理应该留给服务器,而不是手动执行。
MySQL looks for option files in the order described in the following discussion and reads any that exist. If an option file you want to use does not exist, create it using the appropriate method, as just discussed.MySQL按照以下讨论中描述的顺序查找选项文件,并读取任何存在的选项文件。如果您要使用的选项文件不存在,请使用刚才讨论的适当方法创建它。
For information about option files used with NDB Cluster programs, see Section 23.3, “Configuration of NDB Cluster”.有关NDB群集程序使用的选项文件的信息,请参阅第23.3节,“NDB群集的配置”。
On Windows, MySQL programs read startup options from the files shown in the following table, in the specified order (files listed first are read first, files read later take precedence).在Windows上,MySQL程序按照指定的顺序从下表中显示的文件中读取启动选项(首先列出的文件优先读取,稍后读取的文件优先)。
Table 4.1 Option Files Read on Windows Systems表4.1 在Windows系统上读取选项文件
,
| |
C:\my.ini , C:\my.cnf | |
,
| |
defaults-extra-file | --defaults-extra-file , if any--defaults-extra-file 指定的文件(如果有的话) |
| |
| SET PERSIST or SET PERSIST_ONLY (server only)SET PERSIST 或SET PERSIST_ONLY 持久化的系统变量(仅限服务器) |
In the preceding table, 在上表中,%WINDIR%
represents the location of your Windows directory. %WINDIR%
表示Windows目录的位置。This is commonly 这通常是C:\WINDOWS
. C:\WINDOWS
。Use the following command to determine its exact location from the value of the 使用以下命令根据WINDIR
environment variable:WINDIR
环境变量的值确定其确切位置:
C:\> echo %WINDIR%
%APPDATA%
represents the value of the Windows application data directory. 表示Windows应用程序数据目录的值。Use the following command to determine its exact location from the value of the 使用以下命令根据APPDATA
environment variable:APPDATA
环境变量的值确定其确切位置:
C:\> echo %APPDATA%
BASEDIR
represents the MySQL base installation directory. 表示MySQL的基本安装目录。When MySQL 8.0 has been installed using MySQL Installer, this is typically 当使用MySQL安装程序安装MySQL 8.0时,通常是C:\
where PROGRAMDIR
\MySQL\MySQL 8.0 ServerPROGRAMDIR
represents the programs directory (usually Program Files
on English-language versions of Windows), See Section 2.3.3, “MySQL Installer for Windows”.C:\PROGRAMDIR\MySQL\MysQL8.0 Server
,其中PROGRAMDIR
表示程序目录(英语版本的Windows上通常是Program Files
),请参阅第2.3.3节,“适用于Windows的MySQL安装程序”。
DATADIR
represents the MySQL data directory. 表示MySQL数据目录。As used to find 用于查找mysqld-auto.cnf
, its default value is the data directory location built in when MySQL was compiled, but can be changed by --datadir
specified as an option-file or command-line option processed before mysqld-auto.cnf
is processed.mysqld-auto.cnf
,其默认值是MySQL编译时内置的数据目录位置,但可以通过--datadir
进行更改,该datadir指定为在处理mysqld-aut.onf
之前处理的选项文件或命令行选项。
On Unix and Unix-like systems, MySQL programs read startup options from the files shown in the following table, in the specified order (files listed first are read first, files read later take precedence).在Unix和类Unix系统上,MySQL程序按照指定的顺序从下表中显示的文件中读取启动选项(首先列出的文件优先读取,稍后读取的文件优先)。
On Unix platforms, MySQL ignores configuration files that are world-writable. This is intentional as a security measure.在Unix平台上,MySQL会忽略全局可写的配置文件。这是有意作为一种安全措施。
Table 4.2 Option Files Read on Unix and Unix-Like Systems表4.2 在Unix和类Unix系统上读取选项文件
/etc/my.cnf | |
/etc/mysql/my.cnf | |
| |
$MYSQL_HOME/my.cnf | |
defaults-extra-file | --defaults-extra-file , if any--defaults-extra-file 指定的文件(如果有的话) |
~/.my.cnf | |
~/.mylogin.cnf | |
| SET PERSIST or SE PERSIST_ONLY (server only)SET PERSIST 或SE PERSIST_ONLY (仅限服务器)持久化系统变量 |
In the preceding table, 在上表中,~
represents the current user's home directory (the value of $HOME
).~
表示当前用户的主目录($HOME
的值)。
SYSCONFDIR
represents the directory specified with the SYSCONFDIR
option to CMake when MySQL was built. SYSCONFDIR
表示在构建MySQL时,使用SYSCONFDIR
选项为CMake指定的目录。By default, this is the 默认情况下,这是位于已编译安装目录下的etc
directory located under the compiled-in installation directory.etc
目录。
MYSQL_HOME
is an environment variable containing the path to the directory in which the server-specific my.cnf
file resides. If MYSQL_HOME
is not set and you start the server using the mysqld_safe program, mysqld_safe sets it to BASEDIR
, the MySQL base installation directory.MY_RESHOME
是一个环境变量,包含服务器特定my.cnf
文件所在目录的路径。如果未设置MYSQL_HOME
,并且您使用mysqld_safe
程序启动服务器,mysqld_safe
会将其设置为MYSQL基本安装目录BASEDIR
。
DATADIR
represents the MySQL data directory. DATADIR
代表MySQL数据目录。As used to find 用于查找mysqld-auto.cnf
, its default value is the data directory location built in when MySQL was compiled, but can be changed by --datadir
specified as an option-file or command-line option processed before mysqld-auto.cnf
is processed.mysqld-auto.cnf
的默认值是编译MySQL时内置的数据目录位置,但可以通过在处理mysqld-auto.cnf
之前将--datadir
指定为选项文件或命令行选项来更改。
If multiple instances of a given option are found, the last instance takes precedence, with one exception: For mysqld, the first instance of the 如果找到给定选项的多个实例,则最后一个实例优先,但有一个例外:对于mysqld,--user
option is used as a security precaution, to prevent a user specified in an option file from being overridden on the command line.--user
选项的第一个实例用作安全预防措施,以防止在选项文件中指定的用户在命令行上被覆盖。
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, and mysqld-auto.cnf
, which the server creates in JSON format..mylogin.cnf
和mysqld-auto.cnf
,前者是使用mysql_config_editor创建的,并且是加密的,后者是服务器以JSON格式创建的。
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. 在运行MySQL程序时,命令行上可能给出的任何长选项也可以在选项文件中给出。To get the list of available options for a program, run it with the 要获取程序的可用选项列表,请使用--help
option. --help
选项运行它。(For mysqld, use (对于mysqld,使用--verbose
and --help
.)--verbose
和--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”). 在选项文件中指定选项的语法类似于命令行语法(请参阅第4.2.2.1节,“在命令行上使用选项”)。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
--quick
和--host=localhost
应在选项文件的单独行中指定为quick
和host=localhost
。要在选项文件中指定格式为--loose-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 注释行以#
or ;
. 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.group
是要为其设置选项的程序或组的名称。 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
。
opt_name
=value
This is equivalent to 这相当于命令行上的--
on the command line. opt_name
=value
--opt_name=value
。In an option file, you can have spaces around the 在选项文件中,=
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 您可以在选项值中使用转义序列\b
, \t
, \n
, \r
, \\
, and \s
in option values to represent the backspace, tab, newline, carriage return, backslash, and space characters. In option files, these escaping rules apply:\b
、\t
、\n
、\r
、\\
和\s
来表示退格符、制表符、换行符、回车符、反斜杠和空格字符。在选项文件中,这些转义规则适用:
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.\s
被转换为空格。
A backslash not followed by a valid escape sequence character remains unchanged. 反斜杠后面没有有效的转义序列字符,保持不变。For example, 例如,\S
is retained as is.\S
保持原样。
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. 选项文件中的转义序列规则与SQL语句中字符串文字中的转义顺序规则略有不同。In the latter context, if “在后一种情况下,如果“x
” is not a valid escape sequence character, \
becomes “x
x
” rather than \
. x
x
”不是有效的转义序列字符,则\x
变为“x
”而不是\x
。See Section 9.1.1, “String Literals”.请参阅第9.1.1节,“字符串文字”。
The escaping rules for option file values are especially pertinent for Windows path names, which use 选项文件值的转义规则与Windows路径名特别相关,Windows路径名使用\
as a path name separator. \
作为路径名分隔符。A separator in a Windows path name must be written as 如果Windows路径名中的分隔符后面跟有转义序列字符,则必须写为\\
if it is followed by an escape sequence character. \\
。It can be written as 如果不是,它可以写成\\
or \
if it is not. \\
或\
。Alternatively, 或者,/可以用于Windows路径名,并被视为/
may be used in Windows path names and is treated as \
. \
。Suppose that you want to specify a base directory of 假设您想在选项文件中指定C:\Program Files\MySQL\MySQL Server 8.0
in an option file. This can be done several ways. Some examples:C:\Program Files\MySQL\MySQL Server 8.0
的基目录。这可以通过多种方式实现。一些例子:
basedir="C:\Program Files\MySQL\MySQL Server 8.0" basedir="C:\\Program Files\\MySQL\\MySQL Server 8.0" basedir="C:/Program Files/MySQL/MySQL Server 8.0" basedir=C:\\Program\sFiles\\MySQL\\MySQL\sServer\s8.0
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.[mysqld]
和[mysql]
组分别适用于mysqld服务器和mysql客户端程序。
The [client]
option group is read by all client programs provided in MySQL distributions (but not by mysqld). [client]
选项组由MySQL发行版中提供的所有客户端程序读取(但不由mysqld读取)。To understand how third-party client programs that use the C API can use option files, see the C API documentation at mysql_options().要了解使用C API的第三方客户端程序如何使用选项文件,请参阅mysql_options()
上的C API文档。
The [client]
group enables you to specify options that apply to all clients. [client]
组允许您指定适用于所有客户端的选项。For example, 例如,[client]
is the appropriate group to use to specify the password for connecting to the server. [client]
是用于指定连接到服务器的密码的适当组。(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. [client]
组。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. [client]
组更通用,因为它被所有客户端程序读取,而一个[mysqldump]
组只被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.[client]
、[mysqldump]
的顺序排列,可以使mysqldump特定的选项覆盖[client]
选项。
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=128M [mysqldump] quick
Here is a typical user option file:以下是一个典型的用户选项文件:
[client] # The following password is 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 要创建特定MySQL版本系列的mysqld服务器只读的选项组,请使用名称为[mysqld-5.7]
, [mysqld-8.0]
, and so forth. [mysqld-5.7]
、[mysqld-8.0]
等的组。The following group indicates that the 以下组表示sql_mode
setting should be used only by MySQL servers with 8.0.x version numbers:sql_mode
设置应仅由版本号为8.0x的MySQL服务器使用:
[mysqld-8.0] 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. !include
指令以包含其他选项文件,使用!includedir
指令用于在特定目录中搜索选项文件。For example, to include the 例如,要包含/home/mydir/myopt.cnf
file, use the following directive:/home/mydir/myopt.cnf
文件,请使用以下指令:
!include /home/mydir/myopt.cnf
To search the 要搜索/home/mydir
directory and read option files found there, use this directive:/home/mydir
目录并读取在其中找到的选项文件,请使用以下指令:
!includedir /home/mydir
MySQL makes no guarantee about the order in which option files in the directory are read.MySQL无法保证读取目录中选项文件的顺序。
Any files to be found and included using the 使用!includedir
directive on Unix operating systems must have file names ending in .cnf
. !includedir
指令找到并包含的任何文件指令在Unix操作系统上的文件名必须以.cnf
结尾。On Windows, this directive checks for files with the 在Windows上,此指令检查扩展名为.ini
or .cnf
extension..ini
或.cnf
的文件。
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
][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:.my.cnf
文件包含以下行:
!include /home/mydir/myopt.cnf
And suppose that 假设/home/mydir/myopt.cnf
looks like this:/home/mydir/mymot.cnf
看起来是这样的:
[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. my.cnf
是由mysqld处理的,则只使用/home/mydir/mymot.cnf
中的[mysqld]
组。If the file is processed by mysqladmin, only the 如果文件由mysqladmin处理,则只使用[mysqladmin]
group is used. [mysqladmin]
组。If the file is processed by any other program, no options in 如果文件由任何其他程序处理,则不会使用/home/mydir/myopt.cnf
are used./home/mydir/myt.cnf
中的任何选项。
The 这个!includedir
directive is processed similarly except that all option files in the named directory are read.!includedir
指令的处理方式类似,只是读取命名目录中的所有选项文件。
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.!include
或!includedir
指令,则无论选项文件出现在文件中的什么位置,只要处理选项文件,都会处理由这些指令命名的文件。
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
read the option file myopts.ini
:my.ini
中提供的以下语句读取选项文件myopts.ini
:
!include C:/ProgramData/MySQL/MySQL Server/myopts.ini !include C:\ProgramData\MySQL\MySQL Server\myopts.ini !include C:\\ProgramData\\MySQL\\MySQL Server\\myopts.ini
On Windows, if 在Windows上,如果!include
is the last line in the file, make sure that a newline is appended at the end; otherwise, the line is ignored./path/to/extra.ini
!include /path/to/extra.ini
是文件中的最后一行,请确保在末尾附加一行换行符;否则,该行将被忽略。