ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**mysqld** [TOC] ## **mysqld**,The MySQL Server MySQL Server, the main program that does most of the work in a MySQL installation, manages access to the MySQL data directory that contains databases and tables. The data directory is also the default location for other information such as log files and status files. The **`mysqld`** program has many options that can be specified at startup. For a complete list of options, run this command: ~~~terminal shell> mysqld --verbose --help ~~~ MySQL Server has a set of * `system variables`, that affect its operation as it runs. System variables can be set at server startup, and many of them can be changed at runtime to effect dynamic server reconfiguration. * `status variables`, that provide information about its operation. You can monitor these status variables to access runtime performance characteristics. ## **mysqld_safe**, MySQL Server Startup Script **`mysqld_safe`** is the recommended way to start a `mysqld` server on Unix. **`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: ~~~ini [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]`. **`mysqld_safe`** accepts options on the command line and in option files. ## **mysql.server**, MySQL Server Startup Script MySQL distributions on Unix and Unix-like system include a script named **`mysql.server`**, which starts the MySQL server using **`mysqld_safe`**. 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 macOS Startup Item for MySQL. [**mysql.server**](https://dev.mysql.com/doc/refman/5.7/en/mysql-server.html "4.3.3 mysql.server — MySQL Server Startup Script")is the script name as used within the MySQL source tree. The installed name might be different; for example, [**mysqld**](https://dev.mysql.com/doc/refman/5.7/en/mysqld.html "4.3.1 mysqld — The MySQL Server") or [**mysql**](https://dev.mysql.com/doc/refman/5.7/en/mysql.html "4.5.1 mysql — The MySQL Command-Line Client"). In the following discussion, adjust the name [**mysql.server**](https://dev.mysql.com/doc/refman/5.7/en/mysql-server.html "4.3.3 mysql.server — MySQL Server Startup Script") as appropriate for your system. >[warning] Note For some Linux platforms, MySQL installation from `RPM` or `Debian` packages includes `systemd` support for managing MySQL server startup and shutdown. On these platforms, **`mysql.server`** and **`mysqld_safe`** are not installed because they are unnecessary. For more information, see `Managing MySQL Server with systemd`. ## **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. >[warning] Note For some Linux platforms, MySQL installation from `RPM` or `Debian` packages includes systemd support for managing MySQL server startup and shutdown. On these platforms, **`mysqld_multi`** is not installed because it is unnecessary. For information about using systemd to handle multiple MySQL instances, see `Managing MySQL Server with systemd`. ## Managing MySQL Server with `systemd` [[官方文档]](https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html "2.5.10 Managing MySQL Server with systemd") If you install MySQL using an RPM or Debian package on the following Linux platforms, server startup and shutdown is managed by systemd: * RPM package platforms: * Red Hat Enterprise Linux 7; Oracle Linux 7; CentOS 7 * SUSE Linux Enterprise Server 12 * Fedora 28 and 29 * Debian package platforms: * Debian 8 or higher * Ubuntu 16 or higher ### Overview of systemd systemd provides automatic MySQL server startup and shutdown. It also enables manual server management using the **`systemctl`** command. For example: ~~~terminal systemctl {start|stop|restart|status} mysqld ~~~ Alternatively, use the **`service`** command (with the arguments reversed), which is compatible with System V systems: ~~~terminal service mysqld {start|stop|restart|status} ~~~ >[info] Note For the **`systemctl`** or **`service`** commands, if the MySQL service name is not`mysqld`, use the appropriate name. For example, use `mysql` rather than `mysqld` on Debian-based and SLES systems. Support for systemd includes these files: * `mysqld.service`(RPM platforms),`mysql.service`(Debian platforms): systemd service unit configuration file, with details about the MySQL service. * `mysqld@.service`(RPM platforms),`mysql@.service`(Debian platforms): Like`mysqld.service`or`mysql.service`, but used for managing multiple MySQL instances. * `mysqld.tmpfiles.d`: File containing information to support the`tmpfiles`feature. This file is installed under the name`mysql.conf`. * `mysqld_pre_systemd`(RPM platforms),`mysql-system-start`(Debian platforms): Support script for the unit file. This script assists in creating the error log file only if the log location matches a pattern (`/var/log/mysql*.log`for RPM platforms,`/var/log/mysql/*.log`for Debian platforms). In other cases, the error log directory must be writable or the error log must be present and writable for the user running the **`mysqld`** process. ### Configuring systemd for MySQL #### Configuring systemd for MySQL To add or change systemd options for MySQL, these methods are available: * Use a localized systemd configuration file. * Arrange for systemd to set environment variables for the MySQL server process. * Set the `MYSQLD_OPTS` systemd variable. To use a localized systemd configuration file, create the`/etc/systemd/system/mysqld.service.d`directory if it does not exist. In that directory, create a file(`override.conf`) that contains a`[Service]`section listing the desired settings. For example: ~~~ini [Service] LimitNOFILE=max_open_files PIDFile=/path/to/pid/file Nice=nice_level LimitCore=core_file_limit Environment="LD_PRELOAD=/path/to/malloc/library" Environment="TZ=time_zone_setting" ~~~ Newer versions of systemd support the following command, which opens an editor and permits you to edit the file: ~~~ini systemctl edit mysqld # RPM platforms systemctl edit mysql # Debian platforms ~~~ Whenever you create or change`override.conf`, reload the systemd configuration, then tell systemd to restart the MySQL service: ~~~terminal systemctl daemon-reload systemctl restart mysqld # RPM platforms systemctl restart mysql # Debian platforms ~~~ With systemd, the`override.conf`configuration method must be used for certain parameters, rather than settings in a`[mysqld]`or`[mysqld_safe]`group in a MySQL option file: * For some parameters,`override.conf`must be used because systemd itself must know their values and it cannot read MySQL option files to get them. * Parameters that specify values otherwise settable only using options known to **`mysqld_safe`** must be specified using systemd because there is no corresponding **`mysqld`** parameter. You can set the following parameters in`override.conf`: * To specify the process ID file: * As of MySQL 5.7.10: Use`override.conf`and change both `PIDFile` and `ExecStart` to name the PID file path name. Any setting of the process ID file in MySQL option files is ignored. To modify `ExecStart`, it must first be cleared. For example: ~~~ini [Service] PIDFile=/var/run/mysqld/mysqld-custom.pid ExecStart= ExecStart=/usr/sbin/mysqld --pid-file=/var/run/mysqld/mysqld-custom.pid $MYSQLD_OPTS ~~~ * Before MySQL 5.7.10: Use `PIDFile` in `override.conf` rather than the `--pid-file` option for **`mysqld`** or **`mysqld_safe`**. `systemd` must know the PID file location so that it can restart or stop the server. If the PID file value is specified in a MySQL option file, the value must match the `PIDFile` value or MySQL startup may fail. * To set the number of file descriptors available to the MySQL server, use `LimitNOFILE` in `override.conf` rather than the `open_files_limit` system variable for **`mysqld`** or `--open-files-limit` option for **`mysqld_safe`** . * To set the maximum core file size, use `LimitCore` in `override.conf` rather than the `--core-file-size` option for **`mysqld_safe`** . * To set the scheduling priority for the MySQL server, use `Nice` in `override.conf` rather than the `--nice` option for **`mysqld_safe`**. There are multiple ways to specify environment variable values for use by the MySQL server process managed by `systemd`: * Use `Environment` lines in the `override.conf` file. For the syntax, see the example in the preceding discussion that describes how to use this file. * Specify the values in the `/etc/sysconfig/mysql` file (create the file if it does not exist). Assign values using the following syntax: ~~~ini LD_PRELOAD=/path/to/malloc/library TZ=time_zone_setting ~~~ After modifying`/etc/sysconfig/mysql`, restart the server to make the changes effective: ~~~terminal systemctl restart mysqld # RPM platforms systemctl restart mysql # Debian platforms ~~~ To specify options for **`mysqld`** without modifying systemd configuration files directly, set or unset the `MYSQLD_OPTS` systemd variable. For example: ~~~terminal systemctl set-environment MYSQLD_OPTS="--general_log=1" systemctl unset-environment MYSQLD_OPTS ~~~ `MYSQLD_OPTS`can also be set in the`/etc/sysconfig/mysql`file. After modifying the `systemd` environment, restart the server to make the changes effective: ~~~terminal systemctl restart mysqld # RPM platforms systemctl restart mysql # Debian platforms ~~~ For platforms that use `systemd`, the data directory is initialized if empty at server startup. This might be a problem if the data directory is a remote mount that has temporarily disappeared: The mount point would appear to be an empty data directory, which then would be initialized as a new data directory. As of MySQL 5.7.20, to suppress this automatic initialization behavior, specify the following line in the`/etc/sysconfig/mysql`file (create the file if it does not exist): ~~~ini NO_INIT=true ~~~ ### Configuring Multiple MySQL Instances Using systemd #### Migrating from mysqld\_safe to systemd Because **`mysqld_safe`** is not installed on platforms that use systemd to manage MySQL, options previously specified for that program (for example, in an`[mysqld_safe]`option group) must be specified another way: * Some **`mysqld_safe`** options are also understood by **`mysqld`** and can be moved from the`[mysqld_safe]`option group to the`[mysqld]`group. This does **not** include `--pid-file`, `--open-files-limit`, or `--nice`. To specify those options, use the`override.conf`systemd file, described previously. * For some **`mysqld_safe`** options, there are similar **`mysqld`** options. For example, the **`mysqld_safe`** option for enabling `syslog` logging is `--syslog`, which is deprecated. For **`mysqld`**, enable the `log_syslog` system variable instead. For details, see [Section 5.4.2, “The Error Log”](https://dev.mysql.com/doc/refman/5.7/en/error-log.html "5.4.2 The Error Log"). * **`mysqld_safe`** options not understood by **`mysqld`** can be specified in `override.conf` or environment variables. For example, with **`mysqld_safe`** if the server should use a specific memory allocation library, this is specified using the `--malloc-lib` option. For installations that manage the server with systemd, arrange to set the `LD_PRELOAD` environment variable instead, as described previously. ##  MySQL Installation-Related Programs [[官方文档]](https://dev.mysql.com/doc/refman/5.7/en/programs-installation.html) **comp\_err**— Compile MySQL Error Message File **mysql\_install\_db**— Initialize MySQL Data Directory **mysql\_plugin**— Configure MySQL Server Plugins **mysql\_secure\_installation**— Improve MySQL Installation Security **mysql\_ssl\_rsa\_setup**— Create SSL/RSA Files **mysql\_tzinfo\_to\_sql**— Load the Time Zone Tables **mysql\_upgrade**— Check and Upgrade MySQL Tables