4.3.2 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 before MySQL 5.0.27
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 MySQL-Max 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.

mysqld_safe reads all options from the
[mysqld], [server], and
[mysqld_safe] sections in option files. For
example, if you specify a [mysqld] section
like this, mysqld_safe will find and use the
--log-error option:

[mysqld]
log-error=error.log

For backward compatibility, mysqld_safe also
reads [safe_mysqld] sections, but to be
current you should rename such sections to
[mysqld_safe].

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 the server program (in the
ledir 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
--ledir option to
indicate the path name to the directory where the server is
located.

Run the mysqld server as the user having
the name user_name or the numeric
user ID 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
--defaults-file or
--defaults-extra-file 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> mysqld_safe --port=port_num --defaults-file=file_name

Instead, use the following command:

mysql> mysqld_safe --defaults-file=file_name --port=port_num

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.7, “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 bin and
data directories. For source
distributions, it looks for libexec and
var directories. This condition should
be met if you execute mysqld_safe from
your MySQL installation directory (for example,
/usr/local/mysql 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 path names. Typical locations are
/usr/local/libexec and
/usr/local/var. 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> cd mysql_installation_directory
shell> bin/mysqld_safe &

If mysqld_safe fails, even when invoked from
the MySQL installation directory, specify the
--ledir and
--datadir 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
[mysqld_safe] section of a
my.cnf 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:

Runs a number of system and option checks.

Runs a check on MyISAM tables.

Provides a screen presence for the MySQL server.

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

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

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

User Comments

If you invoke mysql_safe from the default /etc/init.d/mysql script, beware of interference from SELinux. You may run into a problem where mysql_safe terminates immediately and causes the start operation to fail. If so, try turning off SELinux and see if it makes the problem go away.