4.2.2.2 Using Option Files使用选项文件

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 --verbose and --help.) (对于mysqld,请使用--verbose--help。)If the program reads option files, the help message indicates which files it looks for and which option groups it recognizes.如果程序读取选项文件,则帮助消息会指示它要查找哪些文件以及可以识别哪些选项组。

Note注意

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:许多选项文件都是使用任何文本编辑器创建的纯文本文件。例外情况是:

Option File Processing Order选项文件处理顺序

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按照以下讨论中描述的顺序查找选项文件,并读取任何存在的选项文件。如果您要使用的选项文件不存在,请使用刚才讨论的适当方法创建它。

Note注意

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系统上读取选项文件

File Name文件名Purpose意图
%WINDIR%\my.ini, %WINDIR%\my.cnfGlobal options全局选项
C:\my.ini, C:\my.cnfGlobal options全局选项
BASEDIR\my.ini, BASEDIR\my.cnfGlobal options全局选项
defaults-extra-fileThe file specified with --defaults-extra-file, if any--defaults-extra-file指定的文件(如果有的话)
%APPDATA%\MySQL\.mylogin.cnfLogin path options (clients only)登录路径选项(仅限客户端)
DATADIR\mysqld-auto.cnfSystem variables persisted with SET PERSIST or SET PERSIST_ONLY (server only)使用SET PERSISTSET PERSIST_ONLY持久化的系统变量(仅限服务器)

In the preceding table, %WINDIR% represents the location of your Windows directory. 在上表中,%WINDIR%表示Windows目录的位置。This is commonly C:\WINDOWS. 这通常是C:\WINDOWSUse 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 C:\PROGRAMDIR\MySQL\MySQL 8.0 Server where PROGRAMDIR represents the programs directory (usually Program Files on English-language versions of Windows), See Section 2.3.3, “MySQL Installer for Windows”.当使用MySQL安装程序安装MySQL 8.0时,通常是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程序按照指定的顺序从下表中显示的文件中读取启动选项(首先列出的文件优先读取,稍后读取的文件优先)。

Note注意

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系统上读取选项文件

File Name文件名Purpose目的
/etc/my.cnfGlobal options全局选项
/etc/mysql/my.cnfGlobal options全局选项
SYSCONFDIR/my.cnfGlobal options全局选项
$MYSQL_HOME/my.cnfServer-specific options (server only)特定于服务器的选项(仅限服务器)
defaults-extra-fileThe file specified with --defaults-extra-file, if any--defaults-extra-file指定的文件(如果有的话)
~/.my.cnfUser-specific options用户特定选项
~/.mylogin.cnfUser-specific login path options (clients only)用户特定的登录路径选项(仅限客户端)
DATADIR/mysqld-auto.cnfSystem variables persisted with SET PERSIST or SE PERSIST_ONLY (server only)使用SET PERSISTSE 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 --user option is used as a security precaution, to prevent a user specified in an option file from being overridden on the command line.如果找到给定选项的多个实例,则最后一个实例优先,但有一个例外:对于mysqld--user选项的第一个实例用作安全预防措施,以防止在选项文件中指定的用户在命令行上被覆盖。

Option File Syntax选项文件语法

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.cnfmysqld-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 --verbose and --help.)(对于mysqld,使用--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-opt_name in an option file, write it as loose-opt_name.例如,命令行上的--quick--host=localhost应在选项文件的单独行中指定为quickhost=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 --opt_name on the command line.这相当于命令行上的--opt_name

  • opt_name=value

    This is equivalent to --opt_name=value on the command line. 这相当于命令行上的--opt_name=valueIn 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, \x becomes x rather than \x. 在后一种情况下,如果“x”不是有效的转义序列字符,则\x变为“x”而不是\xSee 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 \ as a path name separator. 选项文件值的转义规则与Windows路径名特别相关,Windows路径名使用\作为路径名分隔符。A separator in a Windows path name must be written as \\ if it is followed by an escape sequence character. 如果Windows路径名中的分隔符后面跟有转义序列字符,则必须写为\\It can be written as \\ or \ if it is not. 如果不是,它可以写成\\\Alternatively, / may be used in Windows path names and is treated as \. 或者,/可以用于Windows路径名,并被视为\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 [mysqld-5.7], [mysqld-8.0], and so forth. 要创建特定MySQL版本系列的mysqld服务器只读的选项组,请使用名称为[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
Option File Inclusions选项文件包含

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无法保证读取目录中选项文件的顺序。

Note注意

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 .ini or .cnf extension.在Windows上,此指令检查扩展名为.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 [group] 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:在处理包含的文件时,仅使用当前程序正在查找的组中的那些选项。其他组将被忽略。假设.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] group is used. 如果文件由mysqladmin处理,则只使用[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 !include /path/to/extra.ini is the last line in the file, make sure that a newline is appended at the end; otherwise, the line is ignored.在Windows上,如果!include /path/to/extra.ini是文件中的最后一行,请确保在末尾附加一行换行符;否则,该行将被忽略。