文件首頁
MySQL 8.4 參考手冊
相關文件 下載本手冊
PDF (美式信紙) - 39.9Mb
PDF (A4) - 40.0Mb
Man Pages (TGZ) - 258.5Kb
Man Pages (Zip) - 365.5Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


MySQL 8.4 參考手冊  /  ...  /  安裝與使用 MySQL Launch Daemon

2.4.3 安裝與使用 MySQL Launch Daemon

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>
注意

一些使用者回報,加入 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
注意

升級程序會取代您現有的 launchd 檔案,名為 com.oracle.oss.mysql.mysqld.plist

其他 launchd 相關資訊

  • plist 項目會覆寫 my.cnf 項目,因為它們是以命令列引數傳遞。有關傳遞程式選項的詳細資訊,請參閱章節 6.2.2,“指定程式選項”

  • ProgramArguments 區段定義傳遞到程式的命令列選項,在此案例中為 mysqld 二進位檔。

  • 預設的 plist 定義是以較不複雜的使用案例為考量。對於較複雜的設定,您可能會想要移除某些引數,而是依賴 MySQL 設定檔,例如 my.cnf

  • 如果您編輯 plist 檔案,則在重新安裝或升級 MySQL 時取消勾選安裝程式選項。否則,您編輯的 plist 檔案會被覆寫,且所有編輯都會遺失。

由於預設的 plist 定義定義了幾個 ProgramArguments,您可能會移除這些引數中的大部分,而是依賴您的 my.cnf MySQL 設定檔來定義它們。例如

<?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 中定義這些選項。