所以我启动了一个mysql Docker并运行了3个日志文件(常规,错误,慢查询日志),这些文件已写入/ var / log / mysql /(mysql容器内的路径),实际上是一个Docker主机上的目录(名为'log'),并作为docker-compose.yml中指定的卷安装到容器中。
我们选择这种方式,是因为我们不希望在stdout上合并常规日志和慢查询日志,并且我们希望每天轮流使用3个单独的日志文件,因为对于我们而言,查找发出的特定查询似乎更方便-假设-4天前。
由于mysql Docker(afaik)不附带logrotate和/或cron,我们决定在docker-compose.yml中添加另一个名为logrotator的服务,该服务在其入口点启动cron,并依次定期运行logrotate with给定的logrotate.conf。 'log'目录也安装在logrotator容器中,因此它可以在mysql日志文件上进行轮换工作。
现在看来,每次旋转后mysql都需要“ mysqladmin flush-logs”以开始写入新文件描述符,但是logrotator容器无法在mysql容器内发出此命令。
为了简短起见:我敢肯定,有更好的方法可以通过日志轮换来完成单独的日志文件。怎么会任何想法都非常感谢。谢谢。
更新:
由于我们目前使用的是mysql 5.7,因此可能无法通过@buaacss提出的解决方案解决我们的问题(这可能绝对可行),我们决定使用“ cron”容器。此外,我们将docker.io安装在cron容器中,并将docker主机的/var/run/docker.sock安装到cron容器中。这使我们可以使用“ docker exec”从cron容器发出要在mysql容器中执行的命令(在本例中为“ mysqladmin flush-logs”)。问题已解决。
您确实可以使用SIGHUP代替基于文档的flush log
语句
https://dev.mysql.com/doc/refman/5.6/en/log-file-maintenance.html
但可能会产生一些不良影响,即,将大量报告信息写入错误日志。
所以,正如我在评论中提到的那样,他们开发了SIGHUP的简化版本,即SIGUSR1,以完成以下功能
FR1: When SIGUSR1 is sent to the server, it must flush the error log.
FR2: When SIGUSR1 is sent to the server, it must flush the general log.
FR3: When SIGUSR1 is sent to the server, it must flush the slow query log.
FR4: SIGUSR1 must not send MySQL status report.
Currently when SIGHUP is sent to the server a large report of information is
printed to stdout, the status report.
FR5: The server must not fail when SIGUSR1 is sent, even though slow log is not
enabled.
FR6: The server must not fail when SIGUSR1 is sent, even though slow log output
is set to a table (log_output).
FR7: The server must not fail when SIGUSR1 is sent, even though general log is
set to OFF.
NFR1: SIGALRM must be undisguisable from how SIGUSR1 behaved before.
不幸的是,这种信号仅在MySQL 8或更高版本中可用