centos8平台mysql日志的按天切分
一,mysqladmin使用flush-logs的文档:
mysql8官网上面针对mysqladmin的文档地址
https://dev.mysql.com/doc/refman/8.0/en/mysqladmin.html
内容:
flush-logs [log_type ...] Flush all logs. The mysqladmin flush-logs command permits optional log types to be given, to specify which logs to flush. Following the flush-logs command,
you can provide a space-separated list of one or more of the following log types: binary, engine, error, general, relay, slow.
These correspond to the log types that can be specified for the FLUSH LOGS SQL statement.
说明: flush-logs 加 error binary slow 等参数,这个功能点是在mysql5.7版本后才增加的,
更低版本的mysql应该不支持这个参数,一定要注意
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/20/centos8linuxmysql-ri-zhi-de-an-tian-qie-fen/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,创建日志分割后目录
[root@yjweb logs]$ mkdir mysqllogsbackup
三,编写脚本
[root@yjweb crontab]# vi split_mysql_logs.sh
内容:
#!/bin/bash # 备份mysql的日志 # 昨天的日期 file_date=$(date -d"1 day ago" +"%Y%m%d") echo ${file_date} #mysql log的路径 log_path_mysql=/data/mysql/log #日志分割后的路径 back_base=/data/logs/mysqllogsbackup user="root" passwd="demopass" # ===================backup dir name======================== date_year=$(date -d"1 day ago" +"%Y") date_month=$(date -d"1 day ago" +"%m") date_day=$(date -d"1 day ago" +"%d") back_path=${back_base}/${date_year}/${date_month}/${date_day} echo ${back_path} # ===================mkdir back_path======================== if [ -d ${back_path} ];then echo 目录已经存在,不能重复创建 else mkdir -p ${back_path} fi # =================== 备份mysql log ======================== cd ${log_path_mysql} for file in $(ls *log); do mv ${file} ${back_path}/${file_date}_${file} done /usr/local/soft/mysql/bin/mysqladmin -u$user -p$passwd --socket=/data/mysql/var/mysql.sock flush-logs slow error
说明:我们针对两个日志做切分,一个是慢查询日志,一个是错误日志
不建议针对二进制日志即binlog做切分
四,编写完成后添加可执行属性,并加入到crond
[root@yjweb crontab]# chmod +x split_mysql_logs.sh
[root@yjweb log]# crontab -e
增加一行
15 0 * * * sh /data/web/crontab/split_mysql_logs.sh >> /data/logs/cronlogs/splitmysqllogs.log 2>&1
查看效果:
[root@yjweb log]# crontab -l | grep mysql 15 0 * * * sh /data/web/crontab/split_mysql_logs.sh >> /data/logs/cronlogs/splitmysqllogs.log 2>&1
五,查看日志分割的效果:
[root@yjweb log]# ll /data/logs/mysqllogsbackup/2020/03/10/ total 48 -rw-r--r-- 1 mysql mysql 42444 Mar 11 16:52 20200310_mysqld.log -rw-r----- 1 mysql mysql 1184 Mar 11 17:22 20200310_mysql-slow.log
六,查看当前centos服务器版本
[root@yjweb ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)