MySQL 核心運維五大場景速覽
場景一:Binlog 爆滿導致磁碟空間不足、服務啟動失敗
安全清理 SOP (嚴禁 rm 直接刪除檔案)
- 檢查從庫同步位置:
SHOW SLAVE STATUS\G 記錄 Master_Log_File
- 主庫 PURGE 至從庫最舊位置之前:
PURGE BINARY LOGS TO 'mysql-bin.000123';
-- 或按時間
PURGE BINARY LOGS BEFORE '2024-01-15 00:00:00';
- 配置自動過期:
binlog_expire_logs_seconds = 604800 (7天,8.0+)
- 長期規劃:Binlog 專用磁碟分區、啟用壓縮
binlog_transaction_compression=ON
場景二:SELECT 進階查詢技巧
核心子句組合
SELECT
u.id, u.name,
COUNT(o.id) AS order_cnt,
SUM(o.amount) AS total_amount
FROM `user` u
LEFT JOIN `order` o ON u.id = o.user_id AND o.status = 'PAID'
WHERE u.status = 1
AND u.created_at >= '2024-01-01'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
ORDER BY total_amount DESC
LIMIT 20;
關鍵技巧
WHERE 過濾行、HAVING 過濾分組
LIMIT offset, rows 分頁,**大偏移用游標法**
ORDER BY RAND() 禁用,**隨機採樣用 ID 範圍法**
GROUP_CONCAT 行轉列:SELECT GROUP_CONCAT(tag SEPARATOR ',') FROM tags WHERE user_id=1
場景三:本地登錄失敗 (ERROR 1045/1044) 修復
根因:mysql.user 表缺少 root@localhost 記錄
-- 1. 跳過權限表啟動
systemctl stop mysqld
mysqld_safe --skip-grant-tables --skip-networking &
-- 2. 無密碼登入修復
mysql -u root
USE mysql;
-- 方案 A:更新現有 host 為 localhost
UPDATE user SET Host='localhost' WHERE User='root' AND Host='%';
-- 方案 B:插入新記錄 (推薦)
INSERT INTO user (Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv, Create_view_priv, Show_view_priv, Create_routine_priv, Alter_routine_priv, Create_user_priv, Event_priv, Trigger_priv, Create_tablespace_priv, ssl_type, ssl_cipher, x509_issuer, x509_subject, max_questions, max_updates, max_connections, max_user_connections, plugin, authentication_string, password_expired, password_last_changed, password_lifetime, account_locked)
VALUES ('localhost', 'root', '', 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','', '','',0,0,0,0,'mysql_native_password','', 'N', NOW(), NULL, 'N');
FLUSH PRIVILEGES;
-- 3. 正常重啟
mysqladmin -u root shutdown
systemctl start mysqld
場景四:Root 密碼遺忘/重置/清空全平台方案
Linux/Unix
systemctl stop mysqld
mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
USE mysql;
UPDATE user SET authentication_string=PASSWORD('NewPass#2024') WHERE User='root';
FLUSH PRIVILEGES;
mysqladmin -u root shutdown
systemctl start mysqld
Windows
net stop mysql
mysqld --console --skip-grant-tables --skip-networking
# 新開 CMD
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('NewPass#2024') WHERE User='root';
FLUSH PRIVILEGES;
# 關閉 mysqld 控制台窗口
net start mysql
場景五:數據安全防護體系
| 防護層 | 措施 | 實施命令/配置 |
| 賬號 | 移除匿名用戶、禁用 root 遠程、強密碼策略 | DELETE FROM mysql.user WHERE User=''; DELETE FROM mysql.user WHERE User='root' AND Host!='localhost'; |
| 網路 | 禁用 TCP/IP 或綁定 127.0.0.1、防火牆白名單 | my.cnf: bind-address=127.0.0.1 / skip-networking |
| 傳輸加密 | SSL/TLS 加密連線 | require_secure_transport=ON、配置 SSL 憑證 |
| 審計 | 啟用 General Log / Audit Plugin | general_log=1、MariaDB Audit Plugin / MySQL Enterprise Audit |
| 備份加密 | 備份文件加密存儲 | mysqldump | gzip | openssl enc -aes-256-cbc |