2.3.4.2 Creating an Option File创建选项文件

If you need to specify startup options when you run the server, you can indicate them on the command line or place them in an option file. 如果在运行服务器时需要指定启动选项,可以在命令行中指示它们,也可以将它们放在选项文件中。For options that are used every time the server starts, you may find it most convenient to use an option file to specify your MySQL configuration. This is particularly true under the following circumstances:对于每次服务器启动时使用的选项,您可能会发现使用选项文件来指定MySQL配置是最方便的。在以下情况下尤其如此:

When the MySQL server starts on Windows, it looks for option files in several locations, such as the Windows directory, C:\, and the MySQL installation directory (for the full list of locations, see Section 4.2.2.2, “Using Option Files”). 当MySQL服务器在Windows上启动时,它会在多个位置查找选项文件,如Windows目录C:\和MySQL安装目录(有关位置的完整列表,请参阅第4.2.2.2节,“使用选项文件”)。The Windows directory typically is named something like C:\WINDOWS. Windows目录的名称通常类似于C:\WindowsYou can determine its exact location from the value of the WINDIR environment variable using the following command:可以使用以下命令根据WINDIR环境变量的值确定其确切位置:

C:\> echo %WINDIR%

MySQL looks for options in each location first in the my.ini file, and then in the my.cnf file. MySQL首先在my.ini文件中的每个位置查找选项,然后在my.cnf文件中查找选项。However, to avoid confusion, it is best if you use only one file. 但是,为了避免混淆,最好只使用一个文件。If your PC uses a boot loader where C: is not the boot drive, your only option is to use the my.ini file. 如果您的电脑使用的是C:不是引导驱动器的引导加载程序,那么您唯一的选择就是使用my.ini文件。Whichever option file you use, it must be a plain text file.无论使用哪种选项文件,都必须是纯文本文件。

Note注意

When using the MySQL Installer to install MySQL Server, it creates the my.ini at the default location, and the user executing MySQL Installer is granted full permissions to this new my.ini file.当使用MySQL安装程序安装MySQL Server时,它会在默认位置创建my.ini,并且执行MySQL安装程序的用户会被授予对这个新的my.ini文件的完全权限。

In other words, be sure that the MySQL Server user has permission to read the my.ini file.换句话说,请确保MySQL Server用户具有读取my.ini文件的权限。

You can also make use of the example option files included with your MySQL distribution; see Section 5.1.2, “Server Configuration Defaults”.您还可以使用MySQL发行版中包含的示例选项文件;请参阅第5.1.2节,“服务器配置默认值”

An option file can be created and modified with any text editor, such as Notepad. 选项文件可以使用任何文本编辑器(如记事本)创建和修改。For example, if MySQL is installed in E:\mysql and the data directory is in E:\mydata\data, you can create an option file containing a [mysqld] section to specify values for the basedir and datadir options:例如,如果MySQL安装在E:\mysql中,并且数据目录位于E:\mydata\data中,则可以创建一个包含[mysqld]部分的选项文件,以指定basedirdatadir选项的值:

[mysqld]
# set basedir to your installation path
basedir=E:/mysql
# set datadir to the location of your data directory
datadir=E:/mydata/data

Microsoft Windows path names are specified in option files using (forward) slashes rather than backslashes. If you do use backslashes, double them:Microsoft Windows路径名在选项文件中使用(正)斜杠而不是反斜杠指定。如果使用反斜杠,请将其加倍:

[mysqld]
# set basedir to your installation path
basedir=E:\\mysql
# set datadir to the location of your data directory
datadir=E:\\mydata\\data

The rules for use of backslash in option file values are given in Section 4.2.2.2, “Using Option Files”.选项文件值中反斜杠的使用规则见第4.2.2.2节,“使用选项文件”

The ZIP archive does not include a data directory. ZIP存档不包括data目录。To initialize a MySQL installation by creating the data directory and populating the tables in the mysql system database, initialize MySQL using either --initialize or --initialize-insecure. 要通过创建数据目录并填充MySQL系统数据库中的表来初始化MySQL安装,请使用--initialize--initialize-insecure初始化MySQL。For additional information, see Section 2.10.1, “Initializing the Data Directory”.有关更多信息,请参阅第2.10.1节,“初始化数据目录”

If you would like to use a data directory in a different location, you should copy the entire contents of the data directory to the new location. 如果要在其他位置使用数据目录,则应将data目录的全部内容复制到新位置。For example, if you want to use E:\mydata as the data directory instead, you must do two things:例如,如果要使用E:\mydata作为数据目录,则必须执行两件事:

  1. Move the entire data directory and all of its contents from the default location (for example C:\Program Files\MySQL\MySQL Server 8.0\data) to E:\mydata.将整个data目录及其所有内容从默认位置(例如C:\Program Files\MySQL\MySQL Server 8.0\data)移动到E:\mydata

  2. Use a --datadir option to specify the new data directory location each time you start the server.每次启动服务器时,使用--datadir选项指定新的数据目录位置。