二进制日志和数据更新的关系
二進制日志的本質是記錄數據的改變
如果數據沒有改變,就一定不記錄二進制日志嗎
記錄數據改變的情況:
1.所有可能對數據結構造成改變的ddl語句
? ?alter,create,drop,grant,revoke等
2. insert,delete,update語句可能對數據更新的語句
3.select絕大部分情況下是不會記錄的
1.ddl語言
?
create table test2 (id ,int);mysql> show binlog events in 'mysql-bin.000007'; +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | mysql-bin.000007 | 4 | Format_desc | 250 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 | | mysql-bin.000007 | 106 | Query | 250 | 194 | use `hk`; create table test2 (id int) | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ mysql> drop table test2; Query OK, 0 rows affected (0.03 sec)mysql> show binlog events in 'mysql-bin.000007'; +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | mysql-bin.000007 | 4 | Format_desc | 250 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 | | mysql-bin.000007 | 106 | Query | 250 | 194 | use `hk`; create table test2 (id int) | | mysql-bin.000007 | 194 | Query | 250 | 271 | use `hk`; drop table test2 | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 刪除一個不存在的表(沒改變任何數據),日志會記錄嗎 drop table if exists test2; mysql> show binlog events in 'mysql-bin.000007'; +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ | mysql-bin.000007 | 4 | Format_desc | 250 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 | | mysql-bin.000007 | 106 | Query | 250 | 194 | use `hk`; create table test2 (id int) | | mysql-bin.000007 | 194 | Query | 250 | 271 | use `hk`; drop table test2 | | mysql-bin.000007 | 271 | Query | 250 | 358 | use `hk`; drop table if exists test2 | +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 確實記錄了 對于 create,drop,grant,alter,revoke 也是 mysql> insert into test (time) values (now()); Query OK, 1 row affected (0.00 sec)mysql> select * from test; +----+---------------------+ | id | time | +----+---------------------+ | 1 | 2017-02-05 18:44:17 | | 2 | 2017-02-05 20:10:33 | | 3 | 2017-02-05 21:06:35 | | 4 | 2017-02-08 07:48:57 | | 5 | 2017-02-08 07:49:24 | | 6 | 2017-02-08 07:49:29 | | 7 | 2017-02-08 13:30:57 | +----+---------------------+ mysql> insert into test values(7,now()) on duplicate key update id=7; Query OK, 0 rows affected (0.01 sec) show binlog events in 'mysql-bin.000007'; 在二進制日志里也確實可以看到記載了 mysql> delete from test where id=8; Query OK, 0 rows affected (0.03 sec)mysql> show binlog events in 'mysql-bin.000007'; +------------------+------+-------------+-----------+-------------+--------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+------+-------------+-----------+-------------+--------------------------------------------------------------------------+ | mysql-bin.000007 | 4 | Format_desc | 250 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 | | mysql-bin.000007 | 106 | Query | 250 | 194 | use `hk`; create table test2 (id int) | | mysql-bin.000007 | 194 | Query | 250 | 271 | use `hk`; drop table test2 | | mysql-bin.000007 | 271 | Query | 250 | 358 | use `hk`; drop table if exists test2 | | mysql-bin.000007 | 358 | Query | 250 | 449 | use `hk`; insert into test(id) values(7) | | mysql-bin.000007 | 449 | Query | 250 | 537 | use `hk`; delete from test where id=7 | | mysql-bin.000007 | 537 | Query | 250 | 638 | use `hk`; insert into test values(7,now()) | | mysql-bin.000007 | 638 | Query | 250 | 768 | use `hk`; insert into test values(7,now()) on duplicate key update id=7 | | mysql-bin.000007 | 768 | Query | 250 | 856 | use `hk`; delete from test where id=7 | | mysql-bin.000007 | 856 | Query | 250 | 979 | use `hk`; alter table test change id id int primary key auto_increment | | mysql-bin.000007 | 979 | Intvar | 250 | 1007 | INSERT_ID=7 | | mysql-bin.000007 | 1007 | Query | 250 | 1114 | use `hk`; insert into test (time) values (now()) | | mysql-bin.000007 | 1114 | Query | 250 | 1244 | use `hk`; insert into test values(7,now()) on duplicate key update id=7 | | mysql-bin.000007 | 1244 | Query | 250 | 1332 | use `hk`; delete from test where id=8 | +------------------+------+-------------+-----------+-------------+--------------------------------------------------------------------------+以上2條語句均未實際造成數據的改變 復制有延時,處理時間類型的數據 在statement下,sysdate()函數返回的時間并不安全,不能保證復制的精確性 View Code?
?
?
2.隱式信息的記錄
1.自定義變量
?在主庫
從日志中可以看到
復制時把自定義變量也寫到了二進制日志里
在從庫也不會有變量的內容
主庫上的變量 在復制時 和從庫的變量沒任何關系
比如 關閉從庫io進程
在主庫修改記錄 id為@a的
在從庫定義一個@a為另外一個值
開啟從庫io進程
復制不會受到主從庫的變量的影響
主庫的變量在修改記錄后再發生修改變量的值 也不會影響復制
2.隨機函數
mysql> select rand(); +--------------------+ | rand() | +--------------------+ | 0.7021004480329346 | +--------------------+ 每次都在變化 create table a (id int,rnd float(6,5)); mysql> insert into a values (1,rand()); Query OK, 1 row affected, 1 warning (0.00 sec)mysql> show warnings; +-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Note | 1592 | Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statement is unsafe because it uses a system function that may return a different value on the slave. | +-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 在二進制日志中可以看到 | mysql-bin.000011 | 951 | Query | 108 | 1059 | use `hk`; create table a (id int,rnd float(6,5)) | | mysql-bin.000011 | 1059 | Query | 108 | 1134 | BEGIN | | mysql-bin.000011 | 1134 | RAND | 108 | 1173 | rand_seed1=753874615,rand_seed2=506421884 | | mysql-bin.000011 | 1173 | Query | 108 | 1274 | use `hk`; insert into a values (1,rand()) | | mysql-bin.000011 | 1274 | Xid | 108 | 1305 | COMMIT /* xid=77 */ | +------------------+------+-------------+-----------+-------------+--------------------------------------------------+隨機數根據偽隨機種子產生
復制的精確性 是由隨機種子一樣來保證的
發現主從復制的結果是一樣的
3.自增長主鍵
set session binlog_format='statement'; 查看自增主鍵復制的情況 mysql> insert into a(rnd) values(rand()); Query OK, 1 row affected, 1 warning (0.02 sec) | mysql-bin.000012 | 309 | Query | 108 | 384 | BEGIN | | mysql-bin.000012 | 384 | Intvar | 108 | 416 | INSERT_ID=4 | | mysql-bin.000012 | 416 | RAND | 108 | 455 | rand_seed1=196763153,rand_seed2=17450176 | | mysql-bin.000012 | 455 | Query | 108 | 558 | use `hk`; insert into a(rnd) values(rand()) | | mysql-bin.000012 | 558 | Xid | 108 | 589 | COMMIT /* xid=97 */ | +------------------+-----+-------------+-----------+-------------+---------------------------------------------+INSERT_ID=4
就是主鍵Id
總結
statement模式下
1.自定義變量的處理,寫入BinLog
2.rand()隨機函數處理,偽隨機種子寫入Binlog
3.last_insert_id處理 ,id值寫入binlog
4.auto_increment處理,該字段的值會寫入binlog
row模式是保存更改的數據塊,所以能保證復制的精確性
二.外部數據導入處理
load data file處理
statement 模式
mysql把系統文件保存在類似數據塊中,標識為文件id,通過標識文件的id數據塊導入,保證主從復制的精確性
row模式下
把對行的改變的最終結果保存為二進制數據,保證主從復制的精確性
load file處理
statement模式下 為不安全的,無法保證復制精確性
row模式
保存的是改變的最終結果,是二進制數據塊,保證主從復制的精確性
?
3.與非同步庫關聯更新的關系
update users a ,hxf2.users b set a.email=b.email where a.uid=b.uid;
?
hxf2為非同步庫
statement不能保證
row可以保證精確復制
?
存儲過程與日志更新關系
查看存儲過程
show procedure status
查看函數
show function status
?
觸發器
事件(定時任務)
show events;
set global event_scheduler=1;
?
轉載于:https://www.cnblogs.com/HKUI/p/6409019.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的二进制日志和数据更新的关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《算法设计手册》面试题解答 第三章:数据
- 下一篇: 各技术镜像整理