Running MySQL as a Service Under Windows NT

Paul DuBois

Document revision: 1.01
Last update: 2003-01-23

Table of Contents


When you maintain a MySQL installation, normally you want the MySQL server to run all the time. You can start the server by invoking it yourself, but if the machine restarts, MySQL availability is interrupted until you remember to start the server again. Under operating systems that allow you to modify the system startup procedure, you can include a step that launches your MySQL server. That way, the server runs without intervention on your part even in the event of an unattended machine restart. You can achieve this under Windows NT by installing MySQL as a service that is set to run automatically. NT's service management facilities also allow you to control the service manually if necessary--for example, to stop the service to perform administrative tasks with the server down, then start it again afterward.

The discussion in this article uses "Windows NT" as a generic term that also applies to other NT-based systems, such as Windows 2000 and XP. It does not apply to versions of Windows that do not support services, such as Windows 95, 98, or Me.

If MySQL is not already installed on your system, you can download it from the MySQL AB web site:

Some of the commands described below require you to invoke a server from the command line (the DOS prompt in a console window). The servers are found in the bin directory of your MySQL installation. (By default, the MySQL installation procedure places MySQL in the C:\mysql directory, but you can choose another location if you like.) The following instructions make little assumption about the installation location, other than that the bin directory of your MySQL installation is named in your PATH variable setting. If your PATH variable isn't set to include this directory, you'll need to invoke a MySQL server either by changing directory into the bin directory first or by specifying the server's full pathname, such as C:\mysql\bin\mysqld-nt for the mysqld-nt server.

Installing MySQL as a Service

To install a MySQL server as a service under Windows NT, invoke it with the --install option from the command line. For example, to install mysqld-nt this way, use the following commands:

   C:\> mysqld-nt --install
   C:\> NET START MySql
The first command installs the server and sets it to run at the next system restart and each restart thereafter. However, it does not cause the service to start immediately. The NET START command accomplishes that without a machine restart. The argument to NET START is the service name, which is "MySql" no matter which of the MySQL servers you install. (The service name is not case sensitive, so you can specify it in any lettercase.)

When you install a MySQL server as a service from the command line, normally only the --install option is used. You cannot specify runtime options on the commmand line because you don't invoke the server directly from the DOS prompt. To specify options that the server should use each time it runs thereafter, you must place them in an option file instead. Suppose you want the server to start as though you'd given the --flush and --log=query-log options on the command line. To achieve the same effect using an option file, add the options, one per line, to the [mysqld] group in the file:

When it starts up, the server looks for option files in several locations. The two most common are the my.ini file in the Windows system directory, and C:\my.cnf. You can put the startup options in either file (creating it as a plain text file if it doesn't exist). There are several example option files that you can look at in the top-level directory of your MySQL installation.

Controlling the MySQL Service

A MySQL server that has been installed as an automatic service is run by NT's service management facilities each time the machine starts up. Those facilities also allow you to start or stop the server manually or to check its status. These operations are available both at the command line and through the graphical interface provided by the Services Manager.

Controlling the Service From the Command Line

To start or stop the MySQL service from the command line, use these commands:

   C:\> NET START MySql
   C:\> NET STOP MySql
To check the server's current status, use the following command. If the server is running, the service name "MySql" will appear in the list of services displayed by the command:
Another way to stop the server that does not involve NT's service management commands is to use mysqladmin:
   C:\> mysqladmin -p -u root shutdown

Controlling the Service Using the Services Manager

To control the server using a graphical interface, launch the Services Manager. You can find this as the Services item in the Control Panel or in the Administrative Tools item in the Control Panel. The Services Manager displays information like that shown in Figure 1. (Your particular version may have a somewhat different appearance.)

Figure 1. The Services Manager

The MySQL service line will contain the word Started if the server is running, and also includes the service startup mode. The mode is either Automatic (service runs automatically as part of the system startup procedure), Manual (service is started manually), or Disabled (service does not run at all).

To control the MySQL service, select the MySQL line from the service list on the left side of the Services window, then click the appropriate button:

Note that if the service startup mode is Disabled, you won't be able to do anything with the service until you set the mode to Automatic or Manual. For example, to set the service for automatic startup, select the Startup button, then select the Automatic option in the window that appears and click OK to confirm the choice.

If you perform service operations from the command line while the Services Manager is running, it will not notice the effect of those operations. For example, if you install or remove the MySQL service by invoking a server with the --install or --remove options, Services Manager doesn't update its service list. Similarly, if you run commands such as NET START or NET STOP at the DOS prompt, Services Manager doesn't notice changes to the service status. To avoid confusion, close Services Manager before performing service operations from the command line, then reopen it. That way, information that the Services Manager displays will be up-to-date.

Removing the MySQL Service

If you decide you no longer want to run MySQL as a service, remove it by invoking the server with the --remove option. However, before you do this, you should stop the server if it's currently running. For example, if you installed mysqld-nt, stop it and remove it like this:

   C:\> NET STOP MySql
   C:\> mysqld-nt --remove
You should also remove the service if you want to switch servers, because you can't install a second server as a service without first removing the existing one. Suppose you're using mysqld-nt and want to switch to mysqld-max-nt, which includes support for additional features. Run the following commands to stop and remove the service, install the new server, and restart the service:
   C:\> NET STOP MySql
   C:\> mysqld-nt --remove
   C:\> mysqld-max-nt --install
   C:\> NET START MySql
The preceding instructions actually are a bit overspecific. It doesn't really matter which server you use to remove the service. You can invoke any of them with the --remove option, no matter which server is installed. For example, if you have installed mysqld-nt, you can remove it using mysqld.


The original version of this article was written for NuSphere Corporation. The current version is an updated revision of the original.