Documentation Home
MySQL 8.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 39.8Mb
PDF (A4) - 39.9Mb
Man Pages (TGZ) - 257.9Kb
Man Pages (Zip) - 364.9Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


MySQL 8.4 Reference Manual  /  ...  /  Installing and Using the MySQL Launch Daemon

2.4.3 MySQL 启动守护程序的安装和使用

macOS 使用 launch daemons 自动启动、停止和管理进程和应用程序,如 MySQL。

默认情况下,macOS 安装包(DMG)安装了一个名为 /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist 的 launchd 文件,该文件包含类似的 plist 定义:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>             <string>com.oracle.oss.mysql.mysqld</string>
    <key>ProcessType</key>       <string>Interactive</string>
    <key>Disabled</key>          <false/>
    <key>RunAtLoad</key>         <true/>
    <key>KeepAlive</key>         <true/>
    <key>SessionCreate</key>     <true/>
    <key>LaunchOnlyOnce</key>    <false/>
    <key>UserName</key>          <string>_mysql</string>
    <key>GroupName</key>         <string>_mysql</string>
    <key>ExitTimeOut</key>       <integer>600</integer>
    <key>Program</key>           <string>/usr/local/mysql/bin/mysqld</string>
    <key>ProgramArguments</key>
        <array>
            <string>/usr/local/mysql/bin/mysqld</string>
            <string>--user=_mysql</string>
            <string>--basedir=/usr/local/mysql</string>
            <string>--datadir=/usr/local/mysql/data</string>
            <string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
            <string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
            <string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
            <string>--keyring-file-data=/usr/local/mysql/keyring/keyring</string>
            <string>--early-plugin-load=keyring_okv=keyring_okv.so</string>
        </array>
    <key>WorkingDirectory</key>  <string>/usr/local/mysql</string>
</dict>
</plist>
Note

一些用户报告,添加 plist DOCTYPE 声明会导致 launchd 操作失败,尽管它通过 lint 检查通过。我们怀疑这是一个复制-粘贴错误。该文件的 md5 检验和为 d925f05f6d1b6ee5ce5451b596d6baed

要启用 launchd 服务,可以选择以下方法:

  • 打开 macOS 系统偏好设置,选择 MySQL偏好面板,然后执行 启动 MySQL 服务器

    图 2.6 MySQL 偏好面板:位置

    Shows "MySQL" typed into the macOS System Preferences search box, and a highlighted "MySQL" icon in the bottom left portion of the MySQL Preference Pane.

    “实例”页面包括一个选项来启动或停止 MySQL,以及 初始化数据库,重新创建 data/ 目录。 卸载 卸载 MySQL 服务器和可选的 MySQL 偏好面板和 launchd 信息。

    图 2.7 MySQL 偏好面板:实例

    The left side shows a list of MySQL instances separated by "Active Instance", "Installed Instances", and "Data Directories" sections. The right side shows a "Stop MySQL Server" button, a check box titled "Start MySQL when your computer starts up", and "Initialize Database" and "Uninstall" buttons.

  • 或者,手动加载 launchd 文件。

    $> cd /Library/LaunchDaemons
    $> sudo launchctl load -F com.oracle.oss.mysql.mysqld.plist
  • 要配置 MySQL 自动启动在引导时,可以:

    $> sudo launchctl load -w com.oracle.oss.mysql.mysqld.plist
Note

升级过程将替换您的现有 launchd 文件名为 com.oracle.oss.mysql.mysqld.plist

其他 launchd 相关信息:

  • plist条目将overridemy.cnf条目,因为它们作为命令行参数传递。关于传递程序选项的更多信息,请见第6.2.2节,“指定程序选项”

  • ProgramArguments部分定义了将传递到程序中的命令行选项,这在本例中是mysqld二进制文件。

  • 默认的plist定义是为简单的用途编写的。对于更复杂的设置,您可能想删除一些参数,而是依靠MySQL配置文件,例如my.cnf

  • 如果您编辑了plist文件,那么在重新安装或升级MySQL时请勿选择安装选项。否则,您的编辑过的plist文件将被覆盖,所有编辑都将丢失。

由于默认的plist定义定义了多个ProgramArguments,因此您可能想删除大多数这些参数,而是依靠您的my.cnfMySQL配置文件来定义它们。例如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>             <string>com.oracle.oss.mysql.mysqld</string>
    <key>ProcessType</key>       <string>Interactive</string>
    <key>Disabled</key>          <false/>
    <key>RunAtLoad</key>         <true/>
    <key>KeepAlive</key>         <true/>
    <key>SessionCreate</key>     <true/>
    <key>LaunchOnlyOnce</key>    <false/>
    <key>UserName</key>          <string>_mysql</string>
    <key>GroupName</key>         <string>_mysql</string>
    <key>ExitTimeOut</key>       <integer>600</integer>
    <key>Program</key>           <string>/usr/local/mysql/bin/mysqld</string>
    <key>ProgramArguments</key>
        <array>
            <string>/usr/local/mysql/bin/mysqld</string>
            <string>--user=_mysql</string>
            <string>--basedir=/usr/local/mysql</string>
            <string>--datadir=/usr/local/mysql/data</string>
            <string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
            <string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
            <string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
            <string>--keyring-file-data=/usr/local/mysql/keyring/keyring</string>
            <string>--early-plugin-load=keyring_okv=keyring_okv.so</string>
        </array>
    <key>WorkingDirectory</key>  <string>/usr/local/mysql</string>
</dict>
</plist>

在这种情况下,basedirdatadirplugin_dirlog_errorpid_file和--early-plugin-load选项从默认的plist ProgramArguments定义中被删除,您可能在my.cnf文件中定义了该定义。