欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql日志滚动

程序员文章站 2024-02-26 09:13:19
1,查看log情况复制代码 代码如下:mysql> show global variables like '%log%';+---------------------...

1,查看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)

上面主要有三个日志文件,mysql.log,mysqd.log,mysql-slow.log,除了mysqld.log不能通过mysqladmin flush-logs来刷新日志外,其他的都可以,mysqld.log是服务器启动程序mysqld产生的。

2,生成日志刷新用户

复制代码 代码如下:

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)

用root账户登录进去,添加一个log的用户。分开清楚一点。

3,日志滚动脚本,只保留一个星期的日志

复制代码 代码如下:

[root@linux ~]# vim log.sh   //添加以下内容
#!/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
添加可执行权限chmod +x log.sh

脚本很简单,不过有一点要解释一下,就是mv ${log} ${log}_${date},第一周会产生7个文件,第二周会覆盖上周的,星期一覆盖星期一,星期二覆盖星期二,以此内推。

4,日志滚动

复制代码 代码如下:

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