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 或分支。

从官网下载 MySQL 5.7.35 boost 版本源码

Patch 源码

如果 MySQL <= 8.0.21,则需要执行以下脚本 Patch 源码:

1
2
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

CMake options 配置如下:

1
2
3
4
5
6
7
8
9
-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

解释下上面的参数:

1
2
3
4
5
6
7
8
9
-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

手动创建配置中的目录:

1
2
# 为何加`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

Reset Cache and Reload Project

执行完后,CMake 窗口输出如下内容表示执行成功,否则即失败:

1
2
3
4
5
6
/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

首次编译会比较慢,编译完成后输出如下内容:

1
2
3
4
5
6
7
====================[ 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 文件,并编辑:

1
2
[mysqld]
innodb_file_per_table = 1

初始化 mysqld

点击 CLion 窗口右上角 Edit Configurations...

Edit Configurations

找到 mysqld,并配置 Program arguments

mysqld-program-arguments

Program arguments 配置内容:

1
--initialize-insecure

点击 CLion 窗口右上角 Run 图标或快捷键 ⌃+R,进行初始化:

运行 Debug

初始化后,输出如下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/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:

开始Debug

通过已有客户端连接到 MySQL 服务器,比如:

1
mysql -uroot -P3307 -h127.0.0.1

也可以通过 Navicat 连接: Navicat连接

然后在客户端输入一条 SQL,CLion Debug 窗口就会提示:

clion-debug

常见问题

Warning 级别的错误,可忽略

CMake Error at cmake/boost.cmake

错误描述

此错误在执行 cmake 时会报错

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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 或源码文件名区分
1
2
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 的源码安装

1
cmake . -DWITH_BOOST=boost

不带 boost 的源码安装

1
2
3
cmake . \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=<directory>

这是 MySQL 建议的构建方式。CMake 会到 <directory> 下查找符合版本要求的 boost,如果不存在则会下载并解压到该目录下。

执行构建后的输出示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
...
-- 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 目录:

1
2
3
4
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>

如果下载很慢,推荐提前手动下载 boost

Warning: define bzero please_use_memset_rather_than_bzero

在构建过程中,可能会报如下 Warning

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[ 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 时的提示:

1
2
3
4
5
/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 引入的文件中定义过了:

1
2
3
/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

Cannot find appropriate system libraries for WITH_SSL=system

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Cannot find appropriate system libraries for WITH_SSL=system.
Make sure you have specified a supported SSL version. 
Valid options are : 
system (use the OS openssl library), 
yes (synonym for system), 
</path/to/custom/openssl/installation>

CMake Error at cmake/ssl.cmake:63 (MESSAGE):
  Please install the appropriate openssl developer package.

Call Stack (most recent call first):
  cmake/ssl.cmake:280 (FATAL_SSL_NOT_FOUND_ERROR)
  CMakeLists.txt:568 (MYSQL_CHECK_SSL)

CMake 没找到 openssl 开发包。系统可能自带了 openssl,但却仅有软件,而没有开发包。故需要手动补上,本文使用 Brew 快速安装。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 先检查是否已安装
// 为啥先检查?因为可能装其他软件的时候已经安装上此依赖了
brew list | grep openssl

// 如果没安装,则先安装(已安装则跳过此步)
brew install openssl@1.1

// 强行链接到 brew 安装的 openssl
brew link openssl@1.1 --force

// 再根据提示,配置环境变量
echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

编译 mysql 等工具

此前,我们只编译了 mysqld 服务器这一个软件。但我们需要 mysql 客户端,或 mysqldump 等其他工具时,也可以选择配置其他项,并点击 Build 就可以。

build-mysql

参考资料