這篇文章主要講解了“MySQL innodb事務(wù)的實(shí)現(xiàn)方式”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“MySQL innodb事務(wù)的實(shí)現(xiàn)方式”吧!
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)額爾古納免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
【1】Redo Log
在Innodb存儲引擎中,事務(wù)日志是通過redo和innodb的存儲引擎日志緩沖(Innodb log buffer)來實(shí)現(xiàn)的,當(dāng)開始一個(gè)事務(wù)的時(shí)候,會記錄該事務(wù)的lsn(log sequence number)號; 當(dāng)事務(wù)執(zhí)行時(shí),會往InnoDB存儲引擎的日志
的日志緩存里面插入事務(wù)日志;當(dāng)事務(wù)提交時(shí),必須將存儲引擎的日志緩沖寫入磁盤(通過innodb_flush_log_at_trx_commit來控制),也就是寫數(shù)據(jù)前,需要先寫日志。這種方式稱為“預(yù)寫日志方式”,
innodb通過此方式來保證事務(wù)的完整性。也就意味著磁盤上存儲的數(shù)據(jù)頁和內(nèi)存緩沖池上面的頁是不同步的,是先寫入redo log,然后寫入data file,因此是一種異步的方式。通過 show engine innodb status\G 來觀察之間的差距
-- 建立一張表z,然后建立一個(gè)往表導(dǎo)入數(shù)據(jù)的存儲過程load_test。通過命令show engine innodb status觀察當(dāng)前的redo日志情況
create table z(a int, primary key(a))engine=innodb; DELIMITER $$ DROP PROCEDURE IF EXISTS `test`.`load_test`$$ CREATE PROCEDURE load_test(COUNT INT) BEGIN DECLARE i INT UNSIGNED DEFAULT 0; START TRANSACTION; WHILE i < COUNT DO REPLACE INTO z SELECT i; -- 用replace是因?yàn)樾枰貜?fù)調(diào)用,避免主鍵重復(fù)insert報(bào)錯(cuò)。 SET i=i+1; END WHILE; COMMIT; END; $$
......
---
LOG
---
Log sequence number 20499052099 當(dāng)前的LSN
Log flushed up to 20499052099 表示刷新到redo log的LSN
Pages flushed up to 20499052099 表示刷新到磁盤的lsn
Last checkpoint at 20499052099
0 pending log writes, 0 pending chkp writes
373 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 137363456; in additional pool allocated 0
Dictionary memory allocated 1270857
Buffer pool size 8191
Free buffers 7562
Database pages 612
Old database pages 205
Modified db pages 0
Pending reads 0
......
mysql> call test.load_test(100000);
Query OK, 0 rows affected, 1 warning (6.28 sec)
mysql> show engine innodb status\G
......
---
LOG
---
Log sequence number 20504734913
Log flushed up to 20504734913
Pages flushed up to 20504734913
Last checkpoint at 20504734913
0 pending log writes, 0 pending chkp writes
398 log i/o's done, 0.38 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
......
看來Log sequence number和Log flushed up以及Pages flushed u3個(gè)還是一樣的。
再多導(dǎo)入點(diǎn)數(shù)據(jù),重新開啟另外一個(gè)窗口看看innodb狀態(tài)
mysql> call test.load_test(1000000);
mysql> show engine innodb status\G
......
---
LOG
---
Log sequence number 20527044411
Log flushed up to 20525763843
Pages flushed up to 20517902997
Last checkpoint at 20504829471
0 pending log writes, 0 pending chkp writes
432 log i/o's done, 1.26 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
這次看到的show engine innodb status的結(jié)果就不一樣了,Log sequence number > Log flushed up > Pages flushed up > Last checkpoint,所以從這里也可以看出,先寫redo日志,再寫數(shù)據(jù)文件
在實(shí)際寫比較頻繁的productiion上面,這3個(gè)值都會是不一樣的。
【2】Undo
undo的記錄正好與redo的相反,insert變成delete,update變成相反的update,redo放在redo file里面。而undo放在一個(gè)內(nèi)部的一個(gè)特殊segment上面,存儲與共享表空間內(nèi)(ibdata1或者ibdata2中)。
py_innodb_page_info.py
下載地址: wget http://david-mysql-tools.googlecode.com/svn-history/r15/trunk/py_innodb_page_type/py_innodb_page_info.py
[root@mysql data56]# python /root/py_innodb_page_info.py /data56/ibdata1
Traceback (most recent call last):
File "/root/py_innodb_page_info.py", line 3, in <module>
import mylib
ImportError: No module named mylib
[root@mysql data56]#
還需要下載 mylib.py和include.py文件,并和py_innodb_page_info.py放在一個(gè)目錄之下
wget http://david-mysql-tools.googlecode.com/svn-history/r15/trunk/py_innodb_page_type/mylib.py
wget http://david-mysql-tools.googlecode.com/svn-history/r15/trunk/py_innodb_page_type/include.py
ps:有的時(shí)候network會斷掉,要多執(zhí)行幾次wget去下載。
[root@mysql ~]# python /root/py_innodb_page_info.py /data56/ibdata1
Total number of page: 8960:
Insert Buffer Free List: 1035
Insert Buffer Bitmap: 1
System Page: 134
Transaction system Page: 1
Freshly Allocated Page: 4010
Undo Log Page: 1556
File Segment inode: 5
B-tree Node: 2217
File Space Header: 1
[root@mysql ~]#
[root@mysql ~]#
undo不是物理恢復(fù),是邏輯恢復(fù),因?yàn)樗峭ㄟ^執(zhí)行相反的dml語句來實(shí)現(xiàn)的。而且不會回收因?yàn)閕nsert和upate而新增加的page頁的。
undo頁的回收是通過master thread線程來實(shí)現(xiàn)的。
驗(yàn)證row模式下,生產(chǎn)的binlog傳到從庫上面,大概需要多久!
導(dǎo)出
time /usr/local/mysql56/bin/mysqldump --socket=/data56/mysql.sock -uroot -p123456 -P3307 --extended-insert=false --all-databases> /root/adb.sql
[root@mysql ~]# time /usr/local/mysql56/bin/mysqldump --socket=/data56/mysql.sock -uroot -p123456 -P3307 --extended-insert=false --all-databases> /root/adb.sql
Warning: Using a password on the command line interface can be insecure.
real 5m10.757s
user 2m42.137s
sys 0m11.346s
導(dǎo)入
time /usr/local/mysql56/bin/mysql --socket=/data56/mysql.sock -uroot -p123456 -P3307 < /root/adb.sql
在從庫上面檢查:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.88.49.119
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql56-bin.000008
Read_Master_Log_Pos: 513272653
Relay_Log_File: mysql56-relay-bin.000019
Relay_Log_Pos: 103964146
Relay_Master_Log_File: mysql56-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 418536570
Relay_Log_Space: 513273109
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 857
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11901
Master_UUID: a6a1d870-80b5-11e2-84d2-00155d016a07
Master_Info_File: /data56/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: System lock
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ps:Seconds_Behind_Master: 857,有延時(shí)的情況出現(xiàn),可見在row模式下,import6G的單調(diào)insert的sql的時(shí)候,有延時(shí)。
感謝各位的閱讀,以上就是“MySQL innodb事務(wù)的實(shí)現(xiàn)方式”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對MySQL innodb事務(wù)的實(shí)現(xiàn)方式這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
分享名稱:MySQLinnodb事務(wù)的實(shí)現(xiàn)方式
URL鏈接:http://jinyejixie.com/article40/gdpgeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈、微信小程序、ChatGPT、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)