使用 CLion 調(diào)試 MySQL

編譯 MySQL 5.5

-- Packaging as: mysql-5.5.62-Linux-x86_64

安裝需要的軟件包

sudo apt install libncurses5-dev
sudo apt install bison
sudo apt install libssl-dev # 5.6 需要

編譯出錯

/tmp/tmp.yhM6AmIL7d/mysys/my_lib.c: In function ‘my_dir’:
/tmp/tmp.yhM6AmIL7d/mysys/my_lib.c:140:3: error: ‘readdir_r’ is deprecated [-Werror=deprecated-declarations]
   while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp)))
   ^~~~~
In file included from /usr/include/features.h:364:0,
                 from /usr/include/pthread.h:21,
                 from /tmp/tmp.yhM6AmIL7d/include/my_global.h:274,
                 from /tmp/tmp.yhM6AmIL7d/mysys/mysys_priv.h:16,
                 from /tmp/tmp.yhM6AmIL7d/mysys/my_lib.c:19:
/usr/include/dirent.h:189:12: note: declared here
 extern int __REDIRECT (readdir_r,

解決方案:

5.5 is covered under Oracle Lifetime Sustaining Support and will no longer be supported with updates. See

https://www.mysql.com/support/eol-notice.html

The problem isn't present on 5.6+.

參考:https://bugs.mysql.com/bug.php?id=85536

通過 CLion 直接安裝 MySQL 5.6(未完成)

因為 MySQL 5.6 初始化數(shù)據(jù)比較麻煩,不支持 mysqld--initialize-insecure 選項。
所以不選用這種方式了。

參考:MySQL5.7和5.6初始化數(shù)據(jù)的區(qū)別

/usr/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /tmp/tmp.oqztrbxJO0
-- Running cmake version 3.7.2
-- Could NOT find Git (missing:  GIT_EXECUTABLE) 
-- MySQL 5.6.51
-- Packaging as: mysql-5.6.51-Linux-x86_64
...

編譯

  • (1)從 Github 下載 MySQL 5.6 源碼。
  • (2)導入 CLion 中。
  • (3)設置 CMake 參數(shù)。
    • CMAKE_INSTALL_PREFIX:安裝基本目錄。默認值:/usr/local/mysql
    • WITH_DEBUG:禁用優(yōu)化并生成調(diào)試信息,和 -DCMAKE_BUILD_TYPE=Debug 效果相同。
# 設置 mysql 的安裝目錄為 /opt/mysql
-DCMAKE_INSTALL_PREFIX=/opt/mysql -DWITH_DEBUG=1
  • (4)點擊菜單欄【Build】=》【Build All in 'Debug'】進行構(gòu)建。如果出現(xiàn)問題,就在 Remote Host 中安裝需要的軟件包(如上文所示)。

第(2)和第(4)步可以參考 使用 CLion 調(diào)試 redis。

編譯成功輸出:

====================[ Build | mysql | Debug ]===================================
/usr/bin/cmake --build /tmp/tmp.oqztrbxJO0/cmake-build-debug --target mysql -- -j 6
[  0%] Generating help.h
[  6%] Generating common.h
...
[100%] Built target mysql

Build finished

安裝和初始化數(shù)據(jù)目錄

# 創(chuàng)建 /opt/mysql 目錄
root@test:/# mkdir -p /opt/mysql                    
root@test:/# ls -al /opt
total 12
drwxr-xr-x  3 root root 4096 May 27 18:33 .
drwxr-xr-x 24 root root 4096 May 27 18:33 ..
drwxr-xr-x  2 root root 4096 May 27 18:33 mysql

# 設置 /opt/mysql 的用戶和用戶組ID
root@test:/# chown -R meikai:meikai /opt 
root@test:/# ls -al /opt
total 12
drwxr-xr-x  3 meikai meikai 4096 May 27 18:33 .
drwxr-xr-x 24 root   root   4096 May 27 18:33 ..
drwxr-xr-x  2 meikai meikai 4096 May 27 18:33 mysql

擊菜單欄【Build】=》【Install】進行安裝。


安裝成功輸出:

====================[ Install | Debug ]=========================================
/usr/bin/cmake --build /tmp/tmp.dWJ8OrPkMF/cmake-build-debug --target install -- -j 6
[  2%] Built target zlib
[  4%] Built target event_extra
[  7%] Built target edit
[  9%] Built target event
[  9%] Built target INFO_BIN
[ 11%] Built target event_core
[ 11%] Built target INFO_SRC
[ 15%] Built target strings
...
-- Installing: /opt/mysql/sql-bench/test-wisconsin

Install finished

初始化數(shù)據(jù)目錄:

# 進入 /opt/mysql 目錄
meikai@test:~$ cd /opt/mysql
# 初始化數(shù)據(jù)目錄 /opt/mysql/data
meikai@test:/opt/mysql$ scripts/mysql_install_db

選擇 mysqld configuration, 以 Debug 模式運行。

成功運行輸出:

/tmp/tmp.dWJ8OrPkMF/cmake-build-debug/sql/mysqld
...
2021-05-27 19:05:03 8471 [Note] /tmp/tmp.dWJ8OrPkMF/cmake-build-debug/sql/mysqld: ready for connections.
Version: '5.6.51-debug'  socket: '/tmp/mysql.sock'  port: 3306  Source distribution

斷點調(diào)試

在 CLion 中,對 sql_parse.cc 中的 mysql_execute_command() 方法進行斷點。

執(zhí)行下面的語句連接上 mysql 服務。

meikai@test:/opt/mysql$ bin/mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.51-debug Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql-client 中執(zhí)行如下命令:

mysql> show databases;

然后在 CLion 中即可發(fā)現(xiàn)已經(jīng)被斷點攔截,且可以發(fā)現(xiàn) Statement class 中的 query_string 字段和 我們上面輸入的命令 show databases 是一樣。

至此,已經(jīng)完成了 CLion 調(diào)試 MySQL 環(huán)境的搭建。

配置 DBUG 調(diào)試輸出

參考:5.8.3 The DBUG Package

對于 mysqld,可以通過設置 debug 系統(tǒng)變量在運行時更改 DBUG設置 。

此變量具有全局值和會話值:

mysql> SET GLOBAL debug = 'debug_options';
mysql> SET SESSION debug = 'debug_options';

debug_options 值是用冒號(:)分隔的字段的序列:

field_1:field_2:...:field_N

值中的每個字段都包含一個強制性標志字符。

我們使用的標志字符如下所示:

  • d:啟用所有 BUG_XXX 的輸出。
  • t:跟蹤函數(shù)調(diào)用和退出。
  • F:確定每一行調(diào)試或跟蹤輸出的源文件名。
  • L:確定每一行調(diào)試或跟蹤輸出的源文件行號。

root 用戶連接到 mysqld。

meikai@test:/opt/mysql$ bin/mysql -uroot
...

mysql> show variables like 'debug';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| debug         |       |
+---------------+-------+
1 row in set (0.00 sec)

mysql> set GLOBAL debug ='d:t:F:L';
Query OK, 0 rows affected (0.00 sec)

mysql> select @@debug;
+---------+
| @@debug |
+---------+
| d:F:L:t |
+---------+
1 row in set (0.01 sec)

然后,在 mysql 中執(zhí)行 select 語句,就可以在 CLion console 中觀察到如下圖所示輸出。

DBUG 日志輸出

參考

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容