前言
日常工作中MySQL虽然基本使用没有太多问题,但对于底层原理一直不太清楚,对于本人来说一直是个黑盒,本文尝试编译下MySQL源码,为以后研究MySQL底层打下基础
一、官网下载源码
源码地址:
MySQL :: Download MySQL Community Server (Archived Versions)
这里我们之间下载一个带Boost库的源码
上传文件并解压
二、Clion导入项目
1.导入MySql源码
clion导入mysql源码前,先在源码目录下创建 build 和 build/data 两个目录
mkdir -p /opt/mysql-8.0.32/build/data
2.配置cmake编译参数
三、编译
cmake参数配置好后就开始编译了,编译过程中会有报错,基本是缺失相关依赖,这里先做个记录
Could not find devtoolset compiler/linker in /opt/rh/gcc-toolset-11 | yum install gcc-toolset-11-gcc gcc-toolset-11-gcc-c++ gcc-toolset-11-binutils |
Please install the appropriate openssl developer package. | yum install openssl-devel |
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel. | yum install ncurses-devel |
Could not find rpc/rpc.h in /usr/include or /usr/include/tirpc | yum install -y libtirpc libtirpc-devel |
CMake Warning at cmake/rpc.cmake:29 (MESSAGE): Cannot find rpcgen executable. You need to install the required packages: Debian/Ubuntu: apt install rpcsvc-proto RedHat/Fedora/Oracle Linux: yum install rpcgen SuSE: zypper install glibc-devel | https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.3/rpcsvc-proto-1.4.3.tar.xz tar -xvf rpcsvc-proto-1.4.3.tar.xz cd rpcsvc-proto-1.4.3 ./configure make make install |
下面是具体的报错信息,相信大家安装的时候可能也会遇到,这个给的详细点
错误1
CMake Warning (dev) at CMakeLists.txt:211 (EXEC_PROGRAM):
Policy CMP0153 is not set: The exec_program command should not be called.
Run "cmake --help-policy CMP0153" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
Use execute_process() instead.
This warning is for project developers. Use -Wno-dev to suppress it.
-- This is .el8. as found from 'rpm -qf /'
-- Looking for a devtoolset compiler
CMake Warning at CMakeLists.txt:392 (MESSAGE):
Could not find devtoolset compiler/linker in /opt/rh/gcc-toolset-11
CMake Warning at CMakeLists.txt:394 (MESSAGE):
You need to install the required packages:
yum install gcc-toolset-11-gcc gcc-toolset-11-gcc-c++ gcc-toolset-11-binutils
CMake Error at CMakeLists.txt:396 (MESSAGE):
Or you can set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER explicitly.
按照提示安装缺失依赖
yum install gcc-toolset-11-gcc gcc-toolset-11-gcc-c++ gcc-toolset-11-binutils
错误2
安装缺失依赖后继续编译碰到下一个错误
CMake Error at cmake/ssl.cmake:83 (MESSAGE):
Please install the appropriate openssl developer package.
按照提示安装缺失依赖
yum install openssl-devel
错误3
继续编译碰到下一个问题
CMake Error at cmake/readline.cmake:92 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
安装提示安装缺失依赖
yum install ncurses-devel
错误4
继续编译碰到下一个问题
CMake Warning at cmake/rpc.cmake:40 (MESSAGE):
Cannot find RPC development libraries. You need to install the required
packages:
Debian/Ubuntu: apt install libtirpc-dev
RedHat/Fedora/Oracle Linux: yum install libtirpc-devel
SuSE: zypper install glibc-devel
Call Stack (most recent call first):
cmake/rpc.cmake:87 (WARN_MISSING_SYSTEM_TIRPC)
plugin/group_replication/libmysqlgcs/cmake/configure.cmake:34 (MYSQL_CHECK_RPC)
plugin/group_replication/libmysqlgcs/CMakeLists.txt:34 (INCLUDE)
CMake Error at cmake/rpc.cmake:88 (MESSAGE):
Could not find rpc/rpc.h in /usr/include or /usr/include/tirpc
按照提示安装缺失依赖
yum install libtirpc-devel
错误5
继续编译碰到下一个问题
CMake Warning at cmake/rpc.cmake:29 (MESSAGE):
Cannot find rpcgen executable. You need to install the required packages:
Debian/Ubuntu: apt install rpcsvc-proto
RedHat/Fedora/Oracle Linux: yum install rpcgen
SuSE: zypper install glibc-devel
Call Stack (most recent call first):
plugin/group_replication/libmysqlgcs/cmake/rpcgen.cmake:112 (WARN_MISSING_RPCGEN_EXECUTABLE)
plugin/group_replication/libmysqlgcs/CMakeLists.txt:53 (INCLUDE)
CMake Error at plugin/group_replication/libmysqlgcs/cmake/rpcgen.cmake:113 (MESSAGE):
Could not find rpcgen
下载缺失依赖(切到其它路径下安装。)
https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.3/rpcsvc-proto-1.4.3.tar.xz
tar -xvf rpcsvc-proto-1.4.3.tar.xz
cd rpcsvc-proto-1.4.3
./configure
make
make install
三、启动项目
编译完成后在Clion上完成MySQL的启动过程
1、初始化数据库
找到mysqlId 配置参数 先初始化数据库
--initialize --user=mysql
所以最好提前创建好mysql用户
groupadd mysql;
useradd -r -g mysql -s /bin/false mysql;
等待一段时间,初始化成功,这个root密码要记下
2、启动MySQL后台进程
初始化成功后,再在机器上找下看看有没有MySQL进程,发现是没有的,刚才的操作只是初始化了数据库,接下来 我们启动下后台MySql进程,还是配置mysqlId (这里我们用debug模式启动下)
--basedir=/opt/mysql-8.0.32/build --datadir=/opt/mysql-8.0.32/build/data --user=mysql
这样说明数据库启动成功了
3、启动客户端进程
配置MySQL
-uroot -p
编译结束后,我们再在服务器上看下bin目录,看到了属性的mysql 命令
mysql启动后要重新设置root用户密码,还有为了客户端工具能连接上也要做些设置,这里通过mysql命令一起操作下
当然还要检查下防火墙
接下来我们用客户端工具连接看下
成功连上数据库
四、调试源码
安装mysq的方式有很多种,为啥要通过这种方式安装呢,当然是想要了解mysql的实现细节啦(前面启动mysql后台进程的时候采用了debug模式启动)
这样通过跟踪源码就能加深自己对mysql的理解以及底层运行机制了