首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

MySQL 修改 datadir 導致啟動失敗:SELinux 權限阻擋排查實錄

     編輯:MySQL 綜合教學

MySQL 修改 datadir 導致啟動失敗:SELinux 權限阻擋排查實錄

CentOS 6.2 修改 /etc/my.cnf 的 datadir 到掛載的共享目錄 /mnt/hgfs/mysql_data,權限 777、擁有者 root,啟動報錯 Errcode: 13 (Permission denied)。真兇是 SELinux 強制訪問控制。

錯誤日志關鍵信息

120609 11:35:12 mysqld_safe Starting mysqld daemon with databases from /mnt/hgfs/mysql_data
120609 11:35:13 [Warning] Can't create test file /mnt/hgfs/mysql_data/data.lower-test
/usr/libexec/mysqld: Can't change dir to '/mnt/hgfs/mysql_data/' (Errcode: 13)
120609 11:35:13 [ERROR] Aborting

排查邏輯

  1. 文件系統權限:ls -ld /mnt/hgfs/mysql_data → drwxrwxrwx root root ✅
  2. 父目錄權限:ls -ld /mnt/hgfs → 有 rx ✅
  3. MySQL 進程用戶:mysql 用戶組 ✅
  4. SELinux 上下文:ls -Z /mnt/hgfs/mysql_data → 類型可能為 vmware_shared_t 或 nfs_t,**非 mysqld_db_t**,mysqld 進程無權訪問。

解決方案

方案一:臨時放行(驗證用)

getenforce          # Enforcing
setenforce 0        # 切換 Permissive(僅記錄不阻擋)
getenforce          # Permissive
service mysqld start  # 成功啟動

方案二:永久關閉(生產環境慎用)

vi /etc/selinux/config
# SELINUX=enforcing → SELINUX=disabled
# 重啟 OS 生效

方案三:正規軍規 —— 修改 SELinux 文件上下文(推薦)

# 1. 安裝策略管理工具
yum install -y policycoreutils-python

# 2. 為新 datadir 打標
semanage fcontext -a -t mysqld_db_t "/mnt/hgfs/mysql_data(/.*)?"

# 3. 恢復上下文
restorecon -Rv /mnt/hgfs/mysql_data

# 4. 驗證
ls -Z /mnt/hgfs/mysql_data
# 應顯示 system_u:object_r:mysqld_db_t:s0

# 5. 重啟 MySQL(Enforcing 模式下)
setenforce 1
service mysqld start  # 成功

SELinux 核心概念速記

模式命令行為
Enforcingsetenforce 1強制阻擋違規訪問,記錄審計日志
Permissivesetenforce 0不阻擋,僅記錄警告(調試用)
DisabledSELINUX=disabled + 重啟完全關閉 SELinux

最佳實踐

  1. 規劃階段即確定 datadir 路徑,預先打好 mysqld_db_t 標籤。
  2. 使用獨立磁盤/分區掛載作為 datadir,避免共享文件系統(VMware hgfs、NFS)引入 SELinux/性能/一致性風險。
  3. 生產環境**保持 Enforcing**,通過 semanage / restorecon 管理上下文,而非關閉 SELinux。

權限 777 ≠ 可訪問。SELinux 是隱形守門人,ls -Z 看上下文,semanage 改標籤,才是正規軍規。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved