CentOS 6.5 編譯安裝 MySQL 5.6.16 後,登錄瞬間崩潰:
[root@test mysql-5.6.16]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor...
段錯誤 (Segmentation fault)
英文環境顯示 Segmentation fault。這是 MySQL 5.6.16 版本已知 Bug,影響使用 libedit 的命令行客戶端。
Bug 位於 cmd-line-utils/libedit/terminal.c 的 terminal_set() 函數中。局部數組 char buf[TC_BUFSIZE] 在棧上分配,但在某些終端環境下指針操作導致棧溢出或野指針訪問。
官方 Bug 報告:Launchpad #1201123
find /usr/local/src/mysql-5.6.16 -name "terminal.c"
# 通常在:cmd-line-utils/libedit/terminal.c
vim cmd-line-utils/libedit/terminal.c
# 找到第 865-879 行左右的 terminal_set 函數
# 原代碼:
865 protected int
866 terminal_set(EditLine *el, const char *term)
867 {
868 int i;
869 char buf[TC_BUFSIZE]; // 問題行:棧上大數組
870 char *area;
871 const struct termcapstr *t;
872 sigset_t oset, nset;
873 int lins, cols;
874
875 (void) sigemptyset(&nset);
876 (void) sigaddset(&nset, SIGWINCH);
877 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
878
879 area = buf; // 後續操作 area 指針
# 修改後:
865 protected int
866 terminal_set(EditLine *el, const char *term)
867 {
868 int i;
869 /* char buf[TC_BUFSIZE]; */ -- 註釋掉棧數組
870 char *area;
871 char *buf = NULL; -- 新增:動態分配
872 const struct termcapstr *t;
873 sigset_t oset, nset;
874 int lins, cols;
875
876 (void) sigemptyset(&nset);
877 (void) sigaddset(&nset, SIGWINCH);
878 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
879
880 buf = (char *)malloc(TC_BUFSIZE); -- 堆上分配
881 if (buf == NULL) return -1; -- 內存不足保護
882 area = buf; -- 使用動態內存
關鍵點:將棧上固定大小數組改為堆上動態分配,並在函數結束前 free(buf)(原代碼結束處已有釋放邏輯)。
cd /usr/local/src/mysql-5.6.16
make clean
make -j$(nproc)
make install
# 重啟服務驗證
systemctl restart mysqld
mysql -u root -p # 應不再崩潰
官方在 5.6.17 中已修復此 Bug,建議直接升級版本。
cmake . -DWITH_READLINE=1 -DWITH_LIBEDIT=0 ...
需系統安裝 readline-devel。
mysql --no-auto-rehash -u root -p
# 禁用自動補全功能,可繞過 libedit 初始化
# 1. 基本登錄
mysql -u root -p -e "SELECT VERSION();"
# 2. 交互式操作測試
mysql -u root -p
mysql> SHOW DATABASES;
mysql> USE test;
mysql> SELECT 1;
mysql> \q
# 3. 壓測連接
for i in {1..50}; do mysql -u root -p'pass' -e "SELECT $i;" & done; wait