[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
# 基礎網絡
bind-address = 192.168.0.11
port = 3306
socket = /tmp/mysql.sock
skip-locking
skip-external-locking
skip-slave-start
# 連接控制
max_connections = 200
max_connect_errors = 500
back_log = 50
# 通信緩衝
max_allowed_packet = 32M
net_buffer_length = 8K
# 默認引擎與字符集
default-storage-engine = InnoDB
default-table-type = innodb
default-character-set = utf8
default-collation = utf8_bin
character_set_server = utf8
collation_server = utf8_bin
init-connect = 'SET NAMES utf8'
# 查詢緩存(5.7+ 已廢棄,8.0 移除)
query_cache_size = 64M
query_cache_limit = 2M
# 表緩存與線程
table_cache = 2048
thread_cache_size = 8
thread_concurrency = 8
thread_stack = 192K
# 排序/聯接緩衝(按需調大,慎防 OOM)
sort_buffer_size = 5M
join_buffer_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
# 臨時表
tmp_table_size = 64M
max_heap_table_size = 64M # 配套設置
# 事務隔離
transaction_isolation = REPEATABLE-READ
# 慢查詢日志
log_slow_queries
long_query_time = 2
log_long_format
# 主從複製
server-id = 1
log-bin = mysql-bin
binlog_cache_size = 5M
binlog-ignore-db = information_schema
binlog-ignore-db = mysql
binlog-ignore-db = cluster
replicate-do-db = test1
replicate-do-db = test2
replicate-wild-do-table = test1.%
replicate-wild-do-table = test2.%
log-slave-updates
# ========== InnoDB 核心參數 ==========
# 數據/日志目錄(獨立磁盤)
innodb_data_home_dir = /data/dbdata/
innodb_log_group_home_dir = /data/dbdata/
# 表空間文件(預創建 10×2GB + autoextend)
innodb_data_file_path = ibdata1:2000M;ibdata2:2000M;...;ibdata10:2000M:autoextend
# 緩衝池(物理內存 50-80%,32 位系統受 2GB 進程限制)
innodb_buffer_pool_size = 1G
innodb_additional_mem_pool_size = 16M
# 並發與 I/O
innodb_thread_concurrency = 16 # 2×(CPU核數+磁盤數)
innodb_file_io_threads = 4
innodb_file_per_table = 1 # 獨立表空間,便於管理
# 日志參數
innodb_log_file_size = 256M # 25% buffer pool
innodb_log_files_in_group = 3
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 0 # 0=最高性能,1=最安全,2=折中
sync_binlog = 15 # 非 1 犧牲一致性換吞吐
# 鎖與事務
innodb_lock_wait_timeout = 30
innodb_support_xa = OFF # 關閉分布式事務提升性能
# 表鎖優化
innodb_table_locks = 0
# 自動擴容
innodb_autoextend_increment = 256M
# ========== 客戶端工具 ==========
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
| 參數 | 建議值 | 調優依據 |
|---|---|---|
innodb_buffer_pool_size | 物理內存 60-70% | 核心緩存,數據+索引+鎖+自適應哈希,越大命中率越高 |
innodb_log_file_size | 25% buffer pool | 大日志減少 checkpoint 刷盤頻率,但啟動恢復慢 |
innodb_flush_log_at_trx_commit | 0/2(高吞吐) 1(金融級安全) | 0: 秒級丟失 1s 數據;2: 僅 OS crash 丟數據 |
innodb_thread_concurrency | 2×(CPU核+磁盤) | 控制並發線程進入 InnoDB 內核,防止上下文切換抖動 |
innodb_file_per_table | ON | 獨立 .ibd 文件,支持 DROP TABLE 回收空間、單表備份、表空間遷移 |
query_cache_size | 0(5.7+ 建議關閉) | 高併發下鎖爭用嚴重,8.0 已移除 |
max_connections | 應用峰值連接 + 余量 | 配合連接池,避免「Too many connections」 |
tmp_table_size/max_heap_table_size | 64-256M | 防止內部臨時表落盤(Using temporary) |
innodb_buffer_pool_size + key_buffer + max_connections × (sort_buffer + read_buffer + binlog_cache) + thread_stack × max_connections 總和**不可超過 2GB**,否則 glibc 內存分配失敗導致 mysqld 崩潰。innodb_data_home_dir、innodb_log_group_home_dir 目錄,授權 mysql:mysql。vm.swappiness=1。log_slow_queries、long_query_time=1、log_queries_not_using_indexes。FLUSH LOGS 切割 binlog,配合 expire_logs_days=7。