Mysql

MySQL在旋轉後不會將錯誤記錄到新文件中?

  • March 21, 2020

問題已解決,但我正在寫下來以備將來參考。

/root/.my.cnf

[mysqladmin]
user            = root
password        = pa$$w0rd

/etc/logrotate.d/mysql

/var/log/mysql-slow.log /var/log/mysqld.log {
   daily
   rotate 7
   dateext
   compress
   missingok
   #notifempty
   sharedscripts
   create 644 mysql mysql
   postrotate
       /usr/bin/mysqladmin flush-logs
   endscript
}

logrotate從命令行執行時工作正常:

# logrotate -v -f /etc/logrotate.d/mysql

但在凌晨 4 點從 cron 執行時它不起作用日誌文件已輪換,但 MySQL 不會將錯誤記錄到新創建的文件中:

-rw-r--r-- 1 mysql mysql      0 Aug  7 10:13 /var/log/mysqld.log
-rw-r--r-- 1 mysql mysql     20 Aug  4 04:04 /var/log/mysqld.log-20120804.gz
-rw-r--r-- 1 mysql mysql     20 Aug  5 04:04 /var/log/mysqld.log-20120805.gz
-rw-r--r-- 1 mysql mysql     20 Aug  6 16:28 /var/log/mysqld.log-20120806.gz

在 中postrotate,我將 stderr 和 stdout 都重定向到日誌文件以查看會發生什麼:

postrotate
   /usr/bin/mysqladmin flush-logs > /var/log/mysqladmin.flush-logs 2>&1
endscript

我得到的是:

/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

聽起來好像在 logrotate 期間mysqladmin不讀取。/root/.my.cnf

所以,試試這個:

postrotate
   env HOME=/root/ /usr/bin/mysqladmin flush-logs > /var/log/mysqladmin.flush-logs 2>&1
endscript

來源:

引用自:https://serverfault.com/questions/415774