Mysql log rolling bitsCN.com
1. View log information
Mysql> show global variables like '% log % ';
+ --------------------------------- +
| Variable_name | Value |
+ --------------------------------- +
| Back_log | 50 |
| Bin log_cache_size | 32768 |
| Binlog_format | MIXED |
| Expire_logs_days | 0 |
| General_log | ON |
| General_log_file |/usr/local/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)
There are three main log files above, mysql. log, mysqd. log, mysql-slow.log, except mysqld. logs cannot be refreshed by mysqladmin flush-logs. other logs can be refreshed by mysqld. log is generated by the server startup program mysqld.
2. generate a log to refresh the user
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 the root account and add a log user. Separate them clearly.
3. the log rolling script keeps logs for only one week.
[Root @ linux ~] # Vim log. sh // add the following content
#! /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 permission chmod + x log. sh
The script is simple, but it should be explained that mv $ {LOG }$ {LOG }_$ {DATE} will generate seven files in the first week, and the second week will overwrite the files in the last week, monday covers Monday and Tuesday.
4. log scrolling
[Root @ linux ~] #./Log. sh/usr/local/mysql. log
Refresh OK
[Root @ linux ~] # Ls/usr/local/mysql/| grep mysql
Mysql. log
Mysql. log_1
BitsCN.com