分享MySQL常用?內(nèi)核?Debug?幾種常見方法
閱讀本文你將了解:
- 如何準(zhǔn)備 MySQL 調(diào)試環(huán)境
- GDB 調(diào)試入門及操作示例
- Trace 文件調(diào)試及操作示例
一、準(zhǔn)備 Debug 環(huán)境
首先用源碼編譯安裝一個用來調(diào)試的 MySQL 環(huán)境。
開啟-DWITH_DEBUG
,在源碼路徑創(chuàng)建 build 目錄,
進入目錄并執(zhí)行:
cmake .. -DWITH_BOOST=../../boost -DWITH_DEBUG=1
然后通過如下方式,確認(rèn)是否編譯成功。
方式一:
$ ./bin/mysqld --verbose --version
回顯 debug
版本信息,則編譯的是 debug 版本。
ver 8.0.18-debug for Linux on x86_64 (Source distribution)
方式二:
連接數(shù)據(jù)庫,執(zhí)行查看版本命令?;仫@包含了 debug
字樣,則編譯的是 debug 版本。
$ mysql> select version(); +--------------+ | version() ? ?| +--------------+ | 8.0.18-debug | +--------------+ 1 row in set (0.00 sec)
二、使用 GDB 調(diào)試
GDB 全稱 “GNU symbolic debugger”,是 Linux 下常用的程序調(diào)試器,通常以 gdb 命令的形式在終端(Shell)中使用。
啟動 GDB 編譯器
執(zhí)行如下命令啟動 GDB 編譯器(假設(shè) my.cnf 在用戶根目錄中)。進入 GDB 后,敲入 run 即可運行。
gdb --args ./bin/mysqld --defaults-file=~/my.cnf --gdb
其中 --gdb 參數(shù)允許你隨時 Ctrl+C 的方式中斷 mysqld 進程,進行調(diào)試命令。
GDB 常用命令
使用多窗口查看源碼與調(diào)試的讀者,可以使用 layout 命令,在 gdb 中執(zhí)行help layout
可以查看更多 gdb 命令用法。
(gdb) help layout (gdb) help layoutChange the layout of windows. Usage: layout prev | next | <layout_name> Layout names are: ? ?src ? : Displays source and command windows. ? ?asm ? : Displays disassembly and command windows. ? ?split : Displays source, disassembly and command windows. ? ?regs ?: Displays register window. If existing layout ? ? ? ? ? ?is source/command or assembly/command, the ? ? ? ? ? ?register window is displayed. If the ? ? ? ? ? ?source/assembly/command (split) is displayed, ? ? ? ? ? ?the register window is displayed with ? ? ? ? ? ?the window that has current logical focus. ? ? ? ? ? ?(gdb)
Debug 示例
安裝好 Debug 環(huán)境后,我們用以下兩個例子,來簡單演示使用思路及技巧。
1、取變量值
在某種情況下發(fā)現(xiàn) mysqld 已經(jīng) crash,系統(tǒng)只有一個 core 文件,而我們要知道某個系統(tǒng)變量的值。但是系統(tǒng)變量的值,不見得與 my.cnf 文件一致。
此時,就可以用 gdb 命令將變量打印出來,獲取變量值。
如下所示,需獲取變量 version
的值,只需要在前面加mysql_sysvar_
前綴打印即可。
Thread 1 "mysqld" received signal SIGINT, Interrupt. 0x00007ffff5f74cb9 in __GI___poll (fds=0x55555e8a3de0, nfds=2, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:29 29 ? ?../sysdeps/unix/sysv/linux/poll.c: No such file or directory. (gdb) p mysql_sysvar_version $1 = {flags = 68101, name = 0x55555e7ff738 "innodb_version", comment = 0x55555ca953e2 "InnoDB version", check = 0x555558e222f1 <check_func_str(THD*, SYS_VAR*, void*, st_mysql_value*)>, update = 0x555558e22881 <update_func_str(THD*, SYS_VAR*, void*, void const*)>, ? value = 0x55555def1c20 <innodb_version_str>, def_val = 0x55555ca89598 "8.0.18"} (gdb)
2、調(diào)試腳本
假設(shè)需獲取某一個連接進入dispatch_command
有哪些 command
,可以執(zhí)行 gdb 腳本[2] 獲取。
gdb 腳本內(nèi)容如下:
b dispatch_command commands ? ? print command ? ? continue end
執(zhí)行 gdb 腳本,然后使用 mysql
客戶端連接數(shù)據(jù)庫,并執(zhí)行 SQL 語句操作,即可查看到 gdb 調(diào)試信息。
(gdb) b dispatch_command Breakpoint 3 at 0x555558ddb37c: file /home/kyc/mysql8/sql/sql_parse.cc, line 1581. (gdb) commands Type commands for breakpoint(s) 3, one per line. End with a line saying just "end". >print command >continue >end (gdb) c Continuing. [Switching to Thread 0x7fffe01fc700 (LWP 5941)] Thread 49 "mysqld" hit Breakpoint 3, dispatch_command (thd=0x7fff4c000f70, com_data=0x7fffe01fbba0, command=COM_QUERY) at /home/kyc/galaxyengine/sql/sql_parse.cc:1581 1581 ? ? ? ? ? ? ? ? ? ? ? ? ?enum enum_server_command command) { $4 = COM_QUERY
三、使用 Trace 文件調(diào)試
MySQL 的 debug
版提供了一個專門的 DBUG 包[3]。通過這個 DBUG 包,可獲取正在執(zhí)行操作程序的 Trace 文件。
通過控制 DBUG 開關(guān),可以將 MySQL 的任何操作,以及所涉及的調(diào)用模塊、函數(shù)、狀態(tài)信息記錄在 Trace 文件中。
設(shè)置 debug 參數(shù)
通過設(shè)置 debug 參數(shù)選項,指定跟蹤方式。
--debug [ = debug_options ]
[ = debug _ options ] 可識別字符 d、t、i 、o 等。
Debug 示例
若需獲取代碼中DBUG_PRINT
("info:" 打印的日志,可以使用 MySQL 客戶端連上服務(wù)器,并執(zhí)行如下命令,開啟 debug 參數(shù)。
set debug = 'd,info'; use test;
看 mysqld.trace
文件,可獲取 use test 在 MySQL 中的執(zhí)行流程。
do_command: info: Command on socket (46) = 3 (Query) do_command: info: packet: ' ? ? ? ? ? ? ? ? '; command: 3 dispatch_command: info: command: 3 gtid_pre_statement_checks: info: gtid_next->type=0 owned_gtid.{sidno,gno}={0,0} THD::is_ddl_gtid_compatible: info: SQLCOM_CREATE:0 CREATE-TMP:0 SELECT:1 SQLCOM_DROP:0 DROP-TMP:0 trx:0 SELECT_LEX::prepare: info: setup_ref_array this 0x7fff1400d298 ? ?3 : ? ?0 ? ?0 ? ?1 ? ?2 ? ?0 ? ?0 setup_fields: info: thd->mark_used_columns: 1 setup_fields: info: thd->mark_used_columns: 1 SELECT_LEX::setup_conds: info: thd->mark_used_columns: 1 THD::decide_logging_format: info: query: SELECT DATABASE() THD::decide_logging_format: info: variables.binlog_format: 2 ................ MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff14019ae0 MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dd20 MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dcc0 net_send_ok: info: affected_rows: 0 ?id: 0 ?status: 2 ?warning_count: 0 net_send_ok: info: OK sent, so no more error sending allowed
本文使用幾個簡單的示例,演示了 MySQL 內(nèi)核的 Debug 的幾種常見方法。當(dāng)然,僅僅起到拋磚引玉的作用,更多好玩的技巧,還需讀者自行深度挖掘。
參考:
[2]: GDB 腳本調(diào)試:https://sourceware.org/gdb/current/onlinedocs/gdb/Commands.html#Commands
[3]: DBUG Package[:https://dev.mysql.com/doc/refman/8.0/en/dbug-package.html
相關(guān)文章
centos6.4下mysql5.7.18安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了centos6.4下mysql5.7.18安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07innodb如何巧妙的實現(xiàn)事務(wù)隔離級別詳解
隔離是ACID(Atomicity,Consistency,Isolation,Durability)的重要部分,下面這篇文章主要給大家介紹了關(guān)于innodb如何巧妙的實現(xiàn)事務(wù)隔離級別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-11-11VS2019連接MySQL數(shù)據(jù)庫的過程及常見問題總結(jié)
今天想使用VS2019可以配合MySQL一起使用,在配置過程中出現(xiàn)一些錯誤,下面通過本文給大家分享VS2019連接MySQL數(shù)據(jù)庫的過程及常見問題總結(jié),感興趣的朋友跟隨小編一起看看吧2021-11-11SQL實戰(zhàn)演練之網(wǎng)上商城數(shù)據(jù)庫商品類別數(shù)據(jù)操作
一直認(rèn)為,扎實的SQL功底是一名數(shù)據(jù)分析師的安身立命之本,甚至可以稱得上是所有數(shù)據(jù)從業(yè)者的基本功。當(dāng)然,這里的SQL絕不單單是寫幾條查詢語句那么簡單,接下來請跟著小編通過案例項目演練一遍商品類別的數(shù)據(jù)操作吧2021-10-10讓MySQL數(shù)據(jù)庫跑的更快 為數(shù)據(jù)減肥
在MySQL數(shù)據(jù)庫優(yōu)化工作中,使數(shù)據(jù)盡可能的小,使表在硬盤上占據(jù)的空間盡可能的小,這是最常用、也是最有效的手段之一。2011-03-03MySQL使用mysqldump+binlog完整恢復(fù)被刪除的數(shù)據(jù)庫原理解析
這篇文章主要介紹了MySQL使用mysqldump+binlog完整恢復(fù)被刪除的數(shù)據(jù)庫,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04