<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="windows-start-service">
      </a>
      2.3.3.8 Starting MySQL as a Windows Service
     </h4>
    </div>
   </div>
  </div>
  <p>
   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. A MySQL server installed as a
        service can also be controlled from the command line using
   <span class="command">
    <strong>
     NET
    </strong>
   </span>
   commands, or with the graphical
   <span class="command">
    <strong>
     Services
    </strong>
   </span>
   utility. Generally, to install MySQL
        as a Windows service you should be logged in using an account
        that has administrator rights.
  </p>
  <p>
   The
   <span class="command">
    <strong>
     Services
    </strong>
   </span>
   utility (the Windows
   <span class="command">
    <strong>
     Service Control Manager
    </strong>
   </span>
   ) can be found in the
        Windows Control Panel. To avoid conflicts, it is advisable to
        close the
   <span class="command">
    <strong>
     Services
    </strong>
   </span>
   utility while performing
        server installation or removal operations from the command line.
  </p>
  <h5>
   <a name="idm46045331997104">
   </a>
   Installing the service
  </h5>
  <p>
   Before installing MySQL as a Windows service, you should first
        stop the current server if it is running by using the following
        command:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa61479605"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span> <span class="token atrule">"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqladmin"</span>
          <span class="token property">-u</span> root shutdown</code></pre>
  </div>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    If the MySQL
    <code class="literal">
     root
    </code>
    user account has a
          password, you need to invoke
    <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
     <span class="command">
      <strong>
       mysqladmin
      </strong>
     </span>
    </a>
    with the
    <code class="option">
     -p
    </code>
    option and supply the password
          when prompted.
   </p>
  </div>
  <p>
   This command invokes the MySQL administrative utility
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin
     </strong>
    </span>
   </a>
   to connect to the server and tell
        it to shut down. The command connects as the MySQL
   <code class="literal">
    root
   </code>
   user, which is the default
        administrative account in the MySQL grant system.
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    Users in the MySQL grant system are wholly independent from
          any operating system users under Windows.
   </p>
  </div>
  <p>
   Install the server as a service using this command:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa26248586"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span> <span class="token atrule">"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqld"</span> <span class="token property">--install</span></code></pre>
  </div>
  <p>
   The service-installation command does not start the server.
        Instructions for that are given later in this section.
  </p>
  <p>
   To make it easier to invoke MySQL programs, you can add the path
        name of the MySQL
   <code class="filename">
    bin
   </code>
   directory to your
        Windows system
   <code class="literal">
    PATH
   </code>
   environment variable:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      On the Windows desktop, right-click the
      <span class="guiicon">
       My
            Computer
      </span>
      icon, and select
      <span class="guimenuitem">
       Properties
      </span>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Next select the
      <span class="guimenuitem">
       Advanced
      </span>
      tab from
            the
      <span class="guimenu">
       System Properties
      </span>
      menu that appears,
            and click the
      <span class="guibutton">
       Environment Variables
      </span>
      button.
     </p>
    </li>
    <li class="listitem">
     <p>
      Under
      <span class="guilabel">
       System Variables
      </span>
      , select
      <span class="guimenuitem">
       Path
      </span>
      , and then click the
      <span class="guibutton">
       Edit
      </span>
      button. The
      <span class="guimenu">
       Edit System
            Variable
      </span>
      dialogue should appear.
     </p>
    </li>
    <li class="listitem">
     <p>
      Place your cursor at the end of the text appearing in the
            space marked
      <span class="guilabel">
       Variable Value
      </span>
      . (Use the
      <span class="keycap">
       <strong>
        End
       </strong>
      </span>
      key to ensure that your cursor is
            positioned at the very end of the text in this space.) Then
            enter the complete path name of your MySQL
      <code class="filename">
       bin
      </code>
      directory (for example,
      <code class="literal">
       C:\Program Files\MySQL\MySQL Server
            8.4\bin
      </code>
      ), and there should be a
            semicolon separating this path from any values present in
            this field. Dismiss this dialogue, and each dialogue in
            turn, by clicking
      <span class="guibutton">
       OK
      </span>
      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. This includes the
            servers, the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, and all MySQL
            command-line utilities such as
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin
        </strong>
       </span>
      </a>
      and
      <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
       <span class="command">
        <strong>
         mysqldump
        </strong>
       </span>
      </a>
      .
     </p>
     <p>
      You should not add the MySQL
      <code class="filename">
       bin
      </code>
      directory to your Windows
      <code class="literal">
       PATH
      </code>
      if you are
            running multiple MySQL servers on the same machine.
     </p>
    </li>
   </ul>
  </div>
  <div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Warning
   </div>
   <p>
    You must exercise great care when editing your system
    <code class="literal">
     PATH
    </code>
    by hand; accidental deletion or
          modification of any portion of the existing
    <code class="literal">
     PATH
    </code>
    value can leave you with a
          malfunctioning or even unusable system.
   </p>
  </div>
  <p>
   The following additional arguments can be used when installing
        the service:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      You can specify a service name immediately following the
      <code class="option">
       --install
      </code>
      option. The default service name
            is
      <code class="literal">
       MySQL
      </code>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      If a service name is given, it can be followed by a single
            option. By convention, this should be
      <a class="link" href="option-file-options.html#option_general_defaults-file">
       <code class="option">
        --defaults-file=
        <em class="replaceable">
         <code>
          file_name
         </code>
        </em>
       </code>
      </a>
      to specify the name of an option file from which the server
            should read options when it starts.
     </p>
     <p>
      The use of a single option other than
      <a class="link" href="option-file-options.html#option_general_defaults-file">
       <code class="option">
        --defaults-file
       </code>
      </a>
      is possible
            but discouraged.
      <a class="link" href="option-file-options.html#option_general_defaults-file">
       <code class="option">
        --defaults-file
       </code>
      </a>
      is more
            flexible because it enables you to specify multiple startup
            options for the server by placing them in the named option
            file.
     </p>
    </li>
    <li class="listitem">
     <p>
      You can also specify a
      <code class="option">
       --local-service
      </code>
      option following the service name. This causes the server to
            run using the
      <code class="literal">
       LocalService
      </code>
      Windows
            account that has limited system privileges. If both
      <a class="link" href="option-file-options.html#option_general_defaults-file">
       <code class="option">
        --defaults-file
       </code>
      </a>
      and
      <code class="option">
       --local-service
      </code>
      are given following the
            service name, they can be in any order.
     </p>
    </li>
   </ul>
  </div>
  <p>
   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:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If the service-installation command specifies no service
            name or the default service name (
      <code class="literal">
       MySQL
      </code>
      )
            following the
      <code class="option">
       --install
      </code>
      option, the server
            uses the service name of
      <code class="literal">
       MySQL
      </code>
      and reads
            options from the
      <code class="literal">
       [mysqld]
      </code>
      group in the
            standard option files.
     </p>
    </li>
    <li class="listitem">
     <p>
      If the service-installation command specifies a service name
            other than
      <code class="literal">
       MySQL
      </code>
      following the
      <code class="option">
       --install
      </code>
      option, the server uses that
            service name. It reads options from the
      <code class="literal">
       [mysqld]
      </code>
      group and the group that has the
            same name as the service in the standard option files. This
            enables you to use the
      <code class="literal">
       [mysqld]
      </code>
      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.
     </p>
    </li>
    <li class="listitem">
     <p>
      If the service-installation command specifies a
      <a class="link" href="option-file-options.html#option_general_defaults-file">
       <code class="option">
        --defaults-file
       </code>
      </a>
      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.
     </p>
    </li>
   </ul>
  </div>
  <p>
   As a more complex example, consider the following command:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa30683358"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span> <span class="token atrule">"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqld"</span>
          <span class="token property">--install</span> MySQL <span class="token constant">--defaults-file</span><span class="token attr-value"><span class="token punctuation">=</span>C:\my-opts.cnf</span></code></pre>
  </div>
  <p>
   Here, the default service name (
   <code class="literal">
    MySQL
   </code>
   ) is
        given after the
   <code class="option">
    --install
   </code>
   option. If no
   <a class="link" href="option-file-options.html#option_general_defaults-file">
    <code class="option">
     --defaults-file
    </code>
   </a>
   option had been
        given, this command would have the effect of causing the server
        to read the
   <code class="literal">
    [mysqld]
   </code>
   group from the standard
        option files. However, because the
   <a class="link" href="option-file-options.html#option_general_defaults-file">
    <code class="option">
     --defaults-file
    </code>
   </a>
   option is
        present, the server reads options from the
   <code class="literal">
    [mysqld]
   </code>
   option group, and only from the
        named file.
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    On Windows, if the server is started with the
    <a class="link" href="option-file-options.html#option_general_defaults-file">
     <code class="option">
      --defaults-file
     </code>
    </a>
    and
    <a class="link" href="server-options.html#option_mysqld_install">
     <code class="option">
      --install
     </code>
    </a>
    options,
    <a class="link" href="server-options.html#option_mysqld_install">
     <code class="option">
      --install
     </code>
    </a>
    must be first.
          Otherwise,
    <code class="literal">
     mysqld.exe
    </code>
    attempts to start the
          MySQL server.
   </p>
  </div>
  <p>
   You can also specify options as Start parameters in the Windows
   <span class="command">
    <strong>
     Services
    </strong>
   </span>
   utility before you start the MySQL
        service.
  </p>
  <p>
   Finally, before trying to start the MySQL service, make sure the
        user variables
   <code class="literal">
    %TEMP%
   </code>
   and
   <code class="literal">
    %TMP%
   </code>
   (and also
   <code class="literal">
    %TMPDIR%
   </code>
   ,
        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. The default user for running the MySQL service is
   <code class="literal">
    LocalSystem
   </code>
   , and the default value for its
   <code class="literal">
    %TEMP%
   </code>
   and
   <code class="literal">
    %TMP%
   </code>
   is
   <code class="filename">
    C:\Windows\Temp
   </code>
   , a directory
   <code class="literal">
    LocalSystem
   </code>
   has write access to by default.
        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
   <a class="link" href="server-options.html#option_mysqld_tmpdir">
    <code class="option">
     --tmpdir
    </code>
   </a>
   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.
  </p>
  <h5>
   <a name="idm46045331915136">
   </a>
   Starting the service
  </h5>
  <p>
   After a MySQL server instance has been installed as a service,
        Windows starts the service automatically whenever Windows
        starts. The service also can be started immediately from the
   <span class="command">
    <strong>
     Services
    </strong>
   </span>
   utility, or by using an
   <span class="command">
    <strong>
     sc
        start
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   or
   <span class="command">
    <strong>
     NET START
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   command.
   <span class="command">
    <strong>
     SC
    </strong>
   </span>
   and
   <span class="command">
    <strong>
     NET
    </strong>
   </span>
   commands are not case-sensitive.
  </p>
  <p>
   When run as a service,
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   has no access
        to a console window, so no messages can be seen there. If
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   does not start, check the error log to
        see whether the server wrote any messages there to indicate the
        cause of the problem. The error log is located in the MySQL data
        directory (for example,
   <code class="filename">
    C:\Program Files\MySQL\MySQL
        Server 8.4\data
   </code>
   ). It is the file with a
        suffix of
   <code class="filename">
    .err
   </code>
   .
  </p>
  <p>
   When a MySQL server has been installed as a service, and the
        service is running, Windows stops the service automatically when
        Windows shuts down. The server also can be stopped manually
        using the
   <code class="literal">
    Services
   </code>
   utility, the
   <span class="command">
    <strong>
     sc
        stop
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   command, the
   <span class="command">
    <strong>
     NET STOP
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   command, or the
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin shutdown
     </strong>
    </span>
   </a>
   command.
  </p>
  <p>
   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
   <code class="option">
    --install-manual
   </code>
   option rather than the
   <code class="option">
    --install
   </code>
   option:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa87826419"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span> <span class="token atrule">"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqld"</span> <span class="token property">--install-manual</span></code></pre>
  </div>
  <h5>
   <a name="idm46045331897120">
   </a>
   Removing the service
  </h5>
  <p>
   To remove a server that is installed as a service, first stop it
        if it is running by executing
   <span class="command">
    <strong>
     SC STOP
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   or
   <span class="command">
    <strong>
     NET STOP
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   . Then
        use
   <span class="command">
    <strong>
     SC DELETE
     <em class="replaceable">
      <code>
       mysqld_service_name
      </code>
     </em>
    </strong>
   </span>
   to
        remove it:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa923702"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span><span class="token command"> SC</span> DELETE mysql</code></pre>
  </div>
  <p>
   Alternatively, use the
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   <a class="link" href="server-options.html#option_mysqld_remove">
    <code class="option">
     --remove
    </code>
   </a>
   option to remove the
        service.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa86230210"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">C:\&gt;</span> <span class="token atrule">"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqld"</span> <span class="token property">--remove</span></code></pre>
  </div>
  <p>
   If
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   is not running as a service, you
        can start it from the command line. For instructions, see
   <a class="xref" href="windows-start-command-line.html" title="2.3.3.6 Starting MySQL from the Windows Command Line">
    Section 2.3.3.6, “Starting MySQL from the Windows Command Line”
   </a>
   .
  </p>
  <p>
   If you encounter difficulties during installation, see
   <a class="xref" href="windows-troubleshooting.html" title="2.3.4 Troubleshooting a Microsoft Windows MySQL Server Installation">
    Section 2.3.4, “Troubleshooting a Microsoft Windows MySQL Server Installation”
   </a>
   .
  </p>
  <p>
   For more information about stopping or removing a Windows
        service, see
   <a class="xref" href="multiple-windows-services.html" title="7.8.2.2 Starting Multiple MySQL Instances as Windows Services">
    Section 7.8.2.2, “Starting Multiple MySQL Instances as Windows Services”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
