Unix 及类 Unix 系统上的 MySQL 发行版包含一个名为 mysql.server 的脚本,它使用 mysqld_safe 启动 MySQL 服务器。它可用于如 Linux 和 Solaris 等使用 System V 风格运行目录来启动和停止系统服务的系统。它也被 macOS 上的 MySQL 启动项使用。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 是在 MySQL 源代码树中使用的脚本名称。安装后的名称可能不同(例如,mysqld 或 mysql)。在下面的讨论中,请根据您的系统适当调整名称 mysql.server。mysql.server is the script name as used within the MySQL source tree. The installed name might be different (for example, mysqld or mysql). In the following discussion, adjust the name mysql.server as appropriate for your system.
对于某些 Linux 平台,从 RPM 或 Debian 包安装 MySQL 时会包括用于管理 MySQL 服务器启动和关闭的 systemd 支持。在这些平台上,mysql.server 和 mysqld_safe 不会被安装,因为它们是不必要的。有关更多信息,请参阅 第 2.5.9 节,“使用 systemd 管理 MySQL 服务器”。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 Section 2.5.9, “Managing MySQL Server with systemd”.
要使用 mysql.server 脚本手动启动或停止服务器,请在命令行中使用 start 或 stop 参数调用它:To start or stop the server manually using the
mysql.server script, invoke it from the
command line with start or
stop arguments:
mysql.server start mysql.server stop
mysql.server 会更改位置到 MySQL 安装目录,然后调用 mysqld_safe。要以某个特定用户身份运行服务器,请在全局 /etc/my.cnf 选项文件的 [mysqld] 组中添加适当的 user 选项,如本节后面所示。(如果您在非标准位置安装了 MySQL 的二进制发行版,可能必须编辑 mysql.server。在运行 mysqld_safe 之前,将其修改为进入正确的目录。如果您这样做,将来升级 MySQL 时,您修改过的 mysql.server 可能会被覆盖;请保留一份已编辑版本的副本,以便重新安装。)mysql.server changes location to the MySQL
installation directory, then invokes
mysqld_safe. To run the server as some
specific user, add an appropriate user option
to the [mysqld] group of the global
/etc/my.cnf option file, as shown later in
this section. (It is possible that you must edit
mysql.server if you've installed a binary
distribution of MySQL in a nonstandard location. Modify it to
change location into the proper directory before it runs
mysqld_safe. If you do this, your modified
version of mysql.server may be overwritten if
you upgrade MySQL in the future; make a copy of your edited
version that you can reinstall.)
mysql.server stop 通过向服务器发送信号来停止服务器。你也可以通过执行 mysqladmin shutdown 来手动停止服务器。mysql.server stop stops the server by sending a signal to it. You can also stop the server manually by executing mysqladmin shutdown.
要在你的服务器上自动启动和停止 MySQL,必须在你的 /etc/rc* 文件中的适当位置添加启动和停止命令:To start and stop MySQL automatically on your server, you must
add start and stop commands to the appropriate places in your
/etc/rc* files:
如果您使用 Linux 服务器 RPM 包(
MySQL-server-)或本地 Linux 软件包安装,mysql.server 脚本可能会安装在VERSION.rpm/etc/init.d目录下,名称为mysqld或mysql。有关 Linux RPM 包的更多信息,请参见 第 2.5.4 节,“使用 Oracle 的 RPM 软件包在 Linux 上安装 MySQL”。If you use the Linux server RPM package (MySQL-server-), or a native Linux package installation, the mysql.server script may be installed in theVERSION.rpm/etc/init.ddirectory with the namemysqldormysql. See Section 2.5.4, “Installing MySQL on Linux Using RPM Packages from Oracle”, for more information on the Linux RPM packages.如果你是从源码发行版安装 MySQL 或使用不自动安装 mysql.server 的二进制发行格式安装 MySQL,你可以手动安装此脚本。它可以在 MySQL 安装目录下的
support-files目录中找到,或者在 MySQL 源代码树中找到。将该脚本复制到/etc/init.d目录下,并命名为 mysql,并使其可执行:If you install MySQL from a source distribution or using a binary distribution format that does not install mysql.server automatically, you can install the script manually. It can be found in thesupport-filesdirectory under the MySQL installation directory or in a MySQL source tree. Copy the script to the/etc/init.ddirectory with the name mysql and make it executable:cp mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql
安装脚本后,使其在系统启动时运行所需的命令取决于你的操作系统。在 Linux 上,你可以使用 chkconfig:After installing the script, the commands needed to activate it to run at system startup depend on your operating system. On Linux, you can use chkconfig:
chkconfig --add mysql
在一些 Linux 系统上,似乎还需要以下命令才能完全启用 mysql 脚本:On some Linux systems, the following command also seems to be necessary to fully enable the mysql script:
chkconfig --level 345 mysql on
在 FreeBSD 上,启动脚本通常应放在
/usr/local/etc/rc.d/。将mysql.server脚本安装为/usr/local/etc/rc.d/mysql.server.sh以启用自动启动。rc(8)手册页指出,只有当该目录中的脚本的基本名称匹配*.shshell 文件名模式时,才会执行该脚本。目录中存在的其他文件或目录将被静默忽略。On FreeBSD, startup scripts generally should go in/usr/local/etc/rc.d/. Install themysql.serverscript as/usr/local/etc/rc.d/mysql.server.shto enable automatic startup. Therc(8)manual page states that scripts in this directory are executed only if their base name matches the*.shshell file name pattern. Any other files or directories present within the directory are silently ignored.作为前述设置的替代方案,一些操作系统也使用
/etc/rc.local或/etc/init.d/boot.local在启动时启动额外的服务。要使用此方法启动 MySQL,请将如下命令追加到相应的启动文件中:As an alternative to the preceding setup, some operating systems also use/etc/rc.localor/etc/init.d/boot.localto start additional services on startup. To start up MySQL using this method, append a command like the one following to the appropriate startup file:/bin/sh -c 'cd /usr/local/mysql; ./bin/mysqld_safe --user=mysql &'
对于其他系统,请查阅您的操作系统文档以了解如何安装启动脚本。For other systems, consult your operating system documentation to see how to install startup scripts.
mysql.server 从选项文件的 [mysql.server] 和 [mysqld] 部分读取选项。为了向后兼容,它也会读取 [mysql_server] 部分,但为了保持最新,您应该将这些部分重命名为 [mysql.server]。mysql.server reads options from the
[mysql.server] and
[mysqld] sections of option files. For
backward compatibility, it also reads
[mysql_server] sections, but to be current
you should rename such sections to
[mysql.server].
您可以在全局 /etc/my.cnf 文件中为 mysql.server 添加选项。一个典型的 my.cnf 文件可能如下所示:You can add options for mysql.server in a
global /etc/my.cnf file. A typical
my.cnf file might look like this:
[mysqld] datadir=/usr/local/mysql/var socket=/var/tmp/mysql.sock port=3306 user=mysql [mysql.server] basedir=/usr/local/mysql
mysql.server 脚本支持下表中显示的选项。如果指定了,它们 必须 放在选项文件中,而不是命令行上。mysql.server 仅支持 start 和 stop 作为命令行参数。The mysql.server script supports the options
shown in the following table. If specified, they
must be placed in an option file, not on
the command line. mysql.server supports only
start and stop as
command-line arguments.
表 6.8 mysql.server 选项文件选项Table 6.8 mysql.server Option-File Options
| 选项名称Option Name | 说明Description | 类型Type |
|---|---|---|
basedir |
MySQL 安装目录的路径Path to MySQL installation directory | 目录名称Directory name |
datadir |
MySQL 数据目录的路径Path to MySQL data directory | 目录名称Directory name |
pid-file |
服务器应将其进程 ID 写入的文件File in which server should write its process ID | 文件名File name |
service-startup-timeoutservice-startup-timeout |
等待服务器启动需要多久How long to wait for server startup | 整数Integer |
basedir=dir_namebasedir=dir_nameMySQL 安装目录的路径。The path to the MySQL installation directory.
datadir=dir_namedatadir=dir_nameMySQL 数据目录的路径。The path to the MySQL data directory.
pid-file=file_namepid-file=file_name服务器应写入其进程 ID 的文件的路径名。除非给出绝对路径名以指定不同的目录,否则服务器会在数据目录中创建该文件。The path name of the file in which the server should write its process ID. The server creates the file in the data directory unless an absolute path name is given to specify a different directory.
如果未提供此选项,mysql.server 使用默认值
。传递给 mysqld_safe 的 PID 文件值会覆盖host_name.pid[mysqld_safe]选项文件组中指定的任何值。由于 mysql.server 会读取[mysqld]选项文件组,但不会读取[mysqld_safe]组,因此您可以通过在[mysqld_safe]和[mysqld]组中放置相同的pid-file设置,确保从 mysql.server 调用 mysqld_safe 时得到的值与手动调用时相同。If this option is not given, mysql.server uses a default value of. The PID file value passed to mysqld_safe overrides any value specified in thehost_name.pid[mysqld_safe]option file group. Because mysql.server reads the[mysqld]option file group but not the[mysqld_safe]group, you can ensure that mysqld_safe gets the same value when invoked from mysql.server as when invoked manually by putting the samepid-filesetting in both the[mysqld_safe]and[mysqld]groups.service-startup-timeout=secondsservice-startup-timeout=seconds等待服务器启动确认的时间(以秒为单位)。如果服务器未能在此时间内启动,mysql.server 将以错误退出。默认值为 900。值为 0 表示不等待启动。负值表示永久等待(无超时)。How long in seconds to wait for confirmation of server startup. If the server does not start within this time, mysql.server exits with an error. The default value is 900. A value of 0 means not to wait at all for startup. Negative values mean to wait forever (no timeout).