Ubuntu上更改MySQL數據庫數據存儲目錄


之前寫過一篇博客MySQL更改數據庫數據存儲目錄,當時的測試環境是RHEL和CentOS,誰想最近在Ubuntu下面更改MySQL數據庫數據存儲目錄時遇到了之前未遇到的問題,之前的經驗用不上了(或者說之前的總結不是太全面),修改完MySQL數據庫數據存儲目錄后重啟MySQL,發現MySQL服務無法啟動。

 

root@mylnx2:/etc/mysql/mysql.conf.d# service mysql start
 
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

 

連MySQL的錯誤日志也未生成,使用service mysql status命令可以輸出一些較詳細的信息,如下所示:

 

root@mylnx2:/etc/mysql/mysql.conf.d# service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: activating (start-post) (Result: exit-code) since Mon 2018-10-15 22:33:00 CST; 28s ago
  Process: 12947 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
  Process: 12932 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 12947 (code=exited, status=1/FAILURE);         : 12948 (mysql-systemd-s)
    Tasks: 2
   Memory: 1.9M
      CPU: 367ms
   CGroup: /system.slice/mysql.service
           └─control
             ├─12948 /bin/bash /usr/share/mysql/mysql-systemd-start post
             └─13045 sleep 1
 
Oct 15 22:33:00 mylnx2 systemd[1]: Starting MySQL Community Server...
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.013763Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.013836Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.207533Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.207663Z 0 [Warning] Can't create test file /mysql_data/mysql/mylnx2.lower-test
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.207717Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.23-0ubuntu0.16.04.1-log) starting as process 129
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.215413Z 0 [Warning] Can't create test file /mysql_data/mysql/mylnx2.lower-test
Oct 15 22:33:01 mylnx2 mysqld[12947]: 2018-10-15T14:33:01.215445Z 0 [Warning] Can't create test file /mysql_data/mysql/mylnx2.lower-test
Oct 15 22:33:01 mylnx2 systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE

 

 

錯誤信息為[Warning] Can't create test file /mysql_data/mysql/mylnx2.lower-test。其實這里是踩到了AppArmor這個坑,之前對Ubuntu了解不多,所以直到遇到這個問題,才了解、知道這么個概念。下面是百科對AppArmor的介紹:

 

AppArmor是一個高效和易於使用的Linux系統安全應用程序。AppArmor對操作系統和應用程序所受到的威脅進行從內到外的保護,甚至是未被發現的0day漏洞和未知的應用程序漏洞所導致的攻擊。AppArmor安全策略可以完全定義個別應用程序可以訪問的系統資源與各自的特權。AppArmor包含大量的默認策略,它將先進的靜態分析和基於學習的工具結合起來,AppArmor甚至可以使非常復雜的應用可以使用在很短的時間內應用成功。

 

AppArmor對MySQL所能使用的目錄權限做了限制,如下截圖所示,規定了MySQL使用的數據文件路徑權限。

 

# cat /etc/apparmor.d/usr.sbin.mysqld

 

clip_image001

 

我將MySQL的數據庫數據存儲目錄從/var/lib/mysql 切換到/mysql_data/mysql下面。所以就遇到了上面錯誤,需要修改或新增兩條記錄,從而使mysqld可以使用/mysql_data/mysql這個目錄

 

 /mysql_data/mysql/ r,

 /mysql_data/mysql/** rwk,

 

然后重啟AppArmor服務后,然后就可以重啟MySQL服務了。

 

sudo service apparmor restart

 

當然/etc/apparmor.d/usr.sbin.mysqld還有Allow plugin access需要調整,這個不是重點,在此略過。

 

猶豫了一會,還是記錄一下這個小小案例!雖然網上已有不少人總結這個,但是自己動手總結一下,印象也深刻一點!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM