# Linux 搜索順序 (後蓋前)
/etc/my.cnf
/etc/mysql/my.cnf
~/.my.cnf
# Windows
my.ini (安裝目錄) / my.cnf (數據目錄)
# 查看實際生效值
mysqld --verbose --help 2>/dev/null | grep -A1 '^datadir'
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
| 參數 | 建議值 | 說明 |
|---|---|---|
server_id | 唯一整數 (如 101, 102) | 復制/集群必備,1..2^32-1 |
port | 3306 | 防火牆放行 |
bind_address | 0.0.0.0 (或具體內網 IP) | 0.0.0.0 監聽所有網卡 |
max_connections | 2000-5000 | 配合 thread_cache_size、連接池;監控 Threads_connected / Max_used_connections |
max_connect_errors | 100000 | 防誤封禁,配合 FLUSH HOSTS |
skip_name_resolve | ON | **關鍵**:禁用 DNS 反解,避免連接延遲/卡頓,權限表只用 IP |
local_infile | OFF | 安全:禁用 LOAD DATA LOCAL (除非業務必需) |
sql_mode | STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | **生產必開嚴格模式**,防靜默截斷/零日期/除零錯誤 |
default_authentication_plugin | caching_sha2_password | 8.0 默認,更安全;兼容舊驅動可改 mysql_native_password |
| 參數 | 建議值 | 說明 |
|---|---|---|
innodb_buffer_pool_size | 物理內存 60-80% (如 48G) | **最關鍵參數**;數據+索引+鎖+自適應哈希+插入緩衝;專用服務器 70-80% |
innodb_buffer_pool_instances | 8-16 (≤ 8.0 自動) | 減少鎖爭用,每實例 ≥ 1GB |
innodb_log_file_size | 2G-4G (總 innodb_log_files_in_group * size ≤ Buffer Pool) | 大 Redo Log 減少 Checkpoint 壓力、提升寫入;8.0 動態調整 |
innodb_log_files_in_group | 2-4 | 循環寫入組數 |
innodb_flush_log_at_trx_commit | 1 (金融/核心) / 2 (高寫入可容忍秒級丟失) | 1=每提交 fsync,ACID;2=寫 OS 緩存,每秒 fsync,性能↑ 風險↑ |
innodb_flush_method | O_DIRECT / O_DIRECT_NO_FSYNC (8.0.14+) | 避免雙重緩存 (OS + InnoDB);NVMe 建議 O_DIRECT_NO_FSYNC |
innodb_file_per_table | ON | 獨立表空間,支持 DROP/TRUNCATE 釋放空間、TSP、分層存儲 |
innodb_page_size | 16384 (默認) | 編譯時固定,勿改 |
innodb_read_io_threads / innodb_write_io_threads | 8-16 | NVMe/高併發可調大,配合 innodb_io_capacity |
innodb_io_capacity / innodb_io_capacity_max | 2000 / 5000 (SSD) / 10000+ (NVMe) | 告知 InnoDB 磁盤能力,控制刷頁/合併/清理速度 |
innodb_lru_scan_depth | 1024-4096 | 頁清理掃描深度,大 Buffer Pool 可調大 |
| 參數 | 建議值 | 說明 |
|---|---|---|
query_cache_type / query_cache_size | 0 / 0 | **8.0 已移除**,舊版關閉避免鎖爭用 |
sort_buffer_size | 2M-4M (會話級) | 排序緩衝,**慎調大** (OOM 風險),大排序落盤臨時表 |
read_buffer_size / read_rnd_buffer_size | 2M / 4M | 順序/隨機讀緩衝 |
join_buffer_size | 2M-8M (會話級) | 無索引 Join 時用,**優先加索引**;BNL/BKA 算法受益 |
tmp_table_size / max_heap_table_size | 64M-256M | 內存臨時表上限,超過轉 MyISAM 磁盤表;**盡量避免臨時表** |
internal_tmp_mem_storage_engine | TempTable (8.0 默認) | 內存臨時表引擎,動態分配、支援 VARCHAR/BLOB,優於 MEMORY |
| 參數 | 建議值 | 說明 |
|---|---|---|
log_bin | binlog | 啟用 Binlog,復制/時間點恢復/審計必備 |
binlog_format | ROW | **行級**,最安全、支持混合事務、復制一致性最好 |
binlog_expire_logs_seconds | 604800 (7天) | 自動清理,防磁盤爆滿;8.0 替代 expire_logs_days |
sync_binlog | 1 | 每提交 fsync Binlog,配合 innodb_flush_log_at_trx_commit=1 雙 1 保證不丟數據 |
gtid_mode / enforce_gtid_consistency | ON / ON | **GTID 模式**,簡化故障切換、拓撲變更、多源復制 |
binlog_checksum | CRC32 | Binlog 校驗和,防損壞 |
master_verify_checksum / slave_sql_verify_checksum | ON / ON | 主庫寫入/從庫應用時校驗 |
character_set_server = utf8mb4, collation_server = utf8mb4_0900_ai_ci (8.0 默認)。explicit_defaults_for_timestamp = ON (8.0 默認),TIMESTAMP 無隱式 NOT NULL/DEFAULT。lower_case_table_names = 1 (Windows/macOS 強制),Linux 建議 0 (區分大小寫),**集群必須一致**。max_allowed_packet = 1G (大字段/長 SQL/備份導入)。net_read_timeout = 60, net_write_timeout = 60 (防長查詢斷連)。slow_query_log = ON, long_query_time = 0.5 (秒), slow_query_log_file = /var/log/mysql/slow.log, log_queries_not_using_indexes = ON (謹慎,量大)。log_error = /var/log/mysql/error.log, log_error_verbosity = 2 (8.0)。SET GLOBAL ... 驗證效果,確認後寫入配置文件。innodb_io_capacity, flush_method;大內存必調 buffer_pool, log_file_size。