The post Rotating MySQL slow query log appeared first on The WebScale Database Infrastructure Operations Experts.
]]>We often enable MySQL slow_query_log to proactively troubleshoot the performance, There is so lot of information with-in slow_query_log which is enough to address most common (even complex ones occasionally) MySQL performance bottlenecks. What if we haven’t planned for the log lifecycle management of slow query log ? Your log will really grow huge (we keep long_query_time as-low-as 1 second), So we have to plan for slow_query_log rotating to avoid MySQL outage due to “no space left in the disk” to grow further. There are two ways you can do log rotate, I have explained below:
Step 1 – Get the path of slow_query_log
mysql> show variables like '%slow_query%'; +---------------------+-----------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------+ | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/localhost-slow.log | +---------------------+-----------------------------------+ 2 rows in set (0.00 sec)
Step 2 – Disable slow_query_log temporarily
mysql> set global slow_query_log=off; Query OK, 0 rows affected (0.01 sec)
Step 3 – Flush only slow query logs
mysql> flush slow logs; Query OK, 0 rows affected (0.00 sec)
Step 4 – Rename the old slow query log and or compress the same
[root@localhost ~]# mv /var/lib/mysql/localhost-slow.log /var/lib/mysql/localhost-slow-$(date +%Y-%m-%d).log [root@localhost mysql]# ls *.log localhost-slow-2018-04-18.log
[root@localhost mysql]# gzip -c /var/lib/mysql/localhost-slow-2018-04-18.log > /var/lib/mysql/localhost-slow-2018-04-18.log.gz [root@localhost mysql]# ls *.gz localhost-slow-2018-04-18.log.gz [root@localhost mysql]#
Step 5 – Re-enable slow_query_log
mysql> mysql> set global slow_query_log=on; Query OK, 0 rows affected (0.00 sec)
We at MinervaDB love automation, logrotate make slow_query_log rotation simple , To enable logrotate you can use logrotate config file in /etc/logrotate.d/ mysql-slow-query:
/var/lib/mysql/localhost-slow.log { size 2G dateext compress missingok rotate 30 notifempty delaycompress sharedscripts nocopytruncate create 660 mysql mysql postrotate /usr/bin/mysql -e 'select @@global.slow_query_log into @sq_log_save; set global slow_query_log=off; select sleep(5); FLUSH SLOW LOGS; select sleep(10); set global slow_query_log=@sq_log_save;' endscript rotate 250 }
We have explained below logrotate script:
The post Rotating MySQL slow query log appeared first on The WebScale Database Infrastructure Operations Experts.
]]>