在CentOS 上 調試 MySQL-5.7.x 或者 MySQL-8.0 源碼
1.依賴包安裝
<code>yum install gdb gcc gcc-c++ ncurses-devel cmake libaio bison zlib-devel openssl openssl-devel patch/<code>
2.下載mysql源碼包和boost庫
- 下載最新mysql源碼
<code>https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz/<code>
- 下載boost庫,版本 boost_1_59_0
<code>http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.bz2/<code>
mysql-8.0需要boost_1_63_0版本
3.上傳並解壓
上傳並解壓安裝包:
<code>[root@localhost MySQL]# tar -xzvf mysql-boost-5.7.18.tar.gz
[root@localhost MySQL]# tar -xzvf boost_1_59_0.tar.bz2/<code>
4 .建立mysql用戶和組,建立相關目錄
<code>/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/<code>
創建mysql安裝目錄
<code>mkdir /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /home/mysql/<code>
5. 編譯MySQL源碼&安裝mysql
生成makefile文件
<code>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 /<code>
編譯和安裝
<code>make -j4 && make install /<code>
根據機器配置等待一段時間,看到100%字樣,則說明編譯成功
6.配置mysql環境變量
在/etc/profile中添加/usr/local/mysql/bin到PATH變量中
<code>PATH=/usr/local/mysql/bin:$PATH
source /etc/profile /<code>
7.初始化mysql數據庫
初始化data文件
<code>mysqld --defaults-file=/home/mysql/data/3306/my.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data/3306/data/<code>
切換到mysql用戶
<code>su - mysql/<code>
生成ssl所需文件
<code>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/<code>
8.配置數據庫配置文件
<code>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/<code>
9. 啟動mysql
<code>mysqld_safe --defaults-file=/home/mysql/data/3306/my.cnf --user=mysql &/<code>
10. 調試mysql
查看 mysql進程 pid號
<code>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進程號/<code>
進入gdb調試窗口,設置斷點
<code>(gdb) b do_command
Breakpoint 1 at 0x995820: file /root/work_dir/mysql-8.0.1-dmr/sql/sql_parse.cc, line 1066.
(gdb) /<code>
在 gdb調試窗口中輸入c
<code>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)]/<code>
啟動另外一個shell終端,啟動mysql連接上面的mysqld服務
<code>[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.
---等待/<code>
在gdb調試窗口執行n或者s,就可以執行單步調試了
通過si查看mysql源碼,b function_name 設置斷點即可
閱讀更多 zagnix 的文章