前言: mysql
通過前面文章學習,咱們知道 binlog 會記錄數據庫全部執行的 DDL 和 DML 語句(除了數據查詢語句select、show等)。注意默認狀況下會記錄全部庫的操做,那麼若是咱們有另類需求,好比說只讓某個庫記錄 binglog 或排除某個庫記錄 binlog ,是否支持此類需求呢?本篇文章咱們一塊兒來看下。sql
當數據庫實例開啓 binlog 時,咱們執行 show master status 命令,會看到有 Binlog_Do_DB 與 Binlog_Ignore_DB 選項。數據庫
mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000009 | 282838 | | | | +---------------+----------+--------------+------------------+-------------------+
默認狀況下,這兩個選項爲空,那麼這兩個參數有何做用?是否如同其字面意思一個只讓某個庫記錄 binglog 一個排除某個庫記錄 binlog 呢?筆者查閱官方文檔,簡單說明下這兩個參數的做用:服務器
這兩個參數爲互斥關係,通常只選擇其一設置,只能在啓動命令行中或配置文件中加入。指定多個數據庫要分行寫入,舉例以下:運維
# 指定 db1 db2 記錄binlog [mysqld] binlog_do_db = db1 binlog_do_db = db2 # 不讓 db3 db4 記錄binlog [mysqld] binlog_ignore_db = db3 binlog_ignore_db = db4
此外,這兩者參數具體做用與否還與 binlog 格式有關係,在某些狀況下 binlog 格式設置爲 STATEMENT 或 ROW 會有不一樣的效果。在實際應用中 binlog_ignore_db 用途更普遍些,好比說某個庫的數據不過重要,爲了減輕服務器寫入壓力,咱們可能不讓該庫記錄 binlog 。網上也有文章說設置 binlog_ignore_db 會致使從庫同步錯誤,那麼設置該參數到底有什麼效果呢,下面咱們來具體實驗下。學習
首先說明下,個人測試數據庫實例是 5.7.23 社區版本,共有 testdb、logdb 兩個業務庫,咱們設置 logdb 不記錄 binlog ,下面來具體實驗下:測試
# binlog 爲 ROW 格式 # 1.不使用 use db mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 154 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE testdb.`test_tb1` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.06 sec) mysql> insert into testdb.test_tb1 values (1001,'sdfde'); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 653 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE logdb.`log_tb1` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.05 sec) mysql> insert into logdb.log_tb1 values (1001,'sdfde'); Query OK, 1 row affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 883 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ mysql> insert into logdb.log_tb1 values (1002,'sdsdfde'); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 883 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ mysql> alter table logdb.log_tb1 add column c3 varchar(20); Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1070 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ # 結論:其餘庫記錄正常 logdb庫會記錄DDL 不記錄DML # 2.使用 use testdb跨庫 mysql> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select database(); +------------+ | database() | +------------+ | testdb | +------------+ 1 row in set (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1070 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE `test_tb2` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.05 sec) mysql> insert into test_tb2 values (1001,'sdfde'); Query OK, 1 row affected (0.04 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1574 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE logdb.`log_tb2` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.05 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1810 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> insert into logdb.log_tb2 values (1001,'sdfde'); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1810 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) # 結論:一樣logdb庫會記錄DDL 不記錄DML # 3.使用 use logdb跨庫 mysql> use logdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select database(); +------------+ | database() | +------------+ | logdb | +------------+ 1 row in set (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1810 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE testdb.`test_tb3` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.23 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 1810 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> insert into testdb.test_tb3 values (1001,'sdfde'); Query OK, 1 row affected (0.02 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2081 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE `log_tb3` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.05 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2081 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> insert into log_tb3 values (1001,'sdfde'); Query OK, 1 row affected (0.02 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2081 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) # 結論:logdb都不記錄 同時不記錄其餘庫的DDL # 4.每次操做都進入此庫 不跨庫 mysql> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2081 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE `test_tb4` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.05 sec) mysql> insert into test_tb4 values (1001,'sdfde'); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2585 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> use logdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> CREATE TABLE `log_tb4` ( id int , name varchar(30) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.04 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2585 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> insert into log_tb4 values (1001,'sdfde'); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000011 | 2585 | | logdb | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) # 結論:其餘庫所有記錄 logdb全不記錄
一樣的,將 binlog 格式設置爲 STATEMENT ,再次進行測試,這裏再也不贅述測試過程,總結下 STATEMENT 格式下的實驗結果:ui
看了這麼多實驗數據,你是否眼花繚亂了呢,下面咱們以思惟導圖的形式總結以下:this
這麼看來 binlog_ignore_db 參數的效果確實和諸多因素有關,特別是有從庫的狀況下,主庫要特別當心使用此參數,很容易產生主從同步錯誤。不過,按照嚴格標準只對當前數據庫進行操做,則不會產生問題。這也告訴咱們要嚴格按照標準來,只賦予業務帳號某個單庫的權限,也能避免各類問題發生。spa
總結:
不清楚各位讀者是否對這種介紹參數的文章感興趣呢?可能這些是數據庫運維人員比較關注的吧。本篇文章主要介紹關於 binlog 的 binlog_ignore_db 參數的具體做用,可能本篇文章實驗環境還不夠考慮周全,有興趣的同窗能夠參考下官方文檔,有助於對該參數有更深刻的瞭解。