Book HomeManaging and Using MySQLSearch this book

4.2. Server Startup and Shutdown

Because the MySQL server is a server process, it should start up automatically when the machine starts up. It should also shut down cleanly when the computer shuts down. How you achieve these goals depends heavily on your operating system.

TIP: You can start the server directly from the command line using the mysqld command. It is recommended, however, that you use safe_mysqld when starting MySQL by hand under all operating systems.

4.2.1. Unix/Linux

On Unix and Unix-like systems such as Linux (but not on Mac OS X), there are a couple of different ways of starting and stopping MySQL depending on whether you are dealing with a SVR4 or another flavor of Unix. The mysql.server script exists for SVR4 systems, and safe_mysqld works for any other Unix system.[17] We describe general "rules of thumb" for using these scripts. One of the maddening things about Unix systems, however, is in how they differ slightly from system to system in the details of service startup and shutdown.

[17]In the MySQL 4.0 alpha versions, this script has been renamed mysqld_safe.

4.2.1.1. SVR4

The mysql.server script supports the startup and shutdown of MySQL on SVR4 systems.[18] You can find this script in the support-files directory of your installation, usually /usr/local/mysql/support-files. The SVR4 startup/shutdown mechanism relies on a set of scripts in the /etc folder to start and stop services when the system enters different run levels.

[18]The mysql.server script is actually a specially designed script to call safe_mysqld on SVR4 systems.

TIP: If you installed MySQL on Linux using the RPM packages, mysql.server was probably installed on your system. The RPM installer renames mysql.server to mysql when it copies the file to /etc/rc.d/init.d. If the file /etc/rc.d/init.d/mysql exists, you are already set up to have MySQL automatically start up and shut down.

The procedure for installing mysql.server on a RedHat Linux system is:

$ cp mysql.server /etc/rc.d/init.d
$ ln -s /etc/rc.d/init.d/mysql.server /etc/rc.d/rc3.d/S99mysql
$ ln -s /etc/rc.d/init.d/mysql.server /etc/rc.d/rc0.d/S01mysql

The first line installs the script into the initialization scripts directory, /etc/rc.d/init.d. The second command creates a link to this script that causes Linux to execute the script when the system enters run-level 3. For a simple explanation, Linux executes the scripts in /etc/rc.d/rc3.d when the system enters run-level 3. Run-level 3 typically means that an SVR4 system has entered multiuser mode. If your Unix flavor uses a different run level to represent multiuser mode, you should install the link in that directory.

The final line creates a link to the mysql.server script for run-level 0. Run-level 0 executes a system halt. The scripts in /etc/rc.d/rc0.d execute when the system shuts down.

4.2.1.2. Other Unix

On non-SVR4 systems, the script safe_mysqld starts MySQL. You can find it in the /bin directory of your MySQL install, usually /usr/local/mysql/bin.

To use safe_mysqld, you need to know how your flavor of Unix starts services at boot and kills them at shutdown. On some BSD systems, there is a file called /etc/rc.local that you modify to call safe_mysqld. Newer BSD systems such as FreeBSD, however, may have an rc.local file that you should not modify. FreeBSD, for example, expects you to place scripts such as mysql.server in /usr/local/etc/rc.d.

4.2.2. Mac OS X

Mac OS X has introduced yet another way of automatically starting services on a Unix system. It specifically has three different startup directories:

The /System/Library/StartupItems directory is for operating system services, and the $HOME/Library/StartupItems is for user-owned services. MySQL should start up from /Library/StartupItems, the directory for general services that should be started at system boot time.

The StartupItems directories expect each service to have its own directory, so your first step for MySQL is to create the directory /Library/StartupItems/MySQL. It actually does not matter what you call this directory. Whatever you call it, in it you will place two files:

The Unix script is a simple script that calls the safe_mysqld command to start MySQL. It should have the same name as the directory in which you placed it (in this case, MySQL). It should look something like this:

#!/bin/sh

. /etc/rc.common

if [ "${MYSQLSERVER:=-NO-}" = "-YES
    cd /usr/local/mysql
    bin/mysqld_safe --user=mysql &
fi

The check on the value of $MYSQLSERVER enables you to turn off MySQL in your Mac OS X hostconfig file without having to delete the MySQL directory from your StartupItems. To enable MySQL, you need to add the following line to /etc/hostconfig:

MYSQLSERVER=-YES-

Similarly, you can stop MySQL from starting at boot time simply by changing -YES- to -NO-.

Once you have the script installed, you need to add the file StartupParameters.plist:

{
  Description     = "MySQL Database Server";
  Provides        = ("MySQL");
  Requires        = ("Resolver");
  OrderPreference = "None";
  Messages =
  {
    start = "Starting MySQL Server";
    stop  = "Stopping MySQL Server";
  };
}

This file tells Mac OS X about the service in this directory and any dependencies it has on other services. When you reboot, you should see the message "Starting MySQL Server" on your startup screen along with other service startup messages.

4.2.3. Windows NT/2000

Windows NT systems automatically start up and shut down any applications installed as NT services. In Chapter 2 we covered the details of installing MySQL as a server in Windows systems.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.