ตัวอย่าง my.cnf ที่ “ปลอดภัย + เหมาะกับงาน web/app”
สมมติ Server:
- Ubuntu 22.04
- RAM 8 GB
- MySQL / MariaDB 10.x–11.x
- งาน Web + C# / PHP
[mysqld]
# ===============================
# BASIC
# ===============================
user = mysql
port = 3306
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
skip-name-resolve
lower_case_table_names = 1
default-storage-engine = InnoDB
# ===============================
# CHARACTER SET (คงของเดิมไว้ก่อน)
# ===============================
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-character-set-client-handshake
init_connect = 'SET NAMES utf8mb4'
# ===============================
# CONNECTION (สำคัญมาก)
# ===============================
max_connections = 800
thread_cache_size = 256
table_open_cache = 8000
# ===============================
# MEMORY & INNODB
# ===============================
innodb_buffer_pool_size = 45G
innodb_buffer_pool_instances = 8
innodb_log_file_size = 2G
innodb_log_buffer_size = 256M
innodb_flush_method = O_DIRECT
innodb_flush_log_at_trx_commit = 2
innodb_thread_concurrency = 0
innodb_read_io_threads = 16
innodb_write_io_threads = 16
# ===============================
# TEMP & SORT
# ===============================
tmp_table_size = 512M
max_heap_table_size = 512M
sort_buffer_size = 4M
join_buffer_size = 4M
# ===============================
# QUERY CACHE (ปิด!)
# ===============================
query_cache_type = 0
query_cache_size = 0
# ===============================
# LOGGING (จำเป็น)
# ===============================
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 3
log_queries_not_using_indexes = 0
log_error = /var/log/mysql/error.log
# ===============================
# BINLOG / REPLICATION
# ===============================
server-id = 1
log-bin = mysql-bin
binlog_format = ROW
expire_logs_days = 10
max_binlog_size = 1G
sync_binlog = 0
🔹 ถ้า RAM 16GB → เพิ่ม innodb_buffer_pool_size เป็น 10–12GB ได้
| ค่า | ผล |
|---|---|
innodb_buffer_pool_size | index อยู่ใน RAM มากขึ้น |
innodb_buffer_pool_instances | ลด contention |
sort_buffer_size | เร็วขึ้นตอน ORDER BY |
join_buffer_size | JOIN แบบไม่มี index ดีขึ้นเล็กน้อย |