一份經過深思熟慮的 my.cnf 是 MySQL 高效穩定運行的前提。本文基於 MySQL 5.5.13 實戰配置模板,逐項拆解核心參數的含義、關聯依賴與調優建議,幫助 DBA 建立標準化、可維護的配置基準線。
[client]
character-set-server = utf8
port = 3306
socket = /data/mysql/3306/mysql.sock
[mysqld]
character-set-server = utf8
user = mysql
port = 3306
socket = /data/mysql/3306/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /data/mysql/3306/data
log-error = /data/mysql/3306/mysql_error.log
pid-file = /data/mysql/3306/mysql.pid
utf8(注意:5.5 建議用 utf8mb4 完整支援 Emoji)。open_files_limit = 10240
table_cache = 512
table_cache 需配合 max_connections 估算:建議 ≥ max_connections × N(N 為單查詢最大聯表數)。open_files_limit 須大於 table_cache,預留臨時表與日誌描述符。4GB 記憶體建議 8192+,此處設 10240 較保守。back_log = 600
max_connections = 5000
max_connect_errors = 6000
back_log:TCP 連接請求隊列長度,高併發突發場景建議 500~1000,受限於 OS somaxconn。max_connections:5000 較大,需確保 thread_cache_size、記憶體足夠支撐,否則易觸發 OOM。max_allowed_packet = 32M
sort_buffer_size = 8M
join_buffer_size = 1M
max_connections 同時活躍度制約,但務必壓測驗證。sort_buffer_size > 2KB 觸發 mmap() 分配,效率下降;無 ORDER BY/GROUP BY 場景可降至 256K~1M。thread_cache_size = 300
thread_concurrency = 8
thread_cache_size:對比 Connections 與 Threads_created,命中率 < 90% 時增大。300 適合高頻短連場景。thread_concurrency:舊參數(5.5+ 已棄用),建議設為 CPU 邏輯核心數 × 2,現代內核調度器已足夠智能,可忽略。query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
Qcache_free_blocks / Qcache_total_blocks > 20%)時,鎖競爭嚴重,反拖慢寫入。query_cache_size=0 禁用。監控命中率 (Qcache_hits-Qcache_inserts)/Qcache_hits 低於 20% 即應縮容或關閉。
key_buffer_size = 512M
read_buffer_size = 4M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
key_buffer_size:MyISAM 索引核心,佔物理記憶體 25%~40%,詳見文章 73735/73738。read_rnd_buffer_size:排序查詢隨機讀緩衝,Session 級,大數據排序可適度調大。myisam_recover:啟動自動檢測修復損壞表,生產環境強烈建議開啟。
innodb_data_home_dir = /data/mysql/3306/data
innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
| 參數 | 建議值/策略 | 說明 |
|---|---|---|
innodb_buffer_pool_size | 物理記憶體 70%~80%(純 InnoDB) | 數據+索引統一緩存,核心效能引擎。512M 偏小,64GB 機器建議 40GB+。 |
innodb_flush_log_at_trx_commit | 1(強一致) / 2(高效能) | 1:每提交刷盤,零數據丟失;2:寫入 OS cache,每秒刷盤,效能提升但崩潰丟 1 秒數據。 |
innodb_log_file_size | 512M~2G | 大日誌減少檢查點頻率,提升寫入;但崩潰恢復時間變長。建議總日誌 log_file_size × log_files_in_group 達 Buffer Pool 25%~50%。 |
innodb_file_per_table | 1(獨立表空間) | 0 為共享表空間,難以回收空間、遷移表。生產強烈建議開啟(=1)。 |
innodb_max_dirty_pages_pct | 75~90 | 關閉前可動態降至 10 加速脏頁刷新,縮短關機時間。 |
log-bin = /data/mysql/3306/binlog/binlog
binlog_format = MIXED
binlog_cache_size = 4M
max_binlog_size = 1G
expire_logs_days = 30
replicate-ignore-db = mysql,test,information_schema
log-slave-updates
skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
binlog_format=MIXED:兼顧 SBR 效能與 RBR 一致性,推薦預設。skip-name-resolve:跳過 DNS 反解,消除連接延遲,需確保授權表用 IP 或 %。slave-skip-errors:謹慎使用,僅忽略已確認無業務影響的錯誤碼(如主鍵衝突 1062)。
slow_query_log
long_query_time = 1
long_query_time = 0.5~1,配合 log_queries_not_using_indexes 發現全表掃描。
[mysqldump]
quick
max_allowed_packet = 32M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
mysqldump --quick:流式導出,避免大結果集佔用記憶體。myisamchk 參數僅供離線維修使用,線上勿用。mysql -t -e "SHOW VARIABLES LIKE 'innodb_%'" 確認生效。配置優化非一勞永逸,隨著業務增長、數據量變化、MySQL 版本升級,參數需持續迭代。本文提供的模板與解析旨在建立「可解釋、可度量、可演進」的配置管理體系,而非提供萬金油數值。願每位 DBA 都能在參數迷霧中,找到屬於業務的最優解。