日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二进制日志和数据更新的关系

發布時間:2025/4/5 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二进制日志和数据更新的关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二進制日志的本質是記錄數據的改變

如果數據沒有改變,就一定不記錄二進制日志嗎

記錄數據改變的情況:
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.自定義變量
?在主庫

set @a:=1;mysql> select @a; +------+ | @a | +------+ | 10 | +------+ 在從庫是查不到有關記錄的 在主庫 mysql> select * from test where id=@a; +----+---------------------+ | id | time | +----+---------------------+ | 10 | 2017-02-10 09:21:33 | +----+---------------------+ mysql> update test set time=now() where id=@a; Query OK, 1 row affected (0.01 sec)mysql> select * from test where id=@a; +----+---------------------+ | id | time | +----+---------------------+ | 10 | 2017-02-15 14:36:37 | +----+---------------------+ mysql> show binlog events in 'mysql-bin.000011'; +------------------+-----+-------------+-----------+-------------+--------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------+-----------+-------------+--------------------------------------------------+ | mysql-bin.000011 | 4 | Format_desc | 108 | 120 | Server ver: 5.6.35-log, Binlog ver: 4 | | mysql-bin.000011 | 120 | Query | 108 | 203 | BEGIN | | mysql-bin.000011 | 203 | User var | 108 | 250 | @`a`=10 | | mysql-bin.000011 | 250 | Query | 108 | 366 | use `hk`; update test set time=now() where id=@a | | mysql-bin.000011 | 366 | Xid | 108 | 397 | COMMIT /* xid=29 */ | +------------------+-----+-------------+-----------+-------------+--------------------------------------------------+

從日志中可以看到
復制時把自定義變量也寫到了二進制日志里
在從庫也不會有變量的內容
主庫上的變量 在復制時 和從庫的變量沒任何關系
比如 關閉從庫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 */ | +------------------+------+-------------+-----------+-------------+--------------------------------------------------+

隨機數根據偽隨機種子產生
復制的精確性 是由隨機種子一樣來保證的
發現主從復制的結果是一樣的

mysql> show variables like 'binlog_format'; +---------------+-----------+ | Variable_name | Value | +---------------+-----------+ | binlog_format | STATEMENT | +---------------+-----------+ mysql> set session binlog_format='row'; Query OK, 0 rows affected (0.01 sec)mysql> show variables like 'binlog_format'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ mysql> insert into a values (2,rand()); Query OK, 1 row affected (0.01 sec) 沒產生警告信息 查看二進制日志 | mysql-bin.000011 | 1305 | Query | 108 | 1375 | BEGIN | | mysql-bin.000011 | 1375 | Table_map | 108 | 1419 | table_id: 71 (hk.a) | | mysql-bin.000011 | 1419 | Write_rows | 108 | 1463 | table_id: 71 flags: STMT_END_F | | mysql-bin.000011 | 1463 | Xid | 108 | 1494 | COMMIT /* xid=86 */ | +------------------+------+-------------+-----------+-------------+--------------------------------------------------+ alter table a modify id int auto_increment primary key; mysql> insert into a(rnd) values(rand()); Query OK, 1 row affected (0.00 sec)

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位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的二进制日志和数据更新的关系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。