W3cubDocs

/MariaDB

mysqld_safe

The mysqld_safe startup script is in MariaDB distributions on Linux and Unix. It is a wrapper that starts mysqld with some extra safety features. For example, if mysqld_safe notices that mysqld has crashed, then mysqld_safe will automatically restart mysqld.

mysqld_safe is the recommended way to start mysqld on Linux and Unix distributions that do not support systemd. Additionally, the mysql.server init script used by sysVinit starts mysqld with mysqld_safe by default.

From MariaDB 10.4.6, mariadbd-safe is a symlink to mysqld_safe.

Using mysqld_safe

The command to use mysqld_safe and the general syntax is:

mysqld_safe [ --defaults-file | --defaults-extra-file ] <options> <mysqld_options>

Options

Many of the options supported by mysqld_safe are identical to options supported by mysqld. If an unknown option is provided to mysqld_safe on the command-line, then it is passed to mysqld.

mysqld_safe supports the following options:

Option Description
--help Display a help message and exit.
--autoclose (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 --autoclose option to mysqld_safe.
--basedir=path The path to the MariaDB installation directory.
--core-file-size=size The size of the core file that mysqld should be able to create. The option value is passed to ulimit -c.
--crash-script=file Script to call in the event of mysqld crashing.
--datadir=path The path to the data directory.
--defaults-extra-file=path 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. If the file does not exist or is otherwise inaccessible, the server will exit with an error.
--defaults-file=file_name 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.
--flush-caches Flush and purge buffers/caches before starting the server.
--ledir=path If mysqld_safe cannot find the server, use this option to indicate the path name to the directory where the server is located.
--log-error=file_name Write the error log to the given file.
--malloc-lib=lib Preload shared library lib if available. See debugging MariaDB for an example.
--mysqld=prog_nam The name of the server program (in the ledir directory) that you want to start. This option is needed if you use the MariaDB 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.
--mysqld-version=suffix This option is similar to the --mysqld 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-version=debug, mysqld_safe starts the mysqld-debug program in the ledir directory. If the argument to --mysqld-version is empty, mysqld_safe uses mysqld in the ledir directory.
--nice=priority Use the nice program to set the server´s scheduling priority to the given value.
--no-auto-restart Exit after starting mysqld.
--no-defaults Do not read any option files. This must be the first option on the command line if it is used.
--no-watch Exit after starting mysqld.
--numa-interleave Run mysqld with its memory interleaved on all NUMA nodes.
--open-files-limit=count 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 root for this to work properly.
--pid-file=file_name The path name of the process ID file.
--plugin-dir=dir_name, Directory for client-side plugins.
--port=port_num 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 root system user.
--skip-kill-mysqld Do not try to kill stray mysqld processes at startup. This option works only on Linux.
--socket=path The Unix socket file that the server should use when listening for local connections.
--syslog, --skip-syslog --syslog causes error messages to be sent to syslog on systems that support the logger program. --skip-syslog suppresses the use of syslog; messages are written to an error log file.
--syslog-tag=tag For logging to syslog, messages from mysqld_safe and mysqld are written with a tag of mysqld_safe and mysqld, respectively. To specify a suffix for the tag, use --syslog-tag=tag, which modifies the tags to be mysqld_safe-tag and mysqld-tag.
--timezone=timezone Set the TZ time zone environment variable to the given option value. Consult your operating system documentation for legal time zone specification formats. Also see Time Zones.
--user={user_name or user_id} 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 MariaDB user listed in the grant tables.)

Option Files

In addition to reading options from the command-line, mysqld_safe can also read options from option files. If an unknown option is provided to mysqld_safe in an option file, then it is ignored.

The following options relate to how MariaDB command-line tools handles option files. They must be given as the first argument on the command-line:

Option Description
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.

Option Groups

mysqld_safe reads options from the following option groups from option files:

Group Description
[mysqld_safe] Options read by mysqld_safe, which includes both MariaDB Server and MySQL Server.
[mariadb_safe] Options read by mysqld_safe from MariaDB Server.
[safe_mysqld] Options read by mysqld_safe, which includes both MariaDB Server and MySQL Server.

The [safe_mysqld] option group is primarily supported for backward compatibility. You should rename such option groups to [mysqld_safe] in MariaDB installations to prevent breakage in the future if this compatibility is removed.

mysqld_safe also reads options from the following server option groups from option files:

Group Description
[mysqld] Options read by mysqld, which includes both MariaDB Server and MySQL Server.
[server] Options read by MariaDB Server.
[mysqld-X.Y] Options read by a specific version of mysqld, which includes both MariaDB Server and MySQL Server. For example, [mysqld-5.5].
[mariadb] Options read by MariaDB Server.
[mariadb-X.Y] Options read by a specific version of MariaDB Server.
[client-server] Options read by all MariaDB client programs and the MariaDB Server. This is useful for options like socket and port, which is common between the server and the clients.
[galera] Options read by a galera-capable MariaDB Server. Available on systems compiled with Galera support.

For example, if you specify the log_error option in a server option group in an option file, like this:

[mariadb]
log_error=error.log

Then mysqld_safe will also use this value for its own --log-error option:

Configuring the Open Files Limit

When using mysqld_safe, the system's open files limit can be changed by providing the --open-files-limit option either on the command-line or in an option file. For example:

[mysqld_safe]
open_files_limit=4294967295

The option value is passed to ulimit -n. Note that you need to start mysqld_safe as root for this to work properly. However, you can't currently set this to unlimited. See MDEV-18410 about that.

When mysqld_safe starts mysqld, it also uses this option to set the value of the open_files_limit system variable for mysqld.

Configuring the Core File Size

When using mysqld_safe, if you would like to enable core dumps, the system's core file size limit can be changed by providing the --core-file-size option either on the command-line or in an option file. For example:

[mysqld_safe]
core_file_size=unlimited

The option value is passed to ulimit -c. Note that you need to start mysqld_safe as root for this to work properly.

Configuring MariaDB to Write the Error Log to Syslog

When using mysqld_safe, if you would like to redirect the error log to the syslog, then that can easily be done by using the --syslog option. mysqld_safe redirects two types of log messages to the syslog--its own log messages, and log messages for mysqld.

  • mysqld_safe configures its own log messages to go to the daemon syslog facility. The log level for these messages is either notice or error, depending on the specific type of log message. The default tag is mysqld_safe.
  • mysqld_safe also configures the log messages for mysqld to go to the daemon syslog facility. The log level for these messages is error. The default tag is mysqld.

Sometimes it can be helpful to add a suffix to the syslog tag, such as if you are running multiple instances of MariaDB on the same host. To add a suffix to each syslog tag, use the --syslog-tag option.

Specifying mysqld

By default, mysqld_safe tries to start an executable named mysqld.

You can also specify another executable for mysqld_safe to start instead of mysqld by providing the --mysqld or --mysqld-version options either on the command-line or in an option file.

By default, it will look for mysqld in the following locations in the following order:

  • $BASEDIR/libexec/mysqld
  • $BASEDIR/sbin/mysqld
  • $BASEDIR/bin/mysqld
  • $PWD/bin/mysqld
  • $PWD/libexec/mysqld
  • $PWD/sbin/mysqld
  • @libexecdir@/mysql

Where $BASEDIR is set by the --basedir option, $PWD is the current working directory where mysqld_safe was invoked, and @libexecdir@ is set at compile-time by the INSTALL_BINDIR option for cmake.

You can also specify where the executable is located by providing the --ledir option either on the command-line or in an option file.

Specifying datadir

By default, mysqld_safe will look for the datadir in the following locations in the following order:

  • $BASEDIR/data/mysql
  • $BASEDIR/data
  • $BASEDIR/var/mysql
  • $BASEDIR/var
  • @localstatedir@

Where $BASEDIR is set by the --basedir option, and @localstatedir@ is set at compile-time by the INSTALL_MYSQLDATADIR option for cmake.

You can also specify where the datadir is located by providing the --datadir option either on the command-line or in an option file.

Logging

When you use mysqld_safe to start mysqld, mysqld_safe logs to the same destination as mysqld.

mysqld_safe has several log-related options:

  • --syslog: Write error messages to syslog on systems that support the logger program.
  • --skip-syslog: Do not write error messages to syslog. Messages are written to the default error log file (host_name.err in the data directory), or to a named file if the --log-error option is given.
  • --log-error=file_name: Write error messages to the named error file.

If none of these options is provided, then the default is --skip-syslog.

If --syslog and --log-error are both provided, then a warning is issued and --log-error takes precedence.

mysqld_safe also writes notices to stdout and errors to stderr.

Editing mysqld_safe

mysqld_safe is a sh script, so if you need to change its behavior, then it can easily be edited. However, you should not normally edit the script. A lot of behavior can be changed by providing options either on the command-line or in an option file.

If you do edit mysqld_safe, then you should be aware of the fact that a package upgrade can overwrite your changes. If you would like to preserve your changes, be sure to have a backup.

NetWare

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 MyISAM tables.
  3. Provides a screen presence for the MariaDB server.
  4. Starts mysqld, monitors it, and restarts it if it terminates in error.
  5. Sends error messages from mysqld to the host_name.err file in the data directory.
  6. Sends mysqld_safe screen output to the host_name.safe file in the data directory.

See Also

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.

© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/mysqld_safe/