如何理解MySQL层事务提交流程,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

部分栈帧:

prepare栈帧

(gdb)bt#0binlog_prepare(hton=0x346f260,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:1578#10x0000000000f64e80inha_prepare_low(thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2348#20x0000000001856176inMYSQL_BIN_LOG::prepare(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8165#30x0000000000f63d1finha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1803#40x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#50x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#60x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#70x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#80x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#90x00000000016e28f0inhandle_connection(arg=0x384a5d0)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#100x0000000001d7a514inpfs_spawn_thread(arg=0x3848260)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#110x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#120x0000003f740e8bcdinclone()from/lib64/libc.so.6(gdb)binnobase_trx_initBreakpoint7at0x19a5fdb:file/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc,line2753.(gdb)cContinuing.

trx_prepare_low

Breakpoint7,innobase_trx_init(thd=0x7fff2c000b70,trx=0x7ffff2f2db00)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:27532753DBUG_ENTER("innobase_trx_init");(gdb)bt#0innobase_trx_init(thd=0x7fff2c000b70,trx=0x7ffff2f2db00)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:2753#10x00000000019a6291incheck_trx_exists(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:2816#20x00000000019c2b90ininnobase_xa_prepare(hton=0x2e9cdd0,thd=0x7fff2c000b70,prepare_trx=false)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:17458#30x0000000000f64e80inha_prepare_low(thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2348#40x0000000001856176inMYSQL_BIN_LOG::prepare(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8165#50x0000000000f63d1finha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1803#60x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#70x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#80x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#90x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#100x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#110x00000000016e28f0inhandle_connection(arg=0x384a5d0)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#120x0000000001d7a514inpfs_spawn_thread(arg=0x3848260)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#130x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#140x0000003f740e8bcdinclone()from/lib64/libc.so.6(gdb)

flush 之前写redo 栈帧
1145 bool write_header = new_data_offset == 0;

(gdb)bt#0log_group_write_buf(group=0x33ef9f8,buf=0x7fffa5b38000"\200\020\300\253\002",len=1024,pad_len=0,start_lsn=562123776,new_data_offset=465)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/log/log0log.cc:1145#10x0000000001a50dd5inlog_write_up_to(lsn=562124558,flush_to_disk=true)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/log/log0log.cc:1493#20x0000000001a50ec7inlog_buffer_flush_to_disk(sync=true)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/log/log0log.cc:1524#30x00000000019a9077ininnobase_flush_logs(hton=0x2e9cdd0,binlog_group_flush=true)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4407#40x0000000000f65893inflush_handlerton(thd=0x0,plugin=0x7ffff03588e8,arg=0x7ffff0358944)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2606#50x00000000015d76eeinplugin_foreach_with_mask(thd=0x0,func=0xf65835<flush_handlerton(THD*,plugin_ref,void*)>,type=1,state_mask=4294967287,arg=0x7ffff0358944)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_plugin.cc:2318#60x0000000000f658efinha_flush_logs(db_type=0x0,binlog_group_flush=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2617#70x000000000185725dinMYSQL_BIN_LOG::process_flush_stage_queue(this=0x2dffe80,total_bytes_var=0x7ffff0358a88,rotate_var=0x7ffff0358a87,out_queue_var=0x7ffff0358a78)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8541#80x00000000018588bfinMYSQL_BIN_LOG::ordered_commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false,skip_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9189#90x0000000001856f2cinMYSQL_BIN_LOG::commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#100x0000000000f63df8inha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818#110x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#120x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#130x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#140x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#150x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#160x00000000016e28f0inhandle_connection(arg=0x68e1060)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#170x0000000001d7a514inpfs_spawn_thread(arg=0x3848660)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#180x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#190x0000003f740e8bcdinclone()from/lib64/libc.so.6

order commit innodb层commit栈帧

Breakpoint2,trx_purge_add_update_undo_to_history(trx=0x7ffff2f2e068,undo_ptr=0x7ffff2f2e488,undo_page=0x7fffb0244000"E\373\332r",update_rseg_history_len=true,n_added_logs=1,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0purge.cc:332332undo=undo_ptr->update_undo;(gdb)bt#0trx_purge_add_update_undo_to_history(trx=0x7ffff2f2e068,undo_ptr=0x7ffff2f2e488,undo_page=0x7fffb0244000"E\373\332r",update_rseg_history_len=true,n_added_logs=1,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0purge.cc:332#10x0000000001bef543intrx_undo_update_cleanup(trx=0x7ffff2f2e068,undo_ptr=0x7ffff2f2e488,undo_page=0x7fffb0244000"E\373\332r",update_rseg_history_len=true,n_added_logs=1,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0undo.cc:1962#20x0000000001bddf57intrx_write_serialisation_history(trx=0x7ffff2f2e068,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:1698#30x0000000001bdf45dintrx_commit_low(trx=0x7ffff2f2e068,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2204#40x0000000001bdf5d2intrx_commit(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2274#50x0000000001bdff38intrx_commit_for_mysql(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2550#60x00000000019a9101ininnobase_commit_low(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4451#70x00000000019a99e7ininnobase_commit(hton=0x2e9cdd0,thd=0x7fff2c000b70,commit_trx=false)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4740#80x0000000000f64104inha_commit_low(thd=0x7fff2c000b70,all=false,run_after_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1923#90x000000000185764binMYSQL_BIN_LOG::process_commit_stage_queue(this=0x2dffe80,thd=0x7fff2c000b70,first=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8647#100x0000000001858e7dinMYSQL_BIN_LOG::ordered_commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false,skip_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9318#110x0000000001856f2cinMYSQL_BIN_LOG::commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#120x0000000000f63df8inha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818#130x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#140x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#150x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#160x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#170x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#180x00000000016e28f0inhandle_connection(arg=0x68e11d0)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#190x0000000001d7a514inpfs_spawn_thread(arg=0x38484d0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#200x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#210x0000003f740e8bcdinclone()from/lib64/libc.so.6(gdb)cContinuing.Breakpoint8,trx_commit_in_memory(trx=0x7ffff2f2e068,mtr=0x7ffff03582f0,serialised=true)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:19721972trx->must_flush_log_later=false;(gdb)bt#0trx_commit_in_memory(trx=0x7ffff2f2e068,mtr=0x7ffff03582f0,serialised=true)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:1972#10x0000000001bdf52aintrx_commit_low(trx=0x7ffff2f2e068,mtr=0x7ffff03582f0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2250#20x0000000001bdf5d2intrx_commit(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2274#30x0000000001bdff38intrx_commit_for_mysql(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2550#40x00000000019a9101ininnobase_commit_low(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4451#50x00000000019a99e7ininnobase_commit(hton=0x2e9cdd0,thd=0x7fff2c000b70,commit_trx=false)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4740#60x0000000000f64104inha_commit_low(thd=0x7fff2c000b70,all=false,run_after_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1923#70x000000000185764binMYSQL_BIN_LOG::process_commit_stage_queue(this=0x2dffe80,thd=0x7fff2c000b70,first=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8647#80x0000000001858e7dinMYSQL_BIN_LOG::ordered_commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false,skip_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9318#90x0000000001856f2cinMYSQL_BIN_LOG::commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#100x0000000000f63df8inha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818#110x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#120x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#130x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#140x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#150x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#160x00000000016e28f0inhandle_connection(arg=0x68e11d0)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#170x0000000001d7a514inpfs_spawn_thread(arg=0x38484d0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#180x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#190x0000003f740e8bcdinclone()from/lib64/libc.so.6(gdb)cContinuing.Breakpoint3,trx_commit_complete_for_mysql(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:25702570if(trx->id!=0(gdb)bt#0trx_commit_complete_for_mysql(trx=0x7ffff2f2e068)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/trx/trx0trx.cc:2570#10x00000000019a9aabininnobase_commit(hton=0x2e9cdd0,thd=0x7fff2c000b70,commit_trx=false)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4762#20x0000000000f64104inha_commit_low(thd=0x7fff2c000b70,all=false,run_after_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1923#30x000000000185764binMYSQL_BIN_LOG::process_commit_stage_queue(this=0x2dffe80,thd=0x7fff2c000b70,first=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8647#40x0000000001858e7dinMYSQL_BIN_LOG::ordered_commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false,skip_commit=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9318#50x0000000001856f2cinMYSQL_BIN_LOG::commit(this=0x2dffe80,thd=0x7fff2c000b70,all=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#60x0000000000f63df8inha_commit_trans(thd=0x7fff2c000b70,all=false,ignore_global_read_lock=false)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818#70x00000000016b3d7dintrans_commit_stmt(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458#80x00000000015ac64finmysql_execute_command(thd=0x7fff2c000b70,first_level=true)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239#90x00000000015adcaeinmysql_parse(thd=0x7fff2c000b70,parser_state=0x7ffff035b600)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836#100x00000000015a1b6dindispatch_command(thd=0x7fff2c000b70,com_data=0x7ffff035bd70,command=COM_QUERY)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447#110x00000000015a099eindo_command(thd=0x7fff2c000b70)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010#120x00000000016e28f0inhandle_connection(arg=0x68e11d0)at/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312#130x0000000001d7a514inpfs_spawn_thread(arg=0x38484d0)at/root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188#140x0000003f74807aa1instart_thread()from/lib64/libpthread.so.0#150x0000003f740e8bcdinclone()from/lib64/libc.so.6

看完上述内容,你们掌握如何理解MySQL层事务提交流程的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!