mysql故障分析——關閉binlog

最近開始維護一個運行多年的私有化項目。
項目使用docker部署,mysql、應用等都在一個鏡像中,mysql數據掛載在物理機上。mysql屬於單機,開啓了binlog,因爲數據比較重要,經年未做清理,導致binlog文件過大直接爆盤。我們做了以下步驟關閉binlog:

# 進入容器
vi /etc/mysql/my.cnf

# 將以下2行註釋,關閉binlog
# log-bin=mysql-bin
# binlog_format=mixed
# 刪除binlog日誌文件
rm -rf /data/mysql/mysql-bin.*

在關閉binlog後,重啓容器,發現mysql無法啓動,查看日誌顯示

Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (111 'connection refused')

這時,我們將/run/mysqld/mysqld.sock文件刪除,mysqld還是無法啓動。我們將現有容器刪除,重新部署容器,因爲這時的鏡像是未關閉binlog的,所以無法正常啓動,並且會顯示以下錯誤

Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)

然後進入容器,將binlog再次關閉,這時發現mysql未成功啓動原因改變了

[ERROR] Found 1 prepared transactions! It means that mysqld was not shut down properly last time and critical recovery information (last binlog or tc.log file) was manually deleted after a crash. You have to start mysqld with --tc-heuristic-recover switch to commit or rollback pending transactions.

這個問題原因是,我們在關閉binlog時,並沒有關閉mysql進程,導致關閉binlog的過程中還有事物進行,解決辦法如下:

# 先通過  --tc-heuristic-recover=ROLLBACK 將事物回滾
/usr/bin/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib/mysql/plugin --user=root --log-error=/data/mysql/mysqld.err --pid-file=/data/mysql/mysql.pid --socket=/run/mysqld/mysqld.sock --port=3306 --tc-heuristic-recover=ROLLBACK

# 在正常啓動mysql
/usr/bin/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib/mysql/plugin --user=root --log-error=/data/mysql/mysqld.err --pid-file=/data/mysql/mysql.pid --socket=/run/mysqld/mysqld.sock --port=3306

最後,在關閉binlog,刪除binlog日誌文件時,一定要記得先關閉進程。或者直接使用mysql命令 reset master; 直接清除binlog

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章