文件首頁
MySQL 9.0 參考手冊
相關文件 下載本手冊
PDF (US Ltr) - 40.0Mb
PDF (A4) - 40.1Mb
Man Pages (TGZ) - 258.2Kb
Man Pages (Zip) - 365.3Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


MySQL 9.0 參考手冊  /  ...  /  安裝和使用 MySQL 啟動守護程式

2.4.3 安裝和使用 MySQL 啟動守護程式

macOS 使用啟動守護程式來自動啟動、停止和管理程序和應用程式,例如 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 中定義這些選項。