2.3.4.8 Starting MySQL as a Windows Service将MySQL作为Windows服务启动

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.为避免冲突,建议在从命令行执行服务器安装或删除操作时关闭服务实用工具。

Installing the service安装服务

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
Note注意

If the MySQL root user account has a password, you need to invoke mysqladmin with the -p option and supply the password when prompted.如果MySQLroot用户帐户有密码,则需要使用-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 root user, which is the default administrative account in the MySQL grant system.该命令以MySQL root用户的身份连接,MySQL root用户是MySQL授予系统中的默认管理帐户。

Note注意

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 bin directory to your Windows system PATH environment variable:为了更容易地调用MySQL程序,您可以将MySQL bin目录的路径名添加到您的Windows系统PATH环境变量中:

Warning警告

You must exercise great care when editing your system 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.手动编辑系统PATH时,您必须非常小心;意外删除或修改现有PATH值的任何部分都可能导致系统出现故障甚至无法使用。

The following additional arguments can be used when installing the 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服务器,以下规则确定服务器使用的服务名称和选项文件:

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]选项组中读取选项,并且仅从命名文件中读取选项。

Note注意

On Windows, if the server is started with the --defaults-file and --install options, --install must be first. Otherwise, mysqld.exe attempts to start the MySQL server.在Windows上,如果服务器是使用--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 %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. 最后,在尝试启动MySQL服务之前,请确保要运行该服务的操作系统用户的用户变量%TEMP%%TMP%(以及%TMPDIR%,如果曾经设置过的话)指向用户具有写访问权限的文件夹。The default user for running the MySQL service is LocalSystem, and the default value for its %TEMP% and %TMP% is C:\Windows\Temp, a directory LocalSystem has write access to by default. 运行MySQL服务的默认用户是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服务可能无法运行,因为对临时目录的写访问权未授予适当的用户。

Starting the service启动服务

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_nameNET START mysqld_servicename命令启动。SC and NET commands are not case-sensitive.SCNET命令不区分大小写。

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, C:\Program Files\MySQL\MySQL Server 8.0\data). 错误日志位于MySQL数据目录中(例如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
Removing the service删除服务

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_nameNET 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 --remove option to remove the service.或者,使用mysqld --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服务启动”