修改 /etc/my.cnf 的 log-error 無效,日誌仍寫入 /var/log/mysqld.log。
CentOS/RHEL 系統啟動腳本 /etc/init.d/mysqld 通過 get_mysql_option 從 my.cnf 讀取參數,但啟動參數優先級高於配置文件。腳本中默認值硬編碼:
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" errlogfile="$result"
若 my.cnf 未顯式設置,腳本直接使用默認值傳給 mysqld_safe。
vim /etc/init.d/mysqld # 找到 get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" # 改為 get_mysql_option mysqld_safe log-error "/new/path/mysql-error.log" # 保存後重啟服務 service mysql restart
# /etc/my.cnf [mysqld_safe] log-error = /new/path/mysql-error.log [mysqld] # 可選:也同步寫這裡防止直接啟動 mysqld 時生效 log-error = /new/path/mysql-error.log
重啟服務:
service mysql restart # 驗證 ps aux | grep mysqld # 應見 --log-error=/new/path/mysql-error.log
tail -f /new/path/mysql-error.log # 應看到啟動信息
[mysqld] general_log_file = /new/path/general.log slow_query_log_file = /new/path/slow.log
動態生效(無需重啟):
SET GLOBAL general_log_file = '/new/path/general.log'; SET GLOBAL slow_query_log_file = '/new/path/slow.log';
yum update mysql-server 可能覆蓋,建議配合 chattr +i /etc/init.d/mysqld 鎖定或建立 RPM 觸發腳本自動修補。chown mysql:mysql,否則啟動失敗無明顯報錯。