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 NamePurpose
/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)

In the preceding table, ~ represents the current user's home directory (the value of $HOME).

SYSCONFDIR represents the directory specified with the SYSCONFDIR option to CMake when MySQL was built. By default, this is the etc directory located under the compiled-in installation directory.

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.

DATADIR represents the MySQL data directory. 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.

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.

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.

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-opt_name in an option file, write it as 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. 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=value

    This is equivalent to --opt_name=value on the command line. 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:

  • 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, \x becomes x rather than \x. See Section 9.1.1, “String Literals”.

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 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:

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.

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 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=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. 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是文件中的最后一行,请确保在末尾附加一行换行符;否则,该行将被忽略。