• Home
  • DBA Scripts
    • Oracle Scripts
    • SQL Server Scripts
  • Knowledge Base
    • Oracle Database
    • MS SQL Server
    • MongoDB
    • MariaDB
  • Troubleshoot
    • Oracle Database Issues
    • SQL Server Issues
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • Courses
    • Oracle Database
    • Oracle DBA L1
    • Oracle DBA L2
    • Oracle DBA L3
  • Home
  • DBA Scripts
    • Oracle Scripts
    • SQL Server Scripts
  • Knowledge Base
    • Oracle Database
    • MS SQL Server
    • MongoDB
    • MariaDB
  • Troubleshoot
    • Oracle Database Issues
    • SQL Server Issues
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • Courses
    • Oracle Database
    • Oracle DBA L1
    • Oracle DBA L2
    • Oracle DBA L3
home/Knowledge Base/MySQL/Purge Old Logs in MySQL
Popular Search:Oracle, SQL Server, MongoDB

Purge Old Logs in MySQL

35 views 0 March 10, 2024 sandeep

How to purge the old logs in mysql? Rotate the logs in MySQL.

Logrotate: /etc/logrotate.conf
– Linux utility
– Rotate the logs based on the rules
– Used to maintain the logs

* /etc/logrotate.d/ — File to input the parameters
* /var/lib/logrotate/status — check status of log rotate
* /etc/logrotate.conf

1. Login into MySQL to check log expire time. We can change this time accordingly.

mysql> show global variables like 'binlog_expire_logs_seconds';
+----------------------------+---------+
| Variable_name | Value |
+----------------------------+---------+
| binlog_expire_logs_seconds | 2592000 |
+----------------------------+---------+
1 row in set (0.00 sec)

mysql>

2. Manually purge the bin logs by the

mysql> purge binary logs to 'binlog.000006';
Query OK, 0 rows affected (0.02 sec)

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000006 | 180 | No |
| binlog.000007 | 180 | No |
| binlog.000008 | 180 | No |
| binlog.000009 | 180 | No |
| binlog.000010 | 201 | No |
| binlog.000011 | 201 | No |
| binlog.000012 | 1114 | No |
+---------------+-----------+-----------+
7 rows in set (0.00 sec)

3. Schedule the cron job to purge the old logs.

cd /u01/mysql/logs
mkdir archive_error_log

root@Server:/etc/logrotate.d# vi mysql-rotate-logs
root@Server:/etc/logrotate.d# cat mysql-rotate-logs
/u01/mysql/logs/error.log {
create 600 mysql mysql
notifempty
daily
rotate 365
dateext
missingok
compress
copytruncate
olddir /u01/mysql/logs/archive_error_log
}

root@Server:/etc/logrotate.d# logrotate -f mysql-rotate-logs
root@Server:/etc/logrotate.d# cd /u01/mysql/logs/archive_error_log
root@Server:/u01/mysql/logs/archive_error_log# ls -lrth
total 4.0K
-rw-r----- 1 mysql mysql 1.4K Mar 8 22:12 error.log-20240308.gz
root@Server:/u01/mysql/logs/archive_error_log#

**Schedule this command “logrotate -f mysql-rotate-logs” in crontab as per requirement.

Tags:MySQL

Was this helpful?

Yes  No
Related Articles
  • Configure Master Slave Replication in MySQL
  • Type of Logs in MySQL
  • Install and Remove Plugin into MySQL
  • MySQL user Management
  • Start and Stop MySQL using the systemctl
  • Install MySQL in Linux / Ubuntu
Leave A Comment Cancel reply

MySQL
  • Purge Old Logs in MySQL
  • Configure Master Slave Replication in MySQL
  • Type of Logs in MySQL
  • Install and Remove Plugin into MySQL
  • MySQL user Management
  • Start and Stop MySQL using the systemctl
View All 6  
Popular Articles
  • Upgrade Oracle Database from 11.2.0.3 to 12.1.0.2
  • Upgrading MariaDB on Windows
  • Oracle DBA Basic Interview Questions Part A
  • Purging Oracle Sysaux Tablespace
  • Client – Server Architecture
KB Categories
  • Oracle Database
    • Oracle RAC
    • Oracle ASM
    • Oracle GoldenGate
    • Oracle Tuning
    • Oracle 11g Database
    • Oracle Database Upgrade
    • Oracle 12c Database
    • ALL KB Oracle
    • Oracle 18c Database
    • Oracle Standby Database
  • MongoDB
  • MS SQL Server
  • MySQL
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • MariaDB
Database Organization

Database Organization (DB ORG) is knowledge base for DBA to learn and execute the fundamental of different databases under one website. DB ORG is a non-profit initiative. ORACLE, MS SQL Server, MongoDB, MariaDB, Couchbase

Join Our Community
  • KnowledgeBase
  • Documentation
  • Troubleshoot
  • FAQ
Information Links
  • About DBOrg
  • Licenses
  • Terms
  • Privacy Policy
Contact Us
    DB ORG - Database Administration,
    Knowledge Base for DBA
    Mail: support@databaseorg.com
    WhatsApp: (+91) 9306440957
    Monday to Friday: EST - 11:30 AM to 06:30 PM (IST - 9:00 PM to 4:00 AM)
  • © 2023 Database Organization - DB ORG. All Rights Reserved.

Popular Search:Oracle, SQL Server, MongoDB

WhatsApp DB Org