AMP AMP

How to create MySQL logrotate on centos 7.6

Method to create MySQL logrotate on Centos 7.6

Installation process

Check the centos version by using the following command

[root@linuxhelp ~]# rpm -q centos-release
centos-release-7-6.1810.2.el7.centos.x86_64

Enter into the logrotate directory

[root@linuxhelp ~]# cd /etc/logrotate.d/

List out the file

[root@linuxhelp logrotate.d]# ls -la
total 68
drwxr-xr-x.   2 root root  214 Jan 12 01:13 .
drwxr-xr-x. 138 root root 8192 Jan 12 05:17 ..
-rw-r--r--.   1 root root   91 Apr 11  2018 bootlog
-rw-r--r--.   1 root root  160 Sep 15  2017 chrony
-rw-r--r--.   1 root root   71 Apr 11  2018 cups
-rw-r--r--.   1 root root  172 Sep 30  2016 iscsiuiolog
-rw-r--r--.   1 root root  165 Oct 31  2018 libvirtd
-rw-r--r--.   1 root root  142 Oct 31  2018 libvirtd.qemu
-rw-r--r--    1 root root  972 Sep 20 15:03 mysql
-rw-r--r--.   1 root root  106 Apr 11  2018 numad
-rw-r--r--.   1 root root  136 Jun 10  2014 ppp
-rw-r--r--.   1 root root  408 Aug  3  2017 psacct
-rw-r--r--.   1 root root  115 Oct 31  2018 samba
-rw-r--r--.   1 root root  224 Oct 30  2018 syslog
-rw-r--r--.   1 root root  100 Oct 31  2018 wpa_supplicant
-rw-r--r--.   1 root root  103 Nov  5  2018 yum

Create new file for mysql logrotate in the directory

[root@linuxhelp logrotate.d]# vim mysql_new
/var/log/mysqld.log {
create 600 mysql mysql
notifempty
daily
rotate 5
missingok
compress
size 100M
olddir /mnt/test/
}

Check the mysql log by using the following command

[root@linuxhelp logrotate.d]# vim /var/log/mysqld.log
020-01-11T23:47:30.922794Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 9411
2020-01-11T23:47:40.990156Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +wMsqW%AE3hk
2020-01-11T23:47:45.486431Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 10197
2020-01-11T23:47:49.068967Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-01-11T23:47:49.206560Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
2020-01-11T23:47:49.428623Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

Enter into the following directory

[root@linuxhelp logrotate.d]# cd /mnt

Create the test directory

[root@linuxhelp mnt]# mkdir test

Run the following command to execute the logrotate for MySQL by force

[root@linuxhelp mnt]# logrotate -f /etc/logrotate.d/mysql_new

Enter into test directory

[root@linuxhelp mnt]# cd test

List out the file

[root@linuxhelp test]# ls -la
total 4
drwxr-xr-x  2 root  root   29 Jan 12 05:32 .
drwxr-xr-x. 4 root  root   30 Jan 12 05:32 ..
-rw-r-----  1 mysql mysql 441 Jan 12 05:31 mysqld.log.1.gz

Extract the file using gunzip command

[root@linuxhelp test]# gunzip mysqld.log.1.gz

List out the file

[root@linuxhelp test]# ls -la
total 4
drwxr-xr-x  2 root  root   26 Jan 12 05:32 .
drwxr-xr-x. 4 root  root   30 Jan 12 05:32 ..
-rw-r-----  1 mysql mysql 847 Jan 12 05:31 mysqld.log.1

Now you can see the mysql log in the following directory

[root@linuxhelp test]# vim mysqld.log.1
020-01-11T23:47:30.922794Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 9411
2020-01-11T23:47:40.990156Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +wMsqW%AE3hk
2020-01-11T23:47:45.486431Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 10197
2020-01-11T23:47:49.068967Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-01-11T23:47:49.206560Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
2020-01-11T23:47:49.428623Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

The previous log has been removed

[root@linuxhelp test]# vim /var/log/mysqld.log

This is the method to create MySQL logrotate on centos 7.6 comes to end

FAQ
Q
What is the use of notifoption in configuration?
A
The use of notifempty option in the configuration is skipped logrotate if the file is empty
Q
How to store the rotated log in a specific location?
A
To store the rotated log in specific location use the below option in the configuration
olddir
Q
How to mail the rotated log to the Administrator?
A
To mail the rotated log to the Administrator use the below option in the configuration
mail abc@gmail.com
Q
How to specify a date in the rotated log file?
A
To specify a date in rotated log file use the below option in the configuration file
date text
Q
What is logrotate?
A
logrotate is designed to ease administration of systems. It allows automatic rotation, compression, removal, and mailing of log files.
Log Rotation depends on weekly, monthly and yearly, or size