/etc/logrotate.d/mysql postrotate script not work as expected because HOME variable not set

Description

/etc/logrotate.d/mysql file:

  1. The log file name and location can be set in

  2. /etc/my.cnf by setting the "log-error" option

  3. in either [mysqld] or [mysqld_safe] section as

  4. follows:
    #

  5. [mysqld]

  6. log-error=/var/lib/mysql/mysqld.log
    #

  7. In case the root user has a password, then you

  8. have to create a /root/.my.cnf configuration file

  9. with the following content:
    #

  10. [mysqladmin]

  11. password = <secret>

  12. user= root
    #

  13. where "<secret>" is the password.
    #

  14. ATTENTION: The /root/.my.cnf file should be readable

  15. ONLY by root !

/var/lib/mysql/mysqld.log {

  1. create 600 mysql mysql
    notifempty
    daily
    rotate 5
    missingok
    compress
    postrotate

  2. just if mysqld is really running
    if test -x /usr/bin/mysqladmin && \
    /usr/bin/mysqladmin ping &>/dev/null
    then
    /usr/bin/mysqladmin flush-logs
    fi
    endscript
    }

===================================

as result - logrotate service failed with error:

  1. journalctl -u logrotate
    Apr 27 00:00:34 example.com systemd[1]: Starting Rotate log files...
    Apr 27 00:00:34 example.com logrotate[2184]: [52B blob data]
    Apr 27 00:00:34 example.com logrotate[2184]: error: 'Access denied for user 'root'@'localhost' (using password: NO)'
    Apr 27 00:00:34 example.com logrotate[2177]: error: error running shared postrotate script for '/var/log/mysqld.log
    Apr 27 00:00:34 example.com logrotate[2177]: /var/log/mysqld-slow.log
    Apr 27 00:00:34 example.com logrotate[2177]: '
    Apr 27 00:00:34 example.com systemd[1]: logrotate.service: Main process exited, code=exited, status=1/FAILURE
    Apr 27 00:00:34 example.com systemd[1]: logrotate.service: Failed with result 'exit-code'.
    Apr 27 00:00:34 example.com systemd[1]: Failed to start Rotate log files.
    Apr 28 00:00:34 example.com systemd[1]: Starting Rotate log files...
    Apr 28 00:00:34 example.com logrotate[2948]: [52B blob data]
    Apr 28 00:00:34 example.com logrotate[2948]: error: 'Access denied for user 'root'@'localhost' (using password: NO)'
    Apr 28 00:00:34 example.com logrotate[2943]: error: error running shared postrotate script for '/var/log/mysqld.log
    Apr 28 00:00:34 example.com logrotate[2943]: /var/log/mysqld-slow.log
    Apr 28 00:00:34 example.com logrotate[2943]: '
    Apr 28 00:00:34 example.com systemd[1]: logrotate.service: Main process exited, code=exited, status=1/FAILURE
    Apr 28 00:00:34 example.com systemd[1]: logrotate.service: Failed with result 'exit-code'.
    Apr 28 00:00:34 example.com systemd[1]: Failed to start Rotate log files.
    Apr 29 00:00:34 example.com systemd[1]: Starting Rotate log files...
    Apr 29 00:00:34 example.com logrotate[3708]: [52B blob data]
    Apr 29 00:00:34 example.com logrotate[3708]: error: 'Access denied for user 'root'@'localhost' (using password: NO)'
    Apr 29 00:00:34 example.com logrotate[3703]: error: error running shared postrotate script for '/var/log/mysqld.log
    Apr 29 00:00:34 example.com logrotate[3703]: /var/log/mysqld-slow.log
    Apr 29 00:00:34 example.com logrotate[3703]: '
    Apr 29 00:00:34 example.com systemd[1]: logrotate.service: Main process exited, code=exited, status=1/FAILURE
    Apr 29 00:00:34 example.com systemd[1]: logrotate.service: Failed with result 'exit-code'.
    Apr 29 00:00:34 example.com systemd[1]: Failed to start Rotate log files.

=========================================

file /root/.my.cnf exists and user and password is present:

/root/.my.cnf

[client]
user=root
password=******************

if run from root shell - command work fine, without errors:

/usr/bin/mysqladmin flush-logs ; echo $?
0

but from the logrotate this command does not work as expected.

strace executing /usr/bin/mysqladmin flush-logs from the logrotate service:

newfstatat(AT_FDCWD, "~/.my.cnf", 0x7ffcd1802560, 0) = -1 ENOENT (No such file or directory)

strace executing /usr/bin/mysqladmin flush-logs from the root shell:

newfstatat(AT_FDCWD, "/root/.my.cnf", {st_mode=S_IFREG|0644, st_size=42, ...}, 0) = 0

===================================

root cause of the bug:

in the normal mode env. variable

HOME=/root

present, but if executing /usr/bin/mysqladmin flush-logs from the logrotate service - env. variable HOME is absent, and /usr/bin/mysqladmin can't read /root/.my.cnf file and can't read password for connecting to database.

executing command

mysql -e 'FLUSH LOGS'

has the same bug as command

/usr/bin/mysqladmin flush-logs

Environment

Rocky Linux 9.1
Percona Server for MySQL 8.0.32-24 for official repo

Activity

Show:

Aaditya Dubey October 19, 2023 at 7:57 AM

Hi ,

We still haven't heard any news from you. So I assume issue doesn't persists anymore and will close the ticket. If you disagree just reply and create a follow-up.

Aaditya Dubey June 23, 2023 at 2:53 PM

Hi ,

Thank you for the report.
I've unable to repeat the issue from my end, Could you please share exact reproducible steps to further debug the issue?

Gena Makhomed April 30, 2023 at 11:01 AM

adding export HOME=/root to /etc/logrotate.d/mysql file in postrotate script section will be only workaround, root cause of this bug in the mysql/mysqladmin binary. May be root cause of bug also should be fixed? Not only workaround applied?

Incomplete

Details

Assignee

Reporter

Needs QA

Yes

Components

Affects versions

Priority

Smart Checklist

Created April 29, 2023 at 9:56 PM
Updated March 6, 2024 at 9:42 AM
Resolved October 19, 2023 at 7:57 AM