某知名遊戲公司(完美時空)生產環境 MySQL 配置實例:8GB 記憶體、InnoDB 主力、高併發遊戲負載。本文逐項解析關鍵參數設定邏輯,供同規模架構參考。
[client]
port = 3306
socket = /home/mysql/mysql.sock
[mysqld]
port = 3306
socket = /home/mysql/mysql.sock
datadir = /home/mysql/var
server-id = 1
log-bin = /data/mysqllog/binlog/mysql-bin
skip-name-resolve
default-character-set = utf8
back_log = 50
max_connections = 1000
max_connect_errors = 32
max_allowed_packet = 16M
table_cache = 2048
binlog_cache_size = 1M
max_heap_table_size = 64M
tmp_table_size = 64M
key_buffer_size = 32M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
thread_concurrency = 8
thread_stack = 192K
query_cache_size = 64M
query_cache_limit = 2M
tmp_table_size = 64M
ft_min_word_len = 2
default_table_type = INNODB
transaction_isolation = REPEATABLE-READ
log_slow_queries
long_query_time = 2
log_long_format
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2G
innodb_data_file_path = ibdata1:2048M;ibdata2:2048M;ibdata3:2048M;ibdata4:2048M;ibdata5:2048M;ibdata6:1024M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[myisamchk]
key_buffer = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
| 參數 | 設定值 | 解析 |
|---|---|---|
innodb_buffer_pool_size | 2G | 8GB 記憶體分配 25% 給 InnoDB。遊戲讀寫混合,熱點資料集較小(活躍玩家、當前場景),2G 足夠覆蓋核心熱點。剩餘記憶體留給 OS 快取、連線緩衝、排序、其他進程。 |
innodb_data_file_path | 6 個檔案共 11.25GB + autoextend | 預分配大空間避免動態擴展卡頓,autoextend 允許突發增長。分散多檔案利用多磁碟並行 I/O。 |
innodb_flush_log_at_trx_commit | 1 | 遊戲金幣/道具/交易涉及金錢,數據安全優先。每提交刷盤,犧牲部分寫入效能換取零資料遺失。 |
innodb_log_file_size | 256M × 3 群組 | 總 768MB Redo Log。大 Log 減少 Checkpoint 頻率、平滑寫入尖峰。3 群組輪轉寫入。 |
innodb_thread_concurrency | 16 | 限制 InnoDB 內部並發執行緒,防止過度上下文切換。8 核心 CPU 設 2x。 |
innodb_max_dirty_pages_pct | 90 | 允許 90% 髒頁,延遲刷盤、合併隨機寫入為循序,提升寫入吞吐。風險:崩潰恢復時間較長。 |
table_cache | 2048 | 遊戲表數較多(分表、日誌表),大表快取減少開檔開銷。 |
max_connections | 1000 | 支援高併發玩家連線。配合連線池使用,實際活躍連線通常遠低於此值。 |
query_cache_size | 64M | 遊戲有大量重複查詢(排行榜、配置表),查詢快取有效。但需監控鎖爭用。 |
log_slow_queries + long_query_time=2 | 啟用 | 記錄超過 2 秒查詢,定期分析優化。 |
ft_min_word_len=2 | 2 | 支援雙字以上中文全文搜尋(如玩家搜尋物品名)。 |
open-files-limit=8192 | 8192 | 大量表檔案、Binlog、InnoDB 多資料檔需大量檔案描述符。 |
/data/mysqllog/binlog/ 獨立掛載點,避免 Binlog 寫入干擾資料檔 I/O。flush_log_at_trx_commit=1 確保遊戲經濟系統資料零遺失。default_table_type → default_storage_enginetable_cache → table_open_cachelog_slow_queries → slow_query_log=1, slow_query_log_file=...query_cache_*:MySQL 8.0 已移除,建議關閉(query_cache_type=0, query_cache_size=0),改用 Redisinnodb_file_io_threads、innodb_thread_concurrency:新版自適應更好,建議設 0 或移除innodb_additional_mem_pool_size:5.7+ 已移除innodb_flush_method=O_DIRECT、innodb_file_per_table=1、innodb_io_capacity=2000(SSD)