二進制日誌和數據和數據庫操做(ddl,實質未形成改變的語句,自定義變量,隨機函數,自增主鍵,外部導入數據,與非同步庫關聯更新,存儲過程,函數,觸發器,事件等)關係mysql
二進制日誌的本質是記錄數據的改變sql
若是數據沒有改變,就必定不記錄二進制日誌嗎數據庫
記錄數據改變的狀況:
1.全部可能對數據結構形成改變的ddl語句
alter,create,drop,grant,revoke等
2. insert,delete,update語句可能對數據更新的語句
3.select絕大部分狀況下是不會記錄的安全
1.ddl語言session
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.自定義變量
在主庫ide
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.隨機函數ui
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;