binlog日志太大,flush的时候导致binlog写入时间变慢,因为要写入新的binlog,需要时间。解决方案就是调整binlog最大值,将1G降低到128M。


mysql> show variables like 'max_binlog_size';
+-----------------+------------+
| Variable_name | Value |
+-----------------+------------+
| max_binlog_size | 1073741824 |
+-----------------+------------+
1 row in set (0.01 sec)

mysql> select 128*1024*1024;
+---------------+
| 128*1024*1024 |
+---------------+
| 134217728 |
+---------------+
1 row in set (0.01 sec)

mysql> set global max_binlog_size=134217728;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_binlog_size';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| max_binlog_size | 134217728 |
+-----------------+-----------+
1 row in set (0.00 sec)

反思,互联网场景中,max_binlog_size值确实不宜过大,这点需要谨记。


问题扩展:

查看当前正在使用的binlog缓存情况:

MySQL:(none) 13:07:41> show global status like 'bin%';
+----------------------------+-----------+
| Variable_name | Value |
+----------------------------+-----------+
| Binlog_cache_disk_use | 1335001 |
| Binlog_cache_use | 264238120 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 33 |
+----------------------------+-----------+
4 rows in set (0.00 sec)
MySQL:(none) 13:07:46> show global status like 'bin%';
+----------------------------+-----------+
| Variable_name | Value |
+----------------------------+-----------+
| Binlog_cache_disk_use | 1335006 |
| Binlog_cache_use | 264240359 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 33 |
+----------------------------+-----------+
4 rows in set (0.00 sec)

MySQL:(none) 13:07:47>
MySQL:(none) 13:07:48> show global status like 'bin%';
+----------------------------+-----------+
| Variable_name | Value |
+----------------------------+-----------+
| Binlog_cache_disk_use | 1335428 |
| Binlog_cache_use | 264437761 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 33 |
+----------------------------+-----------+
4 rows in set (0.00 sec)


查看binlog的cache设置:

MySQL:(none) 13:10:58> show variables like '%binlog_cache%';
+-----------------------+----------------------+
| Variable_name | Value |
+-----------------------+----------------------+
| binlog_cache_size | 32768 |
| max_binlog_cache_size | 18446744073709547520 |
+-----------------------+----------------------+
2 rows in set (0.00 sec)


binlog_cache_size:

为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存。binlog_cache_disk_use表示因为我们binlog_cache_size设计的内存不足导致缓存二进制日志用到了临时文件的次数;binlog_cache_use表示 用binlog_cache_size缓存的次数;当对应的Binlog_cache_disk_use 值比较大的时候而且Binlog_cache_use也比较大的时候,我们可以考虑适当的调高 binlog_cache_size 对应的值


进一步分析:
我们这里可以看到Binlog_cache_disk_use已经是1.27M,而且Binlog_cache_use值为264437761表示已经执行了2亿多次了,而且这2个值一直在变大,就表名,binlog_cache_size远远不够用,所以这里就可以提高binlog_cache_size的值了。

MySQL:(none) 13:40:08> set global binlog_cache_size=8388608;
Query OK, 0 rows affected (0.00 sec)

max_binlog_cache_size:

max_binlog_cache_size 表示的是binlog 能够使用的最大cache 内存大小,当我们执行多语句事务的时候 所有session的使用的内存超过max_binlog_cache_size的值时,就会报错:“Multi-statement transaction required more than ‘max_binlog_cache_size’ bytes ofstorage”,设置太大的话,会比较消耗内存资源;设置太小又会使用到临时文件即disk

查看mysql 的binlog日志存放的位置

这个你可以看配置文件 启用了才有这样的记录默认是没有的
linux系统中的/etc/my.cnf

my.cnf内容:
log-bin = mysqlbin # 默认配置

一般放在/var/lib/mysql
比如上面的设置重启数据库会生成mysqlbin.000001文件

自定义文件存放位置

修改配置文件,vi /etc/my.cnf,找到log-bin的部分
配置定期自动清理在my.cnf文件中,这个文件路径不知道的话执行mysql --help | grep 'Default options' -A 1,就会列出文件的路径来

然后重启service mysql restart,去新建的目录下看看,已经有最新的日志了

0、查看日志开启状态 show variables like 'log_%';

1、查看所有binlog日志列表 show master logs;

2、查看最新一个binlog日志的编号名称,及其最后一个操作事件结束点 show master status;

3、刷新log日志,立刻产生一个新编号的binlog日志文件,跟重启一个效果 flush logs;

4、清空所有binlog日志 reset master;

5、日志查看,因为是二进制文件没法用vi等打开,可以用mysql的mysqlbinlog打开,/usr/bin/mysqlbinlog /home/logs/mysql-bin.000001,当然每个人路径可能不一样,这个只能自己去找了。

优质内容筛选与推荐>>
1、Linux实用命令(sed,tcpdump,mkpasswd等)
2、转载文章,转自“运维笔记” 博客(Python利用Zabbix API定时报告存在报警的机器)http://lihuipeng.blog.51cto.com/3064864/1306261
3、KVM 介绍(8):使用 libvirt 迁移 QEMU/KVM 虚机和 Nova 虚机 [Nova Libvirt QEMU/KVM Live Migration]
4、powershell 删除一个文件或文件夹
5、Request.ServerVariables 全集以及在ASP和C#中的遍历


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn