暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

MySQL的存储引擎之Blackhole

原创 巩飞 2020-03-04
2906

Blackhole,黑洞,初见这个词,一下就想到了相对论中的那个能吞噬一切,连光都无法逃脱的黑洞。MySQL的Blackhole引擎会怎么样呢?看看官方文档:

The BLACKHOLE storage engine acts as a “black hole” that accepts data but throws it away and does not store it.

Blackhole引擎充当一个“黑洞”,接受数据,但将其扔掉,不存储数据。

root@database-one 12:47: [gftest]> create table testblackhole(i int,c char(10)) engine=blackhole; Query OK, 0 rows affected (0.03 sec) root@database-one 12:48: [gftest]> insert into testblackhole VALUES(1,'record one'),(2,'record two'); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 root@database-one 12:49: [gftest]> select * from testblackhole; Empty set (0.00 sec)

天呐,数据真没了,没了,没了…

再来测试几个操作:

root@database-one 12:51: [gftest]> delete from testblackhole where i=1; Query OK, 0 rows affected, 1 warning (0.00 sec) root@database-one 12:52: [gftest]> show warning; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warning' at line 1 root@database-one 12:52: [gftest]> delete from testblackhole; Query OK, 0 rows affected, 1 warning (0.01 sec) root@database-one 12:53: [gftest]> show warning; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warning' at line 1 root@database-one 12:53: [gftest]> update testblackhole set c='abc' where i=2; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 1 root@database-one 12:54: [gftest]> show warning; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warning' at line 1

可以看到,update和delete操作可以执行,但是会产生个警告。

创建Blackhole表时,MySQL在数据库目录中创建表格式文件,文件名以表名开头,扩展名为.frm。除此之外,没有其它文件。

[root@database-one ~]# cd /home/mysql/gftest/ [root@database-one gftest]# ls -l testblackhole* -rw-r----- 1 mysql mysql 8578 3月 3 12:48 testblackhole.frm

Blackhole引擎支持各种索引,所以建表时可以包含索引声明。Blackhole表支持触发器,因为实际上并不存储数据,因此不会触发UPDATE和DELETE触发器,INSERT触发器会触发。

Blackhole引擎这么特别,如果真的没有用处,也就不可能存在,那它到底有什么价值呢?官方文档列举了几个:

  • Replication场景实现中继或过滤
  • 校验dump文件语法。
  • 测量开启 binlog 日志所带来的额外开销。
  • 查找和存储引擎无关的其他方面的性能瓶颈。

我们来看看Replication场景实现中继或过滤这种。

Insert到Blackhole表中不会存储数据,但如果启用了基于语句的二进制日志记录,则会记录SQL语句到binlog日志。这可以用作中继器或过滤机制。

假设业务需要slave端过滤,但是将所有binlog日志传输到slave端会导致太多流量。在这种情况下,可以在 master主机上设置一个默认存储引擎为Blackhole的“dummy”从进程,如下所示:
图片.png
master写日志到自己的binlog,“dummy”mysqld进程充当slave(中继器或过滤器),应用所需的replicate do-*和replicate ignore-*规则的组合,并生成新的binlog日志,再把此日志提供给真正的slave服务器。

“dummy”mysqld进程实际上并不存储任何数据,因此在master主机上增加的额外开销很小。这种配置也可以在slave机配置。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论