编译 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 初始化数据比较麻烦,不支持 mysqld
的 --initialize-insecure
选项。
所以不选用这种方式了。
参考:MySQL5.7和5.6初始化数据的区别
/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 参数。
-
CMAKE_INSTALL_PREFIX
:安装基本目录。默认值:/usr/local/mysql
-
WITH_DEBUG
:禁用优化并生成调试信息,和-DCMAKE_BUILD_TYPE=Debug
效果相同。
-
# 设置 mysql 的安装目录为 /opt/mysql
-DCMAKE_INSTALL_PREFIX=/opt/mysql -DWITH_DEBUG=1
- (4)点击菜单栏【Build】=》【Build All in 'Debug'】进行构建。如果出现问题,就在 Remote Host 中安装需要的软件包(如上文所示)。
第(2)和第(4)步可以参考 使用 CLion 调试 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
安装和初始化数据目录
# 创建 /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
初始化数据目录:
# 进入 /opt/mysql 目录
meikai@test:~$ cd /opt/mysql
# 初始化数据目录 /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
断点调试
在 CLion 中,对 sql_parse.cc
中的 mysql_execute_command()
方法进行断点。
执行下面的语句连接上 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
中执行如下命令:
mysql> show databases;
然后在 CLion 中即可发现已经被断点拦截,且可以发现 Statement class
中的 query_string
字段和 我们上面输入的命令 show databases
是一样。
至此,已经完成了 CLion 调试 MySQL 环境的搭建。
配置 DBUG 调试输出
参考:5.8.3 The DBUG Package
对于 mysqld
,可以通过设置 debug
系统变量在运行时更改 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
:跟踪函数调用和退出。 -
F
:确定每一行调试或跟踪输出的源文件名。 -
L
:确定每一行调试或跟踪输出的源文件行号。
以 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 中执行 select
语句,就可以在 CLion console 中观察到如下图所示输出。
DBUG 日志输出
参考
- 使用CLion编译调试MySQL
- CMake 入门实战
- Cmake工程报warning: XXX is deprecated [-Wdeprecated-declarations]
- ubuntu系统安装openssl-devel
- 2.9.4 Installing MySQL Using a Standard Source Distribution
- 2.10 Postinstallation Setup and Testing
- mysql修改源码发布及源码中的debug日志查看
- MySQL源码学习——DBUG调试