温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysqlbinlog 日常维护

发布时间:2020-04-10 18:27:20 来源:网络 阅读:577 作者:春秋小记 栏目:MySQL数据库

常用的binlog日志操作命令
1)查看所有binlog日志列表
mysql> show master logs;
mysql> show master logs;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.000020 | 1233973852 |
| mysql-bin.000021 | 1098889933 |
| mysql-bin.000022 | 1073742725 |
| mysql-bin.000023 | 1076857649 |
2 rows in set (0.00 sec)

2)查看master状态,即最后(最新)一个binlog日志的编号名称,及其最后一个操作事件pos结束点(Position)值
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
|  mysql-bin.000023 | 4102 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

3)flush刷新log日志,自此刻开始产生一个新编号的binlog日志文件
mysql> flush logs;
Query OK, 0 rows affected (0.13 sec)

mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 149 |
| mysql-bin.000002 | 4145 |
| mysql-bin.000003 | 106 |
+------------------+-----------+
3 rows in set (0.00 sec)

注意:
每当mysqld服务重启时,会自动执行此命令,刷新binlog日志;在mysqldump备份数据时加 -F 选项也会刷新binlog日志;

4)重置(清空)所有binlog日志
mysql> reset master;
Query OK, 0 rows affected (0.12 sec)

mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 106 |
+------------------+-----------+
1 row in set (0.00 sec)


5.1)清除binlog:

删除binlog.000022之前的文件
//删除指定日志文件的日志索引中binlog日志文件
mysql> purge master logs to 'binlog.000022';
Query OK, 0 rows affected (0.04 sec)

mysql> show master logs;
+---------------+------------+
| Log_name      | File_size  |
+---------------+------------+
| binlog.000022 |  185770450 |
| binlog.000023 |  209780654 |
| binlog.000024 |  210483780 |
| binlog.000025 |  210304097 |
| binlog.000026 |  210346860 |
| binlog.000027 |  209954750 |
| binlog.000028 |  210094938 |

5.2)指定时间进行删除:
purge master logs before'2018-10-02 21:40:00'; //删除指定日期以前的日志索引中binlog日志文件

5.3)删除binlog注意事项:

注意时间和文件名一定不可以写错,特别是时间中的年和文件名中的序号,防止将正在使用的binlog删除,切勿删除正在使用的binlog。
使用该语法,会将对应的文件和mysql-bin.index中的对应路径删除。


5.4)通过设置binlog过期的时间,使系统自动删除binlog文件:

mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name  | Value |
+------------------+-------+
| expire_logs_days |   0  |
+------------------+-------+
mysql> set global expire_logs_days = 14;    #设置binlog多少天过期

注意:
过期时间设置的要适当,对于主从复制,要看从库的延迟决定过期时间,特别是一些繁忙的业务系统,避免主库binlog还未传到从库便因过期而删除,

导致主从不一致,另外保留足够多的binlog日志做恢复使用。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI