On Windows, the recommended way to run MySQL is to install it as a Windows service, so that MySQL starts and stops automatically when Windows starts and stops. 在Windows上,建议将MySQL作为Windows服务安装,以便在Windows启动和停止时MySQL自动启动和停止。A MySQL server installed as a service can also be controlled from the command line using NET commands, or with the graphical Services utility. Generally, to install MySQL as a Windows service you should be logged in using an account that has administrator rights.作为服务安装的MySQL服务器也可以使用NET命令或图形服务实用程序从命令行进行控制。通常,要将MySQL作为Windows服务安装,您应该使用具有管理员权限的帐户登录。
The Services utility (the Windows Service Control Manager) can be found in the Windows Control Panel. 服务实用程序(Windows服务控制管理器)可以在Windows控制面板中找到。To avoid conflicts, it is advisable to close the Services utility while performing server installation or removal operations from the command line.为避免冲突,建议在从命令行执行服务器安装或删除操作时关闭服务实用工具。
Before installing MySQL as a Windows service, you should first stop the current server if it is running by using the following command:在将MySQL安装为Windows服务之前,如果当前服务器正在运行,则应首先使用以下命令停止该服务器:
C:\>"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqladmin"
-u root shutdown
If the MySQL 如果MySQLroot
user account has a password, you need to invoke mysqladmin with the -p
option and supply the password when prompted.root
用户帐户有密码,则需要使用-p
选项调用mysqladmin,并在提示时提供密码。
This command invokes the MySQL administrative utility mysqladmin to connect to the server and tell it to shut down. 这个命令调用MySQL管理实用程序mysqladmin来连接到服务器并告诉它关闭。The command connects as the MySQL 该命令以MySQL root用户的身份连接,MySQL root
user, which is the default administrative account in the MySQL grant system.root
用户是MySQL授予系统中的默认管理帐户。
Users in the MySQL grant system are wholly independent from any operating system users under Windows.MySQL授权系统中的用户完全独立于Windows下的任何操作系统用户。
Install the server as a service using this command:使用以下命令将服务器作为服务安装:
C:\> "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld" --install
The service-installation command does not start the server. 服务安装命令不会启动服务器。Instructions for that are given later in this section.这方面的说明将在本节后面给出。
To make it easier to invoke MySQL programs, you can add the path name of the MySQL 为了更容易地调用MySQL程序,您可以将MySQL bin
directory to your Windows system PATH
environment variable:bin
目录的路径名添加到您的Windows系统PATH
环境变量中:
On the Windows desktop, right-click the My Computer icon, and select .在Windows桌面上,右键单击“我的电脑”图标,然后选择“属性”。
Next select the 接下来,从显示的“系统属性”菜单中选择“高级”选项卡,然后单击“环境变量”按钮。 tab from the menu that appears, and click the button.
Under System Variables, select , and then click the button. The dialogue should appear.在“系统变量”下,选择“路径”,然后单击“编辑”按钮。此时应出现编辑系统变量对话框。
Place your cursor at the end of the text appearing in the space marked Variable Value. 将游标放在标记为“变量值”的空格中显示的文本的末尾。(Use the End key to ensure that your cursor is positioned at the very end of the text in this space.) (使用End键确保游标位于该空间中文本的最后。)Then enter the complete path name of your MySQL 然后输入MySQL bin
directory (for example, C:\Program Files\MySQL\MySQL Server 8.0\bin
), and there should be a semicolon separating this path from any values present in this field. bin
目录的完整路径名(例如,C:\Program Files\MySQL\MySQL Server 8.0\bin
),应该用分号将该路径与该字段中的任何值分隔开。Dismiss this dialogue, and each dialogue in turn, by clicking 单击“确定”,直到所有打开的对话都被取消,从而取消此对话,并依次取消每个对话。 until all of the dialogues that were opened have been dismissed. You should now be able to invoke any MySQL executable program by typing its name at the DOS prompt from any directory on the system, without having to supply the path. 现在,您应该能够通过在DOS提示符下从系统上的任何目录键入任何MySQL可执行程序的名称来调用该程序,而无需提供路径。This includes the servers, the mysql client, and all MySQL command-line utilities such as mysqladmin and mysqldump.这包括服务器、mysql客户端和所有mysql命令行实用程序,如mysqladmin和mysqldump。
You should not add the MySQL 如果在同一台机器上运行多个MySQL服务器,则不应将MySQL bin
directory to your Windows PATH
if you are running multiple MySQL servers on the same machine.bin
目录添加到Windows PATH
中。
You must exercise great care when editing your system 手动编辑系统PATH时,您必须非常小心;意外删除或修改现有PATH值的任何部分都可能导致系统出现故障甚至无法使用。PATH
by hand; accidental deletion or modification of any portion of the existing PATH
value can leave you with a malfunctioning or even unusable system.
The following additional arguments can be used when installing the service:安装服务时可以使用以下附加参数:
You can specify a service name immediately following the 您可以在--install
option. The default service name is MySQL
.--install
选项后面立即指定服务名称。默认服务名称为MySQL
。
If a service name is given, it can be followed by a single option. By convention, this should be 如果给定了一个服务名称,后面可以跟一个选项。按照惯例,这应该是--defaults-file=
to specify the name of an option file from which the server should read options when it starts.file_name
--defaults-file=file_name
,以指定服务器启动时应从中读取选项的选项文件的名称。
The use of a single option other than 可以使用--defaults-file
is possible but discouraged. --defaults-file
is more flexible because it enables you to specify multiple startup options for the server by placing them in the named option file.--defaults-file
以外的单个选项,但不鼓励使用--defaults-file
更灵活,因为它允许您通过将服务器的多个启动选项放置在命名选项文件中来指定它们。
You can also specify a 您还可以在服务名称后面指定--local-service
option following the service name. --local-service
选项。This causes the server to run using the 这会导致服务器使用具有有限系统权限的LocalService
Windows account that has limited system privileges. LocalService
Windows帐户运行。If both 如果在服务名称后面同时提供--defaults-file
and --local-service
are given following the service name, they can be in any order.--defaults-file
和--local-service
,它们可以按任何顺序排列。
For a MySQL server that is installed as a Windows service, the following rules determine the service name and option files that the server uses:对于作为Windows服务安装的MySQL服务器,以下规则确定服务器使用的服务名称和选项文件:
If the service-installation command specifies no service name or the default service name (如果服务安装命令没有指定服务名称或MySQL
) following the --install
option, the server uses the service name of MySQL
and reads options from the [mysqld]
group in the standard option files.--install
选项后面的默认服务名称(MySQL
),则服务器将使用MySQL
的服务名称,并从标准选项文件中的[mysqld]
组中读取选项。
If the service-installation command specifies a service name other than 如果服务安装命令在MySQL
following the --install
option, the server uses that service name. --install
选项后面指定了MySQL
以外的服务名称,则服务器将使用该服务名称。It reads options from the 它从[mysqld]
group and the group that has the same name as the service in the standard option files. [mysqld]
组和与标准选项文件中的服务同名的组中读取选项。This enables you to use the 这使您能够将[mysqld]
group for options that should be used by all MySQL services, and an option group with the service name for use by the server installed with that service name.[mysqld]
组用于所有MySQL服务应使用的选项,并将具有服务名称的选项组用于使用该服务名称安装的服务器。
If the service-installation command specifies a 如果服务安装命令在服务名称后指定--defaults-file
option after the service name, the server reads options the same way as described in the previous item, except that it reads options only from the named file and ignores the standard option files.--defaults-file
选项,则服务器读取选项的方式与上一项中所述相同,只是它只从命名文件中读取选项,而忽略标准选项文件。
As a more complex example, consider the following command:作为一个更复杂的示例,请考虑以下命令:
C:\>"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld"
--install MySQL --defaults-file=C:\my-opts.cnf
Here, the default service name (这里,默认服务名称(MySQL
) is given after the --install
option. MySQL
)在--install
选项之后给出。If no 如果没有提供--defaults-file
option had been given, this command would have the effect of causing the server to read the [mysqld]
group from the standard option files. --defaults-file
选项,则此命令将导致服务器从标准选项文件中读取[mysqld]
组。However, because the 但是,由于存--defaults-file
option is present, the server reads options from the [mysqld]
option group, and only from the named file.--defaults-file
选项,服务器将从[mysqld]
选项组中读取选项,并且仅从命名文件中读取选项。
On Windows, if the server is started with the 在Windows上,如果服务器是使用--defaults-file
and --install
options, --install
must be first. Otherwise, mysqld.exe
attempts to start the MySQL server.--defaults-file
和--install
选项启动的,则必须首先使用--install
。否则,mysqld.exe
将尝试启动MySQL服务器。
You can also specify options as Start parameters in the Windows Services utility before you start the MySQL service.在启动MySQL服务之前,您还可以在Windows服务实用程序中将选项指定为“启动”参数。
Finally, before trying to start the MySQL service, make sure the user variables 最后,在尝试启动MySQL服务之前,请确保要运行该服务的操作系统用户的用户变量%TEMP%
and %TMP%
(and also %TMPDIR%
, if it has ever been set) for the operating system user who is to run the service are pointing to a folder to which the user has write access. %TEMP%
和%TMP%
(以及%TMPDIR%
,如果曾经设置过的话)指向用户具有写访问权限的文件夹。The default user for running the MySQL service is 运行MySQL服务的默认用户是LocalSystem
, and the default value for its %TEMP%
and %TMP%
is C:\Windows\Temp
, a directory LocalSystem
has write access to by default. LocalSystem
,其%TEMP%
和%TMP%
的默认值是C:\Windows\TEMP
,默认情况下LocalSystem
可以写入该目录。However, if there are any changes to that default setup (for example, changes to the user who runs the service or to the mentioned user variables, or the 但是,如果对默认设置有任何更改(例如,对运行服务的用户或上述用户变量的更改,或者使用--tmpdir
option has been used to put the temporary directory somewhere else), the MySQL service might fail to run because write access to the temporary directory has not been granted to the proper user.--tmpdir
选项将临时目录放在其他位置),MySQL服务可能无法运行,因为对临时目录的写访问权未授予适当的用户。
After a MySQL server instance has been installed as a service, Windows starts the service automatically whenever Windows starts. MySQL服务器实例作为服务安装后,每当Windows启动时,Windows都会自动启动该服务。The service also can be started immediately from the Services utility, or by using an sc start 该服务也可以立即从服务实用程序启动,或者使用mysqld_service_name
or NET START mysqld_service_name
command. sc start mysqld_service_name
或NET START mysqld_servicename
命令启动。SC and NET commands are not case-sensitive.SC和NET命令不区分大小写。
When run as a service, mysqld has no access to a console window, so no messages can be seen there. 当作为服务运行时,mysqld无法访问控制台窗口,因此在那里看不到消息。If mysqld does not start, check the error log to see whether the server wrote any messages there to indicate the cause of the problem. 如果mysqld没有启动,请检查错误日志,查看服务器是否在其中写入了任何消息来指示问题的原因。The error log is located in the MySQL data directory (for example, 错误日志位于MySQL数据目录中(例如C:\Program Files\MySQL\MySQL Server 8.0\data
). C:\Program Files\MySQL\MySQL Server 8.0\data
)。It is the file with a suffix of 它是后缀为.err
..err
的文件。
When a MySQL server has been installed as a service, and the service is running, Windows stops the service automatically when Windows shuts down. 当MySQL服务器已作为服务安装,并且该服务正在运行时,Windows会在关闭时自动停止该服务。The server also can be stopped manually using the 也可以使用服务实用程序、Services
utility, the sc stop mysqld_service_name
command, the NET STOP mysqld_service_name
command, or the mysqladmin shutdown command.sc stop mysqld_service_name
命令、NET STOP mysqld_service_name
命令或mysqladmin shutdown
命令手动停止服务器。
You also have the choice of installing the server as a manual service if you do not wish for the service to be started automatically during the boot process. 如果您不希望服务在引导过程中自动启动,您也可以选择将服务器作为手动服务安装。To do this, use the 要执行此操作,请使用--install-manual
option rather than the --install
option:--install-manual
选项,而不是--install
选项:
C:\> "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld" --install-manual
To remove a server that is installed as a service, first stop it if it is running by executing SC STOP 要删除作为服务安装的服务器,如果它正在运行,请首先通过执行mysqld_service_name
or NET STOP mysqld_service_name
. SC stop mysqld_service_name
或NET stop mysqld_services_name
来停止它。Then use SC DELETE 然后使用mysqld_service_name
to remove it:SC DELETE mysqld_service_name
将其删除:
C:\> SC DELETE mysql
Alternatively, use the mysqld 或者,使用mysqld --remove
option to remove the service.--remove
选项来删除服务。
C:\> "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld" --remove
If mysqld is not running as a service, you can start it from the command line. 如果mysqld不是作为服务运行的,可以从命令行启动它。For instructions, see Section 2.3.4.6, “Starting MySQL from the Windows Command Line”.有关说明,请参阅第2.3.4.6节,“从Windows命令行启动MySQL”。
If you encounter difficulties during installation, see Section 2.3.5, “Troubleshooting a Microsoft Windows MySQL Server Installation”.如果您在安装过程中遇到困难,请参阅第2.3.5节,“Microsoft Windows MySQL Server安装疑难解答”。
For more information about stopping or removing a Windows service, see Section 5.8.2.2, “Starting Multiple MySQL Instances as Windows Services”.有关停止或删除Windows服务的更多信息,请参阅第5.8.2.2节,“将多个MySQL实例作为Windows服务启动”。