共计 7969 个字符,预计需要花费 20 分钟才能阅读完成。
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
手动创立配置中的目录:
# 为何加 `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 mysqld
Build 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=<directory>
This CMake script will look for boost in <directory>. 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:80
Call Stack (most recent call first):
cmake/boost.cmake:245 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:536 (INCLUDE)
编译 MySQL 源码,CMake 须要配置 -DWITH_BOOST=<directory>
选项。
确认源码是否携带 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=<directory>
这是 MySQL 倡议的构建形式。CMake 会到 <directory>
下查找合乎版本要求的 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=<directory>
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/