一条SQL语句是如何执行的?count(*),count(1)有什么不同?insert数据时为什么有时候会导致自增主键不连续?等等这一系列问题,可能我们在网上的各种文章上能看到具体的讲解,但是很少有文章能分析到源码层级,都是直接告诉我们结果是什么。深入理解这些问题的实现过程就需要我们自己去看MySQL的源码来找寻答案。
网上搜寻了一些调试源码的文档,发现不全面,所以结合自身配置过程,形成一篇教程以供参考。
一、准备工作
1、MacBook笔记本;[有homebrew]
2、创建MySQL安装目录和MySQL data目录
#mysql安装目录
mkdir -p /Users/tal/data0/dev_mysql/build_out
#mysql data目录
mkdir-p /Users/tal/data0/dev_mysql/build_out/data
3、下载MySQL源码包;
#这里选择5.7.30版本: [https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.30.tar.gz]
#进入下载目录
cd/Users/tal/data0/dev_mysql
#下载源码包
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.30.tar.gz
#解压源码包
tar-zxvf mysql-5.7.30.tar.gz
4、cmake环境;
brew install cmake
这里额外说一下homebrew配置国内源以提高效率:
# 替换 Homebrew
git-C “$(brew –repo)” remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 替换 Homebrew Core
git-C “$(brew –repo homebrew/core)” remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 替换 Homebrew Cask
git-C “$(brew –repo homebrew/cask)” remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 替换 Homebrew-bottles
# 对于 bash 用户:
echo’export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles’ >> ~/.bash_profile
source~/.bash_profile
# 对于 zsh 用户:
echo’export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles’ >> ~/.zshrc
source~/.zshrc
二、编译安装MySQL
#进入mysql解压目录
cd/Users/tal/data0/dev_mysql/mysql-5.7.30#执行cmake 这里用sudo
sudo cmake .-DWITH_DEBUG=1\-DCMAKE_INSTALL_PREFIX=/Users/tal/data0/dev_mysql/build_out \-DMYSQL_DATADIR=/Users/tal/data0/dev_mysql/build_out/data
若遇到以下报错:
CMake Error at cmake/boost.cmake:88(MESSAGE):
You can download it with-DDOWNLOAD_BOOST=1 -DWITH_BOOST=This CMake script will lookfor boost in . If it isnot there,
it will download and unpack it (in that directory) foryou.
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:547(INCLUDE)– Configuring incomplete, errors occurred!See also”/Users/tal/data0/dev_mysql/mysql-5.7.30/CMakeFiles/CMakeOutput.log”.
则说明缺少boost,我们参照提示增加cmake参数:
#首先创建boost目录
sudo mkdir-p /usr/local/boost
#然后
#增加DDOWNLOAD_BOOST=1自动下载boost
#DWITH_BOOST=/usr/local/boost 指定boost目录
sudo cmake .-DWITH_DEBUG=1\-DDOWNLOAD_BOOST=1\-DWITH_BOOST=/usr/local/boost \-DCMAKE_INSTALL_PREFIX=/Users/tal/data0/dev_mysql/build_out \-DMYSQL_DATADIR=/Users/tal/data0/dev_mysql/build_out/data
如果网络不好的话,估计会超时,下载不下来boost,会提示:
CMake Error at cmake/boost.cmake:194(MESSAGE):
You cantrydownloading
http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
manually using curl/wget or a similar tool, or increase the value of
DOWNLOAD_BOOST_TIMEOUT (whichis now 600seconds)
Call Stack (most recent call first):
CMakeLists.txt:547(INCLUDE)– Configuring incomplete, errors occurred!
如果这样的话,那我们wget单独下载boost:
#进入boost目录
cd/usr/local/boost
#wget
sudo wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
#解压
sudo tar-xvzf boost_1_59_0.tar.gz
然后继续执行之前的cmake
sudo cmake . -DWITH_DEBUG=1\-DDOWNLOAD_BOOST=1\-DWITH_BOOST=/usr/local/boost \-DCMAKE_INSTALL_PREFIX=/Users/tal/data0/dev_mysql/build_out \-DMYSQL_DATADIR=/Users/tal/data0/dev_mysql/build_out/data
最好将整个dev_mysql目录的权限更改为当前用户,不要root。且sudo设置为不输入密码。
以上,就编译好了。接下来开始配置Clion:
将/Users/tal/data0/dev_mysql/mysql-5.7.30的MySQL源码引入。
然后进入Clion的配置型:将之前cmake编译的参数放到CMake options中
填写完之后Apply ok后,返回代码界面:
在命令里周到mysqld,然后点击修改配置[Edit Configura…],对命令参数进行配置:
对mysqld命令追加参数: –defaults-file=/Users/tal/data0/dev_mysql/build_out/dev_my.cnf
dev_my.cnf参考内容:
[mysqld]
log-error=/tmp/dev_mysql_log.err
basedir=/Users/tal/data0/dev_mysql/build_out/datadir=/Users/tal/data0/dev_mysql/build_out/data/pid-file=user.pid
skip-grant-tables
innodb_file_per_table=1port=33060# transaction_isolation=READ-COMMITTED
[client]
# 客户端来源数据的默认字符集default-character-set=utf8mb4
[mysqld]
# 服务端默认字符集
character-set-server=utf8mb4
# 连接层默认字符集
collation-server=utf8mb4_unicode_ci
socket=/Users/tal/data0/dev_mysql/build_out/mysql.sock
[mysql]
# 数据库默认字符集default-character-set=utf8mb4
socket=/Users/tal/data0/dev_mysql/build_out/mysql.sock
之后就可以进行debug了:点击debug启动mysql,第一次编译会比较慢,耐心等待下。
启动之后如下:
看下端口,是否成功启动了:
lsof -i:33060COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld39231 tal 13u IPv6 0xb98aab9dd3abf795 0t0 TCP *:33060 (LISTEN)
然后我们连接客户端试试:
cd /Users/tal/data0/dev_mysql/build_out
./bin/mysql -uroot –port=33060 –socket=/Users/tal/data0/dev_mysql/build_out/mysql.sock
连接成功后就可以进行断点调试了。