Mysql

移動到新目錄後啟動 mysql 時出錯

  • March 8, 2020
Ubuntu 18.04
mysql  Ver 14.14 Distrib 5.7.29, for Linux (x86_64)

我使用以下命令將我的 mysql 安裝從預設/var/lib/mysql位置移動到外部驅動器:

sudo /etc/init.d/mysql stop
sudo rsync -av /var/lib/mysql /media/E
sudo mv /var/lib/mysql /var/lib/mysql.bak

然後編輯/etc/mysql/mysql.conf.d/mysqld.cnf文件以將 datadir 參數指向其新位置media/E/mysql

嘗試重新啟動mysql,我得到:

[....] Starting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.

在一次偶然的機會中,我返回並編輯了文件中的log_error參數mysql.conf.d以指向它的新位置(因為它以前指向原始/var/lib/mysql位置)。仍然無法啟動,並留下以下輸出journalctl -xe

chris@chris-x1c6:/usr/share/mysql$ journalctl -xe
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 audit[26261]: AVC apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/media/E/mysql/error.log" pid=26261 comm="mysqld" reque
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.356540Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.357247Z 0 [Warning] Can't create test file /media/E/mysql/chris-x1c6.lower-test
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.357287Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.29-0ubuntu0.18.04.1) starting as process 26261 ...
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359205Z 0 [Warning] Can't create test file /media/E/mysql/chris-x1c6.lower-test
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359247Z 0 [Warning] Can't create test file /media/E/mysql/chris-x1c6.lower-test
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359818Z 0 [ERROR] Could not open file '/media/E/mysql/error.log' for error logging: Permission denied
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359828Z 0 [ERROR] Aborting
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359846Z 0 [Note] Binlog end
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: Initialization of mysqld failed: 0
Feb 29 12:36:46 chris-x1c6 mysqld[26259]: 2020-02-29T20:36:46.359874Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
Feb 29 12:36:46 chris-x1c6 systemd[1]: mysql.service: Control process exited, code=exited status=1
Feb 29 12:36:46 chris-x1c6 systemd[1]: mysql.service: Failed with result 'exit-code'.
Feb 29 12:36:46 chris-x1c6 systemd[1]: Failed to start MySQL Community Server.

我找到了其他建議調整 AppArmor 策略設置的答案(這裡這裡),但似乎有點笨拙和疑惑:

(1) 如果我在移動現有的 mysql 安裝時做錯了什麼;

(2) 是否有比編輯 AppArmor 策略文件更好的解決方案。

經過一些試驗和錯誤,能夠使用這種方法使其工作, 修改文件中的log_error變數mysqld.cnf以指向新位置。

需要添加一行來處理 AppArmor(它包含在連結的教程中),但在其他方面可以正常工作。

引用自:https://dba.stackexchange.com/questions/260912