mysql logs scroll

  • 2020-06-07 05:27:51
  • OfStack

1. Check log


mysql> show global variables like '%log%';
+---------------------------------+---------------------------------+
| Variable_name                   | Value                           |
+---------------------------------+---------------------------------+
| back_log                        | 50                              |
| binlog_cache_size               | 32768                           |
| binlog_format                   | MIXED                           |
| expire_logs_days                | 0                               |
| general_log                     | ON                              |
| general_log_file                | /usr/local/mysql/mysql.log      |
| log                             | ON                              |
| log_bin                         | ON                              |
| log_bin_trust_function_creators | OFF                             |
| log_bin_trust_routine_creators  | OFF                             |
| log_error                       | /var/log/mysqld.log             |
| log_output                      | FILE                            |
| log_queries_not_using_indexes   | OFF                             |
| log_slave_updates               | OFF                             |
| log_slow_queries                | ON                              |
| log_warnings                    | 1                               |
| max_binlog_cache_size           | 4294963200                      |
| max_binlog_size                 | 1073741824                      |
| max_relay_log_size              | 0                               |
| relay_log                       |                                 |
| relay_log_index                 |                                 |
| relay_log_info_file             | relay-log.info                  |
| relay_log_purge                 | ON                              |
| relay_log_space_limit           | 0                               |
| slow_query_log                  | ON                              |
| slow_query_log_file             | /usr/local/mysql/mysql-slow.log |
| sql_log_bin                     | ON                              |
| sql_log_off                     | OFF                             |
| sql_log_update                  | ON                              |
| sync_binlog                     | 0                               |
+---------------------------------+---------------------------------+
30 rows in set (0.00 sec)

Above, there are mainly three log files mysql. log, mysqd. log, mysql - slow. log, besides mysqld. log cannot pass mysqladmin flush - logs to refresh the logs, other can mysqld. log is mysqld server startup program.

2. Generate log refresh users


mysql> GRANT RELOAD ON *.* TO 'log'@'localhost' IDENTIFIED BY 'log';
Query OK, 0 rows affected (0.07 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

Log in with your root account and add 1 log user. One point clear.

3, log scrolling script, only keep the log for 1 week


[root@linux ~]# vim log.sh   // Add the following 
#!/bin/sh
# log refresh 
if [ -f $1 ];
then
    echo "refresh ok" 1>&2
else
    echo "log file do not exist;"
    exit 1
fi
LOG=$1
DB_USER="log"
DB_PASS="log"                                                                                                       
# Others vars
DATE=`date +%w`                                        
BIN_DIR="/usr/local/mysql/bin"
mv ${LOG} ${LOG}_${DATE}
${BIN_DIR}/mysqladmin  -ulog -plog flush-logs
 Add executable permissions chmod +x log.sh

The script is simple, but there is one point to explain. mv ${LOG} ${LOG}_${DATE}, week 1 will produce 7 files, week 2 will cover last week's, Week 1 will cover Week 1, week 2 will cover Week 2, and so on.

4. Log scrolling


[root@linux ~]# ./log.sh /usr/local/mysql/mysql.log
refresh ok
[root@linux ~]# ls /usr/local/mysql/ |grep mysql
mysql.log
mysql.log_1


Related articles: