MySQL|MySQL 源码阅读 —— macOS CLion 编译调试 MySQL 5.7
macOS + VSCode 版本看 这里环境信息
- macOS Big Sur 11.5.2
- CLion 2021.2
- MySQL 5.7.35
- CMake 3.21.1
- openssl 1.1
从官网下载携带boost
版本源码
下载链接:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.35.tar.gz
也可以从 GitHub 上克隆代码,切换到指定 TAG 或分支。
文章图片
Patch 源码 如果
MySQL <= 8.0.21
,则需要执行以下脚本 Patch 源码:mv VERSION MYSQL_VERSION
sed -i '' 's|${CMAKE_SOURCE_DIR}/VERSION|${CMAKE_SOURCE_DIR}/MYSQL_VERSION|g' cmake/mysql_version.cmake
具体原因,可参考文章:MySQL 源码 —— 问题 expanded from macro MYSQL_VERSION_MAJOR
配置 CMake
文章图片
CMake options 配置如下:
-DWITH_DEBUG=1
-DDOWNLOAD_BOOST=1
-DDOWNLOAD_BOOST_TIMEOUT=60000
-DWITH_BOOST=boost
-DCMAKE_INSTALL_PREFIX=build_out
-DMYSQL_DATADIR=build_out/data
-DSYSCONFDIR=build_out/etc
-DMYSQL_TCP_PORT=3307
-DMYSQL_UNIX_ADDR=mysql-debug.sock
解释下上面的参数:
-DWITH_DEBUG=1# 开启DEBUG模式
-DDOWNLOAD_BOOST=1# boost不存在时自动下载
-DDOWNLOAD_BOOST_TIMEOUT=60000# 下载boost的超时时间
-DWITH_BOOST=boost# boost目录,不存在时会自动下载到该目录
-DCMAKE_INSTALL_PREFIX=build_out# MySQL安装目录,可在启动时指定`--basedir`覆盖
-DMYSQL_DATADIR=build_out/data# MySQL数据目录,可在启动时指定`--datadir`覆盖
-DSYSCONFDIR=build_out/etc# `my.cnf`默认目录,可在启动时指定`--defaults-file=file_name`覆盖
-DMYSQL_TCP_PORT=3307# 如果本机已安装过MySQL,避免冲突换个别的
-DMYSQL_UNIX_ADDR=mysql-debug.sock # 默认/tmp/mysql.sock,避免冲突,此处相对与`--datadir`目录会自动创建
所有可选参数: MySQL Source-Configuration Options【MySQL|MySQL 源码阅读 —— macOS CLion 编译调试 MySQL 5.7】手动创建配置中的目录:
# 为何加`cmake-build-debug`前缀,因为`CLion`中`CMake`的`Build directory`就是`cmake-build-debug`,可自行修改
mkdir -p cmake-build-debug/build_out cmake-build-debug/build_out/data cmake-build-debug/build_out/etc
运行 CMake 方法一:
Tools
> CMake
> Reset Cache and Reload Project
方法二:
View
> Tool Windows
> CMake
> Reset Cache and Reload Project
文章图片
执行完后,
CMake
窗口输出如下内容:/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_DEBUG=1 -DDOWNLOAD_BOOST=1 -DDOWNLOAD_BOOST_TIMEOUT=60000 -DWITH_BOOST=boost -DCMAKE_INSTALL_PREFIX=build_out -DMYSQL_DATADIR=build_out/data -DSYSCONFDIR=build_out/etc -DMYSQL_TCP_PORT=3307 -DMYSQL_UNIX_ADDR=mysql-debug.sock -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - Unix Makefiles" /path/to/mysql-5.7.35······
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/mysql-5.7.35/cmake-build-debug
编译 mysqld 点击
CLion
窗口右上角 Build
按钮或快捷键 ?+F9
:文章图片
首次编译会比较慢,编译完成后输出如下内容:
====================[ Build | mysqld | Debug ]==================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /path/to/mysql-5.7.35/cmake-build-debug --target mysqld -- -j 3
[0%] Built target regex
··· 中间日志省略 ···
[100%] Built target mysqldBuild finished
配置 my.cnf
如有其他定制化配置需要,可添加/修改
my.cnf
在 CMake
配置的 -DSYSCONFDIR=build_out/etc
目录下,创建 my.cnf
文件,并编辑:[mysqld]
innodb_file_per_table = 1
初始化 mysqld 点击
CLion
窗口右上角 Edit Configurations...
:文章图片
找到
mysqld
,并配置 Program arguments
:文章图片
Program arguments
配置内容:--initialize-insecure
点击
CLion
窗口右上角 Run
图标或快捷键 ?+R
,进行初始化:文章图片
初始化后,输出如下内容:
/path/to/mysql-5.7.35/cmake-build-debug/sql/mysqld --initialize-insecure
2021-08-25T05:35:14.403520Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-25T05:35:14.403970Z 0 [ERROR] Can't find error-message file '/path/to/mysql-5.7.35/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2021-08-25T05:35:14.405823Z 0 [Warning] Setting lower_case_table_names=2 because file system for /path/to/mysql-5.7.35/cmake-build-debug/build_out/data/ is case insensitive
2021-08-25T05:35:14.658572Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-25T05:35:15.068077Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-25T05:35:15.214272Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 398f5a88-0566-11ec-9fc8-c1b33e1edaea.
2021-08-25T05:35:15.218653Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-25T05:35:16.089766Z 0 [Warning]
2021-08-25T05:35:16.089804Z 0 [Warning]
2021-08-25T05:35:16.090612Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-25T05:35:16.385026Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
此时,MySQL 已自动创建一个
root@localhost
账号,密码为空!再将
Program arguments
配置中的 --initialize-insecure
删除!!!因为初始化只需执行一次,后续的执行或 Debug 都会用到这里的配置,不删除就会报错。
开始 DEBUG 点击
CLion
窗口右上角 Debug
图标或快捷键 ?+D
,开始 Debug:文章图片
通过已有客户端连接到 MySQL 服务器,比如:
mysql -uroot -P3307 -h127.0.0.1
也可以通过 Navicat 连接:
文章图片
然后在客户端输入一条 SQL,
CLion
Debug 窗口就会提示:文章图片
常见问题
Warning 级别的错误,可忽略CMake Error at cmake/boost.cmake
错误描述
此错误在执行
cmake
时会报错
CMake Error at cmake/boost.cmake:88 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=This CMake script will look for boost in .If it is not there,
it will download and unpack it (in that directory) for you.If you are inside a firewall, you may need to use an http proxy:export http_proxy=http://example.com:80Call Stack (most recent call first):
cmake/boost.cmake:245 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:536 (INCLUDE)
编译 MySQL 源码,CMake 需要配置
-DWITH_BOOST=
选项。确认源码是否携带
boost
区分源码中是否携带有 boost
的方式有2种:- 通过下载 URL 或源码文件名区分
https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.35.tar.gz
https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.35.tar.gz
文章图片
- 源码中是否有
boost
目录
从 GitHub 下载的源码是不含
boost
的
带 boost
的源码安装
cmake . -DWITH_BOOST=boost
不带
boost
的源码安装
cmake . \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=
这是 MySQL 建议的构建方式。CMake 会到
下查找符合版本要求的 boost
,如果不存在则会下载并解压到该目录下。执行构建后的输出示例:
...
-- MySQL 5.7.35
-- Packaging as: mysql-5.7.35-osx10.16-x86_64
-- DTRACE is enabled
-- Downloading boost_1_59_0.tar.gz to /path/to/mysql-server-mysql-5.7.35/boost
-- cd /path/to/mysql-server-mysql-5.7.35/boost;
tar xfz /path/to/mysql-server-mysql-5.7.35/boost/boost_1_59_0.tar.gz
-- Found /path/to/mysql-server-mysql-5.7.35/boost/boost_1_59_0/boost/version.hpp
-- BOOST_VERSION_NUMBER is #define BOOST_VERSION 105900
-- BOOST_INCLUDE_DIR /path/to/mysql-server-mysql-5.7.35/boost/boost_1_59_0
...
或者手动下载并解压到某个目录,再在
WITH_BOOST
参数中指定 boost
目录:wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxf boost_1_59_0.tar.gz
cmake . \
-DWITH_BOOST=
Warning: define bzero please_use_memset_rather_than_bzero
在构建过程中,可能会报如下 Warning
[ 31%] Building C object storage/myisammrg/CMakeFiles/myisammrg.dir/myrg_records.c.o
In file included from /path/to/mysql-5.7.35/storage/myisam/mi_page.c:25:
In file included from /path/to/mysql-5.7.35/storage/myisam/myisamdef.h:26:
In file included from /path/to/mysql-5.7.35/include/myisam.h:34:
/path/to/mysql-5.7.35/include/m_string.h:32:9: warning: 'bzero' macro redefined [-Wmacro-redefined]
#define bzero please_use_memset_rather_than_bzero
^
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/secure/_strings.h:52:9: note: previous definition is here
#define bzero(dest, ...) \
^
In file included from /path/to/mysql-5.7.35/storage/myisammrg/myrg_records.c:24:
In file included from /path/to/mysql-5.7.35/storage/myisammrg/myrg_def.h:25:
In file included from /path/to/mysql-5.7.35/storage/myisammrg/../myisam/myisamdef.h:26:
In file included from /path/to/mysql-5.7.35/include/myisam.h:34:
/path/to/mysql-5.7.35/include/m_string.h:32:9: warning: 'bzero' macro redefined [-Wmacro-redefined]
#define bzero please_use_memset_rather_than_bzero
^
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/secure/_strings.h:52:9: note: previous definition is here
#define bzero(dest, ...) \
^
1 warning generated.
MacOSX11.3.sdk
中定义 bzero
时的提示:/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/secure/_strings.h:52:9: note: previous definition is here#define bzero(dest, ...) \
__builtin___memset_chk (dest, 0, __VA_ARGS__, __darwin_obsz0 (dest))
#endif
意思是
bzero
这个宏已经在 mysql
引入的文件中定义过了:/path/to/mysql-5.7.35/include/m_string.h:32:9: warning: 'bzero' macro redefined [-Wmacro-redefined]#define bzero please_use_memset_rather_than_bzero
而
mysql
中的宏定义却并非函数代码,反而提示应该用 memset
函数,而非 bzero
函数,此意是提示 MySQL
开发者后续要修改代码了。此处是用 redefined 来提示,并不对实际运行产生影响。在 MySQL 8.0 中已经没了此类提示。所以,此类 Warning 我们可忽略。
expanded from macro MYSQL_VERSION_MAJOR
参考 MySQL 源码 —— 问题 expanded from macro MYSQL_VERSION_MAJOR
编译 mysql 等工具
此前,我们只编译了
mysqld
服务器这一个软件。但我们需要 mysql
客户端,或 mysqldump
等其他工具时,也可以选择配置其他项,并点击 Build
就可以。文章图片
参考资料
- 《MySQL Internals Manual》Building MySQL Server with CMake
- 《MySQL 5.7 Reference Manual》Installing MySQL from Source
- 《MySQL 5.7 Reference Manual》MySQL Source-Configuration Options
- MySQL 源码阅读 —— 问题 expanded from macro MYSQL_VERSION_MAJOR
感谢您的阅读,觉得内容不错,点个赞吧
原文地址: https://shockerli.net/post/mysql-source-macos-clion-debug-5-7/
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- Ⅴ爱阅读,亲子互动——打卡第178天
- 上班后阅读开始变成一件奢侈的事
- 历史教学书籍
- 绘本讲师训练营【24期】14/21阅读原创《小黑鱼》
- 21天|21天|M&M《见识》04
- 绘本讲师训练营7期9/21阅读原创《蜗牛屋|绘本讲师训练营7期9/21阅读原创《蜗牛屋 》
- 桂妃研读社|桂妃研读社|D124|如何有效阅读一本书 Day1
- Android事件传递源码分析
- 4.23世界阅读日,樊登读书狂欢放送,听书中成长