Ubuntu

權限被拒絕寫入 mysql 日誌

  • June 8, 2015

我只是在 Vagrant 上測試一個新的 Ubuntu (Vivid 15.04) 安裝,並在 mysql 和登錄到自定義位置時遇到問題。

/var/log/syslog我得到

/usr/bin/mysqld_safe: cannot create /var/log/mysqld.log: Permission denied

如果ls -l /var我得到

drwxrwxr-x 10 root syslog 4096 Jun  8 19:52 log

如果我查看 /var/log 文件不存在

我以為我暫時禁用了 apparmor 只是為了隔離它是否是導致問題的原因或其他原因,但不確定它是否仍然會產生問題(編輯:認為它可能仍然被啟用,所以不確定這是一個問題還是簡單權限)。

如果我嘗試手動將文件創建為 mysql,我也會被拒絕(我臨時允許它 bash 訪問測試,之後我將刪除)。

touch /var/log/mysql.log
touch: cannot touch ‘/var/log/mysql.log’: Permission denied

如果我查看另一個正在執行的伺服器(centos),它具有上述權限(並以 mysql 使用者身份寫入),所以我想知道 mysql 通常如何獲得訪問 /var/log 目錄的權限,以及如何獲得它通過正常執行訪問該文件夾?

這是我對 mysql 的 apparmor 配置文件


/usr/sbin/mysqld {
 #include 
 #include 
 #include 
 #include 
 #include 

 capability dac_override,
 capability sys_resource,
 capability setgid,
 capability setuid,

 network tcp,

 /etc/hosts.allow r,
 /etc/hosts.deny r,

 /etc/mysql/** r,
 /usr/lib/mysql/plugin/ r,
 /usr/lib/mysql/plugin/*.so* mr,
 /usr/sbin/mysqld mr,
 /usr/share/mysql/** r,
 /var/log/mysqld.log rw,
 /var/log/mysqld.err rw,
 /var/lib/mysql/ r,
 /var/lib/mysql/** rwk,
 /var/log/mysql/ r,
 /var/log/mysql/* rw,
 /var/run/mysqld/mysqld.pid rw,
 /var/run/mysqld/mysqld.sock w,
 /run/mysqld/mysqld.pid rw,
 /run/mysqld/mysqld.sock w,

 /sys/devices/system/cpu/ r,

/var/log/mysqld.log rw,

 # Site-specific additions and overrides. See local/README for details.
 #include 
}

我還將上述文件添加到 apparmor.d/disable 目錄中

注意:我添加了這一行/var/log/mysqld.log rw,它最初並不存在,並且有同樣的問題(在重新載入 apparmor 之後)。


apparmor module is loaded.
5 profiles are loaded.
5 profiles are in enforce mode.
  /sbin/dhclient
  /usr/lib/NetworkManager/nm-dhcp-client.action
  /usr/lib/NetworkManager/nm-dhcp-helper
  /usr/lib/connman/scripts/dhclient-script
  /usr/sbin/tcpdump
0 profiles are in complain mode.
1 processes have profiles defined.
1 processes are in enforce mode.
  /sbin/dhclient (565) 
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

Jun  8 20:33:33 vagrant-ubuntu-vivid-64 systemd[1]: Starting MySQL Community Server...
Jun  8 20:33:33 vagrant-ubuntu-vivid-64 mysqld_safe[11231]: 150608 20:33:33 mysqld_safe Logging to '/var/log/mysqld.log'.
Jun  8 20:33:33 vagrant-ubuntu-vivid-64 mysqld_safe[11231]: touch: cannot touch ‘/var/log/mysqld.log’: Permission denied
Jun  8 20:33:33 vagrant-ubuntu-vivid-64 mysqld_safe[11231]: chmod: cannot access ‘/var/log/mysqld.log’: No such file or directory
Jun  8 20:33:33 vagrant-ubuntu-vivid-64 mysqld_safe[11231]: 150608 20:33:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jun  8 20:33:33 vagrant-ubuntu-vivid-64 mysqld_safe[11231]: /usr/bin/mysqld_safe: 126: /usr/bin/mysqld_safe: cannot create /var/log/mysqld.log: Permission denied

在我看來,大多數人創建一個名為mysqlinside 的目錄/var/log,將此文件夾的所有者更改為 mysql 使用者。

sudo mkdir /var/log/mysql
sudo chown mysql:mysql /var/log/mysql   

那應該這樣做。請務必更新伺服器的日誌記錄位置並重新啟動它。測試後重新啟用 mysql 的 apparmor 配置文件。

引用自:https://unix.stackexchange.com/questions/208328