4.1.4 The mysql.server Server Startup Script - MySQL
If you need to administer MySQL, this article gets you off to a good start. The first of a multi-part series, it is excerpted from chapter four of the book MySQL Administrator's Guide, written by Paul Dubois (Sams; ISBN: 0672326345).
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 support-files directory under your MySQL installation directory or in a MySQL source tree.
If you use the Linux server RPM package (MySQL-server-VERSION.rpm), the mysql.server script will already have been installed in the /etc/init.d directory with the name mysql. You need not install it manually. See Section 2.2.2, "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 use a binary distribution format that does not install mysql.server automatically, you can install it manually. Instructions are provided in Section 2.4.3, "Starting and Stopping MySQL Automatically."
mysql.server reads options from the [mysql.server] and [mysqld] sections of option files. For backward compatibility, it also reads [mysql_server] sections, although you should rename such sections to [mysql.server] when you begin using MySQL 4.0 or later.
4.1.5 The mysqld_multi Program for Managing Multiple MySQL Servers
mysqld_multi is meant for managing 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 program searches for groups named [mysqld#] in my.cnf (or in the file named by the --config-file option). # can be any positive integer. This number is referred to in the following discussion as the option group number, or GNR. 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 [mysqld] group used for starting mysqld. (See, for example, Section 2.4.3, "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 4.9, "Running Multiple MySQL Servers on the Same Machine."
start, stop, and report indicate which operation you want to perform. You can perform the designated operation on a single server or multiple servers, depending on the GNR list that follows the option name. If there is no list, mysqld_multi performs the operation for all servers in the option file.
Each GNR 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 GNR for a group named [mysqld17] is 17. To specify a range of numbers, separate the first and last numbers by a dash. The GNR value 10-13 represents groups [mysqld10] through [mysqld13]. 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 GNR list; anything after a whitespace character is ignored.
This command starts a single server using option group [mysqld17]:
shell> mysqld_multi start 17
This command stops several servers, using option groups [mysql8] and [mysqld10] through [mysqld13]:
shell> mysqld_multi start 8,10-13
For an example of how you might set up an option file, use this command:
shell> mysqld_multi --example
mysqld_multi supports the following options:
Specify the name of an alternative option file. This affects where mysqld_multi looks for [mysqld#] option groups. Without this option, all options are read from the usual my.cnf file. The option does not affect where mysqld_multi reads its own options, which are always taken from the [mysqld_multi] group in the usual my.cnf file.
Display a sample option file.
Display a help message and exit.
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. The options are passed to mysqld. Just make sure that you have the directory where mysqld is located in your PATH environment variable setting or fix mysqld_safe.
Print log information to stdout 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.
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 stop and report operations.
The username of the MySQL account to use when invoking mysqladmin.
Display version information and exit.
Some notes about mysqld_multi:
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 SHUTDOWN privilege. If the servers that you want to manage have many 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 multi_admin account by executing the following commands for each server:
shell> mysql -u root -S /tmp/mysql.sock
mysql> GRANT SHUTDOWN ON *.* -> TO 'multi_admin'@'localhost' IDENTIFIED
See Section 4.4.2, "How the Privilege System Works." You will have to do this for each mysqld server. Change the connection parameters appropriately when connecting to each one. Note that the host part of the account name must allow you to connect as multi_admin from the host where you want to run mysqld_multi.
The --pid-file option is very important if you are using mysqld_safe to start mysqld (for example, --mysqld=mysqld_safe) Every mysqld should have its own process ID file. The advantage of using mysqld_safe instead of mysqld is that mysqld_safe "guards" its mysqld process and will restart it if the process terminates due to a signal sent using kill -9, 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:
# 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 \
See Section 4.1.3, "The mysqld_safe Server Startup Script." The test performed by these lines should be successful, or you might encounter problems.
The Unix socket file and the TCP/IP port number must be different for every mysqld.
You might want to use the --user option for mysqld, but in order to do this you need to run the mysqld_multi script as the Unix root user. Having the option in the option file doesn't matter; you will just get a warning, if you are not the superuser and the mysqld processes are started under your own Unix account.
Important: Make sure that the data directory is fully accessible to the Unix account that the specific mysqld process is started as. Do not use the Unix root account for this, unless you know what you are doing.
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 will not give you extra performance in a threaded system. See Section 4.9, "Running Multiple MySQL Servers on the Same Machine."
The following example shows how you might set up an option file for use with mysqld_multi. The first and fifth [mysqld#] group were intentionally left out from the example to illustrate that you can have "gaps" in the option file. This gives you more flexibility. The order in which the mysqld programs are started or stopped depends on the order in which they appear in the option file.
# This file should probably be in your home dir
# or /etc/my.cnf
# Version 2.1 by Jani Tolonen
mysqld = /usr/local/bin/mysqld_safe
mysqladmin = /usr/local/bin/mysqladmin
user = multi_admin
password = multipass
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
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
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
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