上一次写了篇文章MySQL5.7源码在MacOS上的编译运行,是在commandLine上编译及运行,而这次我要在Clion上编译并且完成单步调试,然后就可以逐步拆解MySQL源码了

参考文章:MySQL 源码阅读 —— macOS CLion 编译调试 MySQL 5.7

目录

  • 步骤一:源码下载
  • 步骤二:下载boost包
  • 步骤三:源码导入Clion
  • 步骤四:配置CMake
  • 步骤五:构建mysqld
  • 步骤六:初始化运行mysqld
  • 步骤七:单步调试

步骤一:源码下载

首先,需要下载源码

打开 https://dev.mysql.com/downloads/mysql/,点击Looking for the latest GA version,按图中所选,点击Download

下载后解压缩包,tar -xzvf mysql-5.7.40.tar.gz,得到一个文件夹:mysql-5.7.40,里面就是源码

步骤二:下载boost包

打开 https://www.boost.org/,点击More Downloads…,点击Version History,找到1.59.0,点击Download

下载完后可以得到一个压缩包,然后解压,tar -zxvf boost_1_59_0.tar.gz,得到一个文件夹:boost_1_59_0

步骤三:源码导入Clion

当然,先得下载软件Clion,通过注册码验证,我买的是正版,这里就不分享具体过程了

点击File - Open,选择mysql-5.7.40文件夹,Open
导入后项目结构如下

步骤四:配置CMake

点击CLion - Preferences - Build, Execution, Deployment - CMake

CMAKE参数

-DWITH_DEBUG=1
-DWITH_BOOST=/Users/peteryuanpan/mysql/boost_1_59_0
-DCMAKE_INSTALL_PREFIX=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out
-DMYSQL_DATADIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data
-DSYSCONFDIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/etc
-DMYSQL_TCP_PORT=3307
-DMYSQL_UNIX_ADDR=mysql-debug.sock

点击Tools - CMake - Reset Cache and Reload Project

运行结果

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_DEBUG=1 -DWITH_BOOST=/Users/peteryuanpan/mysql/boost_1_59_0 -DCMAKE_INSTALL_PREFIX=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out -DMYSQL_DATADIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data -DSYSCONFDIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/etc -DMYSQL_TCP_PORT=3307 -DMYSQL_UNIX_ADDR=mysql-debug.sock -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - Unix Makefiles" /Users/peteryuanpan/mysql/mysql-5.7.40
CMake Deprecation Warning at CMakeLists.txt:30 (CMAKE_MINIMUM_REQUIRED):Compatibility with CMake < 2.8.12 will be removed from a future version ofCMake.......-- Configuring done
-- Generating done
-- Build files have been written to: /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug[Finished]

步骤五:构建mysqld

找到 mysqld,点击构建按钮
会编译打包出mysqld相关的动态链接库

运行结果(这一步需要些时间)

====================[ Build | mysqld | Debug ]==================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug --target mysqld -- -j 3
Scanning dependencies of target zlib
Scanning dependencies of target strings
[  0%] Generating include/probes_mysql_dtrace.h, include/probes_mysql_nodtrace.h
[  0%] Building C object extra/zlib/zlib-1.2.12/CMakeFiles/zlib.dir/adler32.c.o
[  0%] Built target gen_dtrace_header
[  0%] Building C object extra/zlib/zlib-1.2.12/CMakeFiles/zlib.dir/compress.c.o......[ 50%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/fut/fut0fut.cc.o
[ 50%] Building CXX object storage/perfschema/CMakeFiles/perfschema.dir/pfs_events_waits.cc.o
[ 50%] Building CXX object sql/CMakeFiles/binlog.dir/rpl_filter.cc.o
[ 50%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/fut/fut0lst.cc.o
[ 50%] Building CXX object storage/perfschema/CMakeFiles/perfschema.dir/pfs_global.cc.o......[ 85%] Building CXX object sql/CMakeFiles/sql.dir/procedure.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/protocol_classic.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/records.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/rpl_group_replication.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/rpl_handler.cc.o.......Scanning dependencies of target mysqld
[100%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
[100%] Linking CXX executable mysqld
[100%] Built target mysqldBuild finished

步骤六:初始化运行mysqld

点击Edit Configurations

找到mysqld,在Program arguments处输入–initialize-insecure,点击OK

然后运行 mysqld

运行结果(有错)

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld --initialize-insecure
mysqld: Can't create directory '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/' (Errcode: 2 - No such file or directory)
2022-10-26T09:42:00.421445Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:42:00.421934Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:42:00.423085Z 0 [ERROR] Aborting

有错误,这是因为文件夹cmake-build-debug/build_out/share没创建
在上面配置CMake过程中,指定了许多文件夹,需要将它们都创建一下

Command Line

mkdir -p cmake-build-debug/build_out cmake-build-debug/build_out/data cmake-build-debug/build_out/etc

然后再运行 mysqld

运行结果

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld --initialize-insecure
2022-10-26T09:44:18.416856Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:44:18.417402Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:44:18.418613Z 0 [Warning] Setting lower_case_table_names=2 because file system for /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ is case insensitive
2022-10-26T09:44:18.563112Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-26T09:44:18.650628Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-26T09:44:18.719189Z 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: c2f88054-5512-11ed-8613-adb99ad55f5a.
2022-10-26T09:44:18.751552Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-26T09:44:18.910258Z 0 [Warning]
2022-10-26T09:44:18.910274Z 0 [Warning]
2022-10-26T09:44:18.910903Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-26T09:44:18.967925Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.Process finished with exit code 0

完成初始化,用户名是root,无密码

在Program arguments处删除–initialize-insecure,否则会报错!

然后以Debug方式运行mysqld

运行结果

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld
2022-10-26T09:46:22.168628Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:46:22.169287Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2022-10-26T09:46:22.169357Z 0 [Note] /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld (mysqld 5.7.40-debug) starting as process 54634 ...
2022-10-26T09:46:22.169426Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:46:22.172069Z 0 [Warning] Setting lower_case_table_names=2 because file system for /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ is case insensitive
2022-10-26T09:46:22.174448Z 0 [Note] InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!
2022-10-26T09:46:22.174474Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-10-26T09:46:22.174479Z 0 [Note] InnoDB: Uses event mutexes
2022-10-26T09:46:22.174485Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2022-10-26T09:46:22.174491Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.12
2022-10-26T09:46:22.175028Z 0 [Note] InnoDB: Number of pools: 1
2022-10-26T09:46:22.175232Z 0 [Note] InnoDB: Using CPU crc32 instructions
2022-10-26T09:46:22.176898Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2022-10-26T09:46:22.227774Z 0 [Note] InnoDB: Completed initialization of buffer pool
2022-10-26T09:46:22.244214Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2022-10-26T09:46:22.278665Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2022-10-26T09:46:22.278955Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2022-10-26T09:46:22.290362Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2022-10-26T09:46:22.296979Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2022-10-26T09:46:22.297003Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2022-10-26T09:46:22.298246Z 0 [Note] InnoDB: Waiting for purge to start
2022-10-26T09:46:22.352425Z 0 [Note] InnoDB: 5.7.40 started; log sequence number 2755482
2022-10-26T09:46:22.352751Z 0 [Note] InnoDB: Loading buffer pool(s) from /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ib_buffer_pool
2022-10-26T09:46:22.353131Z 0 [Note] Plugin 'FEDERATED' is disabled.
2022-10-26T09:46:22.362113Z 0 [Note] InnoDB: Buffer pool(s) load completed at 221026 17:46:22
2022-10-26T09:46:22.377191Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2022-10-26T09:46:22.377222Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2022-10-26T09:46:22.377240Z 0 [Warning]
2022-10-26T09:46:22.377245Z 0 [Warning]
2022-10-26T09:46:22.377946Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-26T09:46:22.378030Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-10-26T09:46:22.378307Z 0 [Note] Server hostname (bind-address): '*'; port: 3307
2022-10-26T09:46:22.378684Z 0 [Note] IPv6 is available.
2022-10-26T09:46:22.378728Z 0 [Note]   - '::' resolves to '::';
2022-10-26T09:46:22.378836Z 0 [Note] Server socket created on IP: '::'.
2022-10-26T09:46:22.419539Z 0 [Note] Event Scheduler: Loaded 0 events
2022-10-26T09:46:22.419884Z 0 [Note]

运行成功,进程正在监听外部请求

步骤七:单步调试

找到文件sql_executor.cc,在do_select方法内部打一个断点

使用命令行,连接mysql
输入命令:show databases;

➜  mysql -uroot -P3307 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40-debug Source distributionCopyright (c) 2000, 2021, Oracle and/or its affiliates.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> show databases;

Clion这边应该有反应,断点进入

接下来就可以Debug + 单步调试了

MySQL5.7源码在MacOS+Clion上的单步调试相关推荐

  1. mysql5.7.21源码解析,史上最强Mysql5.7源码安装讲解

    今天给小伙伴们介绍一下Mysql5.7源码安装,在安装过程中还是碰到了一些问题,下面介绍安装步骤时,会告诉大家问题在哪儿,怎么解决的,废话不多说,下面就可以说说安装全过程 1.创建用户 groupad ...

  2. 51ak带你看MYSQL5.7源码2:编译现有的代码

    从事DBA工作多年 MYSQL源码也是头一次接触 尝试记录下自己看MYSQL5.7源码的历程 申明:个人Python编程很溜,但是C++还停在白痴水平,源码理解方面有点弱,如发现有错误的地方,轻喷 目 ...

  3. mysql 黑名单_51ak带你看MYSQL5.7源码4:实现SQL黑名单功能

    博客迁移至: 从事DBA工作多年 MYSQL源码也是头一次接触 尝试记录下自己看MYSQL5.7源码的历程 申明:个人Python编程很溜,但是C++还停在白痴水平,源码理解方面有点弱,如发现有错误的 ...

  4. 基于java线上教学平台计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署

    基于java线上教学平台计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署 基于java线上教学平台计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署 本源码技术栈: 项目架构 ...

  5. HBase源码分析之HRegion上compact流程分析(三)

    在<HBase源码分析之HRegion上compact流程分析(二)>一文中,我们没有讲解真正执行合并的CompactionContext的compact()方法.现在我们来分析下它的具体 ...

  6. 节后大礼包!XSql 源码开放,插件源码开放,Sofire v1.6 源码开放(已上传)

    随着上一篇随笔 SOFIRE v1.5 概念版-- X-SQL 已过去近两个月.经内部不完全测试,Sofire Suite 迎来了v1.6的 beta 版.此次,将会开放源码以及测试程序,以及专用于 ...

  7. 小程序源码:最新掌上题库微信小程序源码下载,修复登录接口,支持在线考试,自定义导入考题-多玩法安装简单

    这是一款题库微信小程序源码 支持积分商城.自定义试题及导入.知识点分类.模式试题考试.流量主等 首页模块:专项刷题 .题型刷题.乱序刷题.我的收藏.我的错题.未作习题.刷知识点.考前必背 另外还有更多 ...

  8. 看PX4的源码可以去Github上看

    看PX4的源码可以去Github上看 之前一直以为PX4没有keil工程,然后什么开发环境搭建复杂,因为不搭建好开发环境就看不了源码,实际不是这样的 主要是由这里跳转发现的,跳转到PX4的mavlin ...

  9. jQuery图片批量上传插件源码,支持批量上传、预览、删除、放大,可配置上传数量、上传大小、追加方式,含详细使用文档

    jQuery图片批量上传插件源码,支持批量上传.预览.删除.放大,可配置上传数量.上传大小.追加方式,含详细使用文档 程序包内含使用Demo 完整程序源代码:jQuery图片批量上传插件源码 上传前 ...

  10. 多商户商城源码,搭建线上商城,满足用户多种需求

    电子商务是伴随信息经济而发展起来的,20世纪90年代兴起于美国.欧洲等发达国家,我们可以将其理解为在开放的互联网环境下,运用服务器实现消费者的网上购物.企业间商业交易等,大家常说的B2B(企业与企业间 ...

最新文章

  1. 二元关系在计算机中的应用(数据库为主),数据库技术复习题答案 选择题
  2. R语言数据横向合并cbind函数实战
  3. mysql保持开启事件调度器_MySQL 5.1.6开始的事件调度器
  4. 震惊!原来现在无所不能的世界主人翁,早已在三千年前就悄然出现!
  5. sql,dateadd,datediff
  6. wdatepicker不展示秒_十一出游去哪里?苏州人都不一定知道的地方!
  7. 【MATLAB】三维曲线(plot3)
  8. 现任明教教主 NAC Framework EOU 视频
  9. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_2 Mybatis的CRUD-保存操作
  10. 比较好用的Java模拟器,515最好的java模拟器
  11. BXP无盘XP2000系统安装详解
  12. sql注入 java_JAVA实现sql注入点检测
  13. MongoDB 文档操作之插入、查询
  14. 写一篇靠谱的方案设计文档-之实践问题清单
  15. iOS修复字符串中 有\n 不换行的问题
  16. 拾色器 插件 Farbtastic
  17. UVa 10934 Dropping water balloons:dp(递推)
  18. 华为国产系统Android,国产手机系统即将出现!华为将抛弃安卓:成功研发自有手机系统...
  19. 若有下列共用体定义_若有下列共用体定义:
  20. 拼装机器人感想_拼装机器人 感受未来科技

热门文章

  1. tomcat-CNVD-2020-10487-漏洞处理
  2. 如何设置QQ空间免费背景音乐
  3. C++ 批量图片拼接
  4. 我来告诉你,草根程序员如何进入BAT
  5. AntD Pro v5记录-布局
  6. 学妹面试拼刀刀被问Java策略模式是什么鬼?哈哈哈哈
  7. mac M1安装SVN错误Error: Command failed with exit 128: git
  8. 数字游戏-SSL 1653
  9. 安全产品的部署方式及位置
  10. hexo入门学习(四):文章(带图片)