手记

在CentOS 上 调试 MySQL-5.7.x 或者 MySQL-8.0 源码

在CentOS 上 调试 MySQL-5.7.x 或者 MySQL-8.0 源码

1.依赖包安装

yum install gdb gcc gcc-c++ ncurses-devel cmake libaio bison zlib-devel openssl openssl-devel patch

2.下载mysql源码包和boost库

  • 下载最新mysql源码
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
  • 下载boost库,版本 boost_1_59_0
http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.bz2

mysql-8.0需要boost_1_63_0版本

3.上传并解压

上传并解压安装包:

[root@localhost MySQL]# tar -xzvf mysql-boost-5.7.18.tar.gz
[root@localhost MySQL]# tar -xzvf boost_1_59_0.tar.bz2

4 .建立mysql用户和组,建立相关目录

/usr/sbin/groupadd mysql
/usr/sbin/useradd mysql -g mysql -d /home/mysql -s /sbin/nologin
mkdir /home/mysql/data/3306/data
mkdir /home/mysql/data/3306/log
mkdir /home/mysql/data/3306/tmp
mkdir /home/mysql/data/3306/binlog

创建mysql安装目录

mkdir /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /home/mysql

5. 编译MySQL源码&安装mysql

生成makefile文件

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/home/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_EDITLINE=bundled \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_DEBUG=1  \ 
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/xxx/xxx/boost_1_59_0 -DDOWNLOAD_BOOST_TIMEOUT=60000 

编译和安装

make -j4 && make install 

根据机器配置等待一段时间,看到100%字样,则说明编译成功

6.配置mysql环境变量

在/etc/profile中添加/usr/local/mysql/bin到PATH变量中

PATH=/usr/local/mysql/bin:$PATH

source /etc/profile 

7.初始化mysql数据库

初始化data文件

mysqld --defaults-file=/home/mysql/data/3306/my.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data/3306/data

切换到mysql用户

su - mysql

生成ssl所需文件

mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data/3306/data

#启动mysql

mysqld --defaults-file=/home/mysql/data/3306/my.cnf --user=mysql 

#先通过unix socket修改mysql root用户密码

mysql -uroot -p -S /home/mysql/data/3306/mysql.sock

UPDATE mysql.user SET authentication_string = PASSWORD('mysql') WHERE USER LIKE '%root%';

grant all privileges on *.* to 'root'@'%' identified by 'mysql' with grant option;

flush privileges;

#验证通过tcp/ip协议登陆mysql

mysql -h127.0.0.1 -uroot -p -P3306

8.配置数据库配置文件

cd /usr/local/mysql/
cp support-files/my-default.cnf  /etc/my.cnf

vi /etc/my.cnf

[client]
socket=/home/mysql/mysql.sock
[mysqld]
symbolic-links=0
server_id          = 120
default_storage_engine   = Innodb
user            = mysql
port            = 3306
basedir           = /usr/local/mysql
datadir           = /home/mysql/data
tmpdir           = /home/mysql/tmp
socket           = /home/mysql/mysql.sock
pid_file          = /home/mysql/mysql.pid
character_set_server    = utf8
collation_server      = utf8_general_ci
open_files_limit      = 10240
explicit_defaults_for_timestamp
max_allowed_packet     = 256M
max_heap_table_size     = 256M
net_buffer_length      = 8K
sort_buffer_size      = 2M
join_buffer_size      = 4M
read_buffer_size      = 2M
read_rnd_buffer_size    = 16M
log_error          = /home/mysql/log/error.log
log_bin           = /home/mysql/binlog/mysql-bin
binlog_cache_size      = 32M
max_binlog_cache_size    = 2G
max_binlog_size       = 500M
binlog_format        = mixed
log_output         = FILE
expire_logs_days      = 5
slow_query_log       = 1
long_query_time       = 3
slow_query_log_file     = /home/mysql/log/slow-query.log
innodb_buffer_pool_size   = 500M
innodb_log_file_size    = 200M
innodb_log_files_in_group  = 3
innodb_lock_wait_timeout  = 20
[mysql]
no_auto_rehash
[mysqld_safe]
log-error=/home/mysql/log/error.log

9. 启动mysql

mysqld_safe --defaults-file=/home/mysql/data/3306/my.cnf --user=mysql &

10. 调试mysql

查看 mysql进程 pid号

ps -ef | grep mysqld
mysql   23917 23221 15 10:27 pts/1   00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/home/mysql/data/3306/my.cnf --basedir=/usr/local/mysql --datadir=/home/mysql/data/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/home/mysql/data/3306/log/error.log --open-files-limit=10240 --pid-file=/home/mysql/data/3306/mysql.pid --socket=/home/mysql/data/3306/mysq.sock --port=3306

# 调试
gdb --tui --pid 23917 --上面查看到的mysqld进程号

进入gdb调试窗口,设置断点

(gdb) b	do_command
Breakpoint 1 at	0x995820: file /root/work_dir/mysql-8.0.1-dmr/sql/sql_parse.cc,	line 1066.
(gdb) 

在 gdb调试窗口中输入c

Breakpoint 1 at	0x995820: file /root/work_dir/mysql-8.0.1-dmr/sql/sql_parse.cc,	line 1066.
(gdb) c
Continuing.
[New Thread 0x7fb67d4f8700 (LWP 24769)]
[New Thread 0x7fb67d4b7700 (LWP 24770)]
[New Thread 0x7fb67d476700 (LWP 24771)]

启动另外一个shell终端,启动mysql连接上面的mysqld服务

[root@zagnix ~]# mysql -uroot -pmysql -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.

---等待

在gdb调试窗口执行n或者s,就可以执行单步调试了

通过si查看mysql源码,b function_name 设置断点即可

0人推荐
随时随地看视频
慕课网APP