5.4. MySQL Server Startup Programs

MySQL 5.0

5.4. MySQL Server Startup Programs

This section describes several programs that are used to start mysqld, the MySQL server.

5.4.1. mysqld_safe — MySQL Server Startup Script

mysqld_safe is the recommended way to start a mysqld server on Unix and NetWare. mysqld_safe adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log file. NetWare-specific behaviors are listed later in this section.

Note: To preserve backward compatibility with older versions of MySQL, MySQL binary distributions still include safe_mysqld as a symbolic link to mysqld_safe. However, you should not rely on this because it is removed as of MySQL 5.1.

By default, mysqld_safe tries to start an executable named mysqld-max if it exists, and mysqld otherwise. Be aware of the implications of this behavior:

  • On Linux, the RPM relies on this mysqld_safe behavior. The RPM installs an executable named mysqld-max, which causes mysqld_safe to automatically use that executable rather than mysqld from that point on.

  • If you install a MySQL-Max distribution that includes a server named mysqld-max, and then upgrade later to a non-Max version of MySQL, mysqld_safe will still attempt to run the old mysqld-max server. If you perform such an upgrade, you should manually remove the old mysqld-max server to ensure that mysqld_safe runs the new mysqld server.

To override the default behavior and specify explicitly the name of the server you want to run, specify a or option to mysqld_safe. You can also use to indicate the directory where mysqld_safe should look for the server.

Many of the options to mysqld_safe are the same as the options to mysqld. See Section 5.2.1, “mysqld Command Options”.

All options specified to mysqld_safe on the command line are passed to mysqld. If you want to use any options that are specific to mysqld_safe and that mysqld doesn't support, do not specify them on the command line. Instead, list them in the group of an option file. See Section 4.3.2, “Using Option Files”.

mysqld_safe reads all options from the , , and sections in option files. For backward compatibility, it also reads sections, although you should rename such sections to in MySQL 5.0 installations.

mysqld_safe supports the following options:

  • Display a help message and exit. (Added in MySQL 5.0.3)

  • (NetWare only) On NetWare, mysqld_safe provides a screen presence. When you unload (shut down) the mysqld_safe NLM, the screen does not by default go away. Instead, it prompts for user input:

    *<NLM has terminated; Press any key to close the screen>*
    

    If you want NetWare to close the screen automatically instead, use the option to mysqld_safe.

  • The path to the MySQL installation directory.

  • The size of the core file that mysqld should be able to create. The option value is passed to ulimit -c.

  • The path to the data directory.

  • The name of an option file to be read in addition to the usual option files. This must be the first option on the command line if it is used. As of MySQL 5.0.6, if the file does not exist or is otherwise inaccessible, the server will exit with an error.

  • The name of an option file to be read instead of the usual option files. This must be the first option on the command line if it is used.

  • If mysqld_safe cannot find the server, use this option to indicate the pathname to the directory where the server is located.

  • Write the error log to the given file. See Section 5.12.1, “The Error Log”.

  • The name of the server program (in the directory) that you want to start. This option is needed if you use the MySQL binary distribution but have the data directory outside of the binary distribution. If mysqld_safe cannot find the server, use the option to indicate the pathname to the directory where the server is located.

  • This option is similar to the option, but you specify only the suffix for the server program name. The basename is assumed to be mysqld. For example, if you use , mysqld_safe starts the mysqld-max program in the directory. If the argument to is empty, mysqld_safe uses mysqld in the directory.

  • Use the program to set the server's scheduling priority to the given value.

  • Do not read any option files. This must be the first option on the command line if it is used.

  • The number of files that mysqld should be able to open. The option value is passed to ulimit -n. Note that you need to start mysqld_safe as for this to work properly!

  • The pathname of the process ID file.

  • The port number that the server should use when listening for TCP/IP connections. The port number must be 1024 or higher unless the server is started by the system user.

  • The Unix socket file that the server should use when listening for local connections.

  • Set the time zone environment variable to the given option value. Consult your operating system documentation for legal time zone specification formats.

  • |}

    Run the mysqld server as the user having the name or the numeric user ID . (“User” in this context refers to a system login account, not a MySQL user listed in the grant tables.)

If you execute mysqld_safe with the or option to name an option file, the option must be the first one given on the command line or the option file will not be used. For example, this command will not use the named option file:

mysql>  --defaults-file=

Instead, use the following command:

mysql>  --port=

The mysqld_safe script is written so that it normally can start a server that was installed from either a source or a binary distribution of MySQL, even though these types of distributions typically install the server in slightly different locations. (See Section 2.1.5, “Installation Layouts”.) mysqld_safe expects one of the following conditions to be true:

  • The server and databases can be found relative to the working directory (the directory from which mysqld_safe is invoked). For binary distributions, mysqld_safe looks under its working directory for and directories. For source distributions, it looks for and directories. This condition should be met if you execute mysqld_safe from your MySQL installation directory (for example, for a binary distribution).

  • If the server and databases cannot be found relative to the working directory, mysqld_safe attempts to locate them by absolute pathnames. Typical locations are and . The actual locations are determined from the values configured into the distribution at the time it was built. They should be correct if MySQL is installed in the location specified at configuration time.

Because mysqld_safe tries to find the server and databases relative to its own working directory, you can install a binary distribution of MySQL anywhere, as long as you run mysqld_safe from the MySQL installation directory:

shell> 
shell> 

If mysqld_safe fails, even when invoked from the MySQL installation directory, you can specify the and options to indicate the directories in which the server and databases are located on your system.

Normally, you should not edit the mysqld_safe script. Instead, configure mysqld_safe by using command-line options or options in the section of a option file. In rare cases, it might be necessary to edit mysqld_safe to get it to start the server properly. However, if you do this, your modified version of mysqld_safe might be overwritten if you upgrade MySQL in the future, so you should make a copy of your edited version that you can reinstall.

On NetWare, mysqld_safe is a NetWare Loadable Module (NLM) that is ported from the original Unix shell script. It starts the server as follows:

  1. Runs a number of system and option checks.

  2. Runs a check on tables.

  3. Provides a screen presence for the MySQL server.

  4. Starts mysqld, monitors it, and restarts it if it terminates in error.

  5. Sends error messages from mysqld to the .err file in the data directory.

  6. Sends mysqld_safe screen output to the .safe file in the data directory.

5.4.2. mysql.server — MySQL Server Startup Script

MySQL distributions on Unix include a script named mysql.server. It can be used on systems such as Linux and Solaris that use System V-style run directories to start and stop system services. It is also used by the Mac OS X Startup Item for MySQL.

mysql.server can be found in the directory under your MySQL installation directory or in a MySQL source distribution.

If you use the Linux server RPM package (.rpm), the mysql.server script will be installed in the directory with the name . You need not install it manually. See Section 2.4, “Installing MySQL on Linux”, for more information on the Linux RPM packages.

Some vendors provide RPM packages that install a startup script under a different name such as mysqld.

If you install MySQL from a source distribution or using a binary distribution format that does not install mysql.server automatically, you can install it manually. Instructions are provided in Section 2.10.2.2, “Starting and Stopping MySQL Automatically”.

mysql.server reads options from the and sections of option files. For backward compatibility, it also reads sections, although you should rename such sections to when using MySQL 5.0.

5.4.3. mysqld_multi — Manage Multiple MySQL Servers

mysqld_multi is designed to manage several mysqld processes that listen for connections on different Unix socket files and TCP/IP ports. It can start or stop servers, or report their current status. The MySQL Instance Manager is an alternative means of managing multiple servers (see Section 5.5, “mysqlmanager — The MySQL Instance Manager”).

mysqld_multi searches for groups named ] in (or in the file named by the option). can be any positive integer. This number is referred to in the following discussion as the option group number, or . Group numbers distinguish option groups from one another and are used as arguments to mysqld_multi to specify which servers you want to start, stop, or obtain a status report for. Options listed in these groups are the same that you would use in the group used for starting mysqld. (See, for example, Section 2.10.2.2, “Starting and Stopping MySQL Automatically”.) However, when using multiple servers, it is necessary that each one use its own value for options such as the Unix socket file and TCP/IP port number. For more information on which options must be unique per server in a multiple-server environment, see Section 5.13, “Running Multiple MySQL Servers on the Same Machine”.

To invoke mysqld_multi, use the following syntax:

shell> ] {start|stop|report} [[,] ...]

, , and indicate which operation to perform. You can perform the designated operation for a single server or multiple servers, depending on the list that follows the option name. If there is no list, mysqld_multi performs the operation for all servers in the option file.

Each value represents an option group number or range of group numbers. The value should be the number at the end of the group name in the option file. For example, the for a group named is . To specify a range of numbers, separate the first and last numbers by a dash. The value represents groups through . Multiple groups or group ranges can be specified on the command line, separated by commas. There must be no whitespace characters (spaces or tabs) in the list; anything after a whitespace character is ignored.

This command starts a single server using option group :

shell> 

This command stops several servers, using option groups and through :

shell> 

For an example of how you might set up an option file, use this command:

shell> 

mysqld_multi supports the following options:

  • Display a help message and exit.

  • Specify the name of an alternative option file. This affects where mysqld_multi looks for ] option groups. Without this option, all options are read from the usual file. The option does not affect where mysqld_multi reads its own options, which are always taken from the group in the usual file.

  • Display a sample option file.

  • Specify the name of the log file. If the file exists, log output is appended to it.

  • The mysqladmin binary to be used to stop servers.

  • The mysqld binary to be used. Note that you can specify mysqld_safe as the value for this option also. If you use mysqld_safe to start the server, you can include the or options in the corresponding ] option group. These options indicate the name of the server that mysqld_safe should start and the pathname of the directory where the server is located. (See the descriptions for these options in Section 5.4.1, “mysqld_safe — MySQL Server Startup Script”.) Example:

    [mysqld38]
    mysqld = mysqld-max
    ledir  = /opt/local/mysql/libexec
    
  • Print log information to rather than to the log file. By default, output goes to the log file.

  • The password of the MySQL account to use when invoking mysqladmin. Note that the password value is not optional for this option, unlike for other MySQL programs.

  • Silent mode; disable warnings.

  • Connect to each MySQL server via the TCP/IP port instead of the Unix socket file. (If a socket file is missing, the server might still be running, but accessible only via the TCP/IP port.) By default, connections are made using the Unix socket file. This option affects and operations.

  • The username of the MySQL account to use when invoking mysqladmin.

  • Be more verbose.

  • Display version information and exit.

Some notes about mysqld_multi:

  • Most important: Before using mysqld_multi be sure that you understand the meanings of the options that are passed to the mysqld servers and why you would want to have separate mysqld processes. Beware of the dangers of using multiple mysqld servers with the same data directory. Use separate data directories, unless you know what you are doing. Starting multiple servers with the same data directory does not give you extra performance in a threaded system. See Section 5.13, “Running Multiple MySQL Servers on the Same Machine”.

  • Important: Make sure that the data directory for each server is fully accessible to the Unix account that the specific mysqld process is started as. Do not use the Unix account for this, unless you know what you are doing. See Section 5.7.5, “How to Run MySQL as a Normal User”.

  • Make sure that the MySQL account used for stopping the mysqld servers (with the mysqladmin program) has the same username and password for each server. Also, make sure that the account has the privilege. If the servers that you want to manage have different usernames or passwords for the administrative accounts, you might want to create an account on each server that has the same username and password. For example, you might set up a common account by executing the following commands for each server:

    shell> 
    Enter password:
    mysql> 
        -> 
    

    See Section 5.8.2, “How the Privilege System Works”. You have to do this for each mysqld server. Change the connection parameters appropriately when connecting to each one. Note that the hostname part of the account name must allow you to connect as from the host where you want to run mysqld_multi.

  • The Unix socket file and the TCP/IP port number must be different for every mysqld.

  • The option is very important if you are using mysqld_safe to start mysqld (for example, ) Every mysqld should have its own process ID file. The advantage of using mysqld_safe instead of mysqld is that mysqld_safe monitors its mysqld process and restarts it if the process terminates due to a signal sent using or for other reasons, such as a segmentation fault. Please note that the mysqld_safe script might require that you start it from a certain place. This means that you might have to change location to a certain directory before running mysqld_multi. If you have problems starting, please see the mysqld_safe script. Check especially the lines:

    ----------------------------------------------------------------
    MY_PWD=`pwd`
    # Check if we are starting this relative (for the binary release)
    if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
     -x ./bin/mysqld
    ----------------------------------------------------------------
    

    The test performed by these lines should be successful, or you might encounter problems. See Section 5.4.1, “mysqld_safe — MySQL Server Startup Script”.

  • You might want to use the option for mysqld, but to do this you need to run the mysqld_multi script as the Unix user. Having the option in the option file doesn't matter; you just get a warning if you are not the superuser and the mysqld processes are started under your own Unix account.

The following example shows how you might set up an option file for use with mysqld_multi. The order in which the mysqld programs are started or stopped depends on the order in which they appear in the option file. Group numbers need not form an unbroken sequence. The first and fifth ] groups were intentionally omitted from the example to illustrate that you can have “gaps” in the option file. This gives you more flexibility.

# This file should probably be in your home dir (~/.my.cnf)
# or /etc/my.cnf
# Version 2.1 by Jani Tolonen

[mysqld_multi]
mysqld     = /usr/local/bin/mysqld_safe
mysqladmin = /usr/local/bin/mysqladmin
user       = multi_admin
password   = multipass

[mysqld2]
socket     = /tmp/mysql.sock2
port       = 3307
pid-file   = /usr/local/mysql/var2/hostname.pid2
datadir    = /usr/local/mysql/var2
language   = /usr/local/share/mysql/english
user       = john

[mysqld3]
socket     = /tmp/mysql.sock3
port       = 3308
pid-file   = /usr/local/mysql/var3/hostname.pid3
datadir    = /usr/local/mysql/var3
language   = /usr/local/share/mysql/swedish
user       = monty

[mysqld4]
socket     = /tmp/mysql.sock4
port       = 3309
pid-file   = /usr/local/mysql/var4/hostname.pid4
datadir    = /usr/local/mysql/var4
language   = /usr/local/share/mysql/estonia
user       = tonu

[mysqld6]
socket     = /tmp/mysql.sock6
port       = 3311
pid-file   = /usr/local/mysql/var6/hostname.pid6
datadir    = /usr/local/mysql/var6
language   = /usr/local/share/mysql/japanese
user       = jani

See Section 4.3.2, “Using Option Files”.