如何為 MySQL 伺服器建立 systemd 服務單元設定檔?

如何為 MySQL 伺服器建立 systemd 服務單元設定檔?

更新 由於按照 Robert Longson 的建議使用 /etc/systemd/system 存在,我能夠建立 mysqld.service 文件

現在我的問題已經改變了。當我啟動服務時它失敗了。我跑了systemctl status mysqld.service,這是輸出:

   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-03-29 19:01:25 CDT; 38s ago
     Docs: man:mysqld(7)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 9251 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonize --pid
mysqld.service: Service hold-off time over, scheduling restart.
mysqld.service: Scheduled restart job, restart counter is at 5.
Stopped MySQL Server.
mysqld.service: Start request repeated too quickly.
mysqld.service: Failed with result 'exit-code'.
mysqld.service: Failedto start MySQL Server

我正在嘗試使用 systemd 啟動 MySQL Server 5.7,這是安裝後的第一次。手冊說在 /usr/lib/systemd/system 中建立設定文件但該子目錄不存在(我使用的是 Ubuntu 18.04)。我正在遵循本指南:

https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-post-install.html#secure-deployment-systemd-startup

配置 MySQL 安裝以與 systemd 一起使用:

  1. 新增 systemd 服務單元設定文件,其中包含有關 MySQL 服務的詳細資訊。該檔案名稱為 mysqld.service,位於 /usr/lib/systemd/system 中。
shell> cd /usr/lib/systemd/system
shell> touch mysqld.service
shell> chmod 644 mysqld.service

由於我沒有 systemd 的「system」子目錄,因此我只是建立了 mysql.service 文件直接地在 /usr/lib/systemd.遵循其餘說明後,我必須:

shell> systemctl start mysqld

但它失敗了:

Failed to enable unit: Unit file mysqld.service does not exist.

有人可以解釋一下我做錯了什麼嗎?謝謝。

答案1

服務檔案實際上位於 /etc/systemd/system 而不是 /usr/lib/systemd/system

如果您想找出服務失敗的原因,您可以嘗試systemctl status mysqld或查看journalctl命令以獲得更完整的日誌,例如journalctl -u mysqld -S today

相關內容