Seting Mysql Server Tuning Performa Tidak mati / crash

Seting Mysql Server Tuning Performa Tidak mati / crash

Database MySQL sangat populer digunakan sebagai database server karena kestabilan, fleksibilitas, dan dukungan komunitas yang luas. Namun, jika konfigurasi MySQL Tuning tidak tepat, dampaknya bisa serius. Server bisa sering down, crash, atau mengalami overhead yang berat, sehingga mengganggu layanan yang bergantung pada database.

Kesalahan konfigurasi juga bisa membuat RAM cepat penuh, bahkan ketika jumlah user atau query belum terlalu banyak. Hal ini sering terjadi jika buffer pool, per-connection buffers, atau jumlah koneksi maksimal diatur terlalu tinggi tanpa menyesuaikan kapasitas VPS. Akibatnya, MySQL mungkin tidak hanya mati, tetapi performanya juga menjadi sangat lambat, meskipun spesifikasi server sudah memadai.

Contoh kasus umum meliputi: penggunaan parameter deprecated atau tidak didukung di MySQL 8, setelan thread_handling yang salah, buffer per-connection terlalu besar, atau log InnoDB yang tidak optimal. Oleh karena itu, optimasi konfigurasi MySQL sesuai kapasitas RAM dan core CPU sangat penting agar database berjalan stabil, cepat, dan efisien.

Tunging Mysql CPU 3 core dan RAM 4 GB ~ OS Linux

Saat ini admin fokus untuk seting optimasi performa anti down namun query tetap stabil khususnya untuk engine innodb dan mysql server versi 8++.

Seting Mysql Server

Untuk server linux, ubuntu buka ssh :

#Backup
sudo mv /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf.backup.$(date +%F_%H-%M-%S)
# Tulis baru
nano /etc/mysql/mysql.conf.d/mysqld.cnf
# seting permision dan grup akses
sudo chown root:mysql /etc/mysql/mysql.conf.d/mysqld.cnf
sudo chmod 640 /etc/mysql/mysql.conf.d/mysqld.cnf

Konfigurasi Mysql server ringan + Stabil RAM 4 + CPU 3 Core Versi minimal RAM kecil tp bisa untuk +250 koneksi query simultan:

[mysqld]
user = mysql
bind-address = 127.0.0.1
default-time-zone = "+07:00"
skip-name-resolve

character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci

# ==================================================
# CONNECTIONS (STABIL > BESAR)
# ==================================================
max_connections = 150
wait_timeout = 600
interactive_timeout = 600
thread_cache_size = 128

# ==================================================
# INNODB CORE (ยฑ60% RAM)
# ==================================================
innodb_buffer_pool_size = 2G
innodb_buffer_pool_instances = 2

innodb_log_file_size = 256M
innodb_log_buffer_size = 32M
innodb_redo_log_capacity = 512M

innodb_flush_log_at_trx_commit = 2
innodb_flush_log_at_timeout = 2
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_flush_neighbors = 0

# ==================================================
# INNODB I/O (3 CORE)
# ==================================================
innodb_io_capacity = 400
innodb_io_capacity_max = 800
innodb_read_io_threads = 3
innodb_write_io_threads = 3

# ==================================================
# TEMP & MEMORY (AMAN)
# ==================================================
tmp_table_size = 32M
max_heap_table_size = 32M

sort_buffer_size = 256K
join_buffer_size = 256K
read_buffer_size = 256K
read_rnd_buffer_size = 256K

# ==================================================
# TABLE & PACKET
# ==================================================
table_open_cache = 2048
table_definition_cache = 2048
# handel upload big files
max_allowed_packet = 256M

# ==================================================
# LOGGING (MINIMAL)
# ==================================================
log_error = /var/log/mysql/error.log
log_error_verbosity = 2

slow_query_log = 0
general_log = 0
skip-log-bin

# ==================================================
# SQL MODE
# ==================================================
sql_mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

# ==================================================
# PERFORMANCE
# ==================================================
performance_schema = OFF

# ==================================================
# TMPDIR & SECURITY
# ==================================================
tmpdir = /tmp

Save + Restart mysql:

sudo systemctl restart mysql
sudo systemctl status mysql

NOTE: ini konfigurasi MySQL 8 yang ringan dan stabil untuk server 4 GB RAM. Di bawah ini saya jelaskan arti detail setiap bagian, fungsi tiap parameter, dampaknya, dan rekomendasi tambahan kecil agar sistemmu optimal, aman, dan efisien.

Wajib Analisa penggunaan RAM setelah 3-5 Jam aktif pada jam sibuk:

ps -C mysqld -o pid,rss | awk 'NR>1 {sum+=$2} END {print "MySQL RAM:", sum/1024, "MB"}'

Keterangan Setingan SQL

Parameter, termasuk fungsi, efek, dan rekomendasi. Aku akan jelaskan dengan detail, supaya kamu paham alasan setiap setting.

๐ŸŒ Basic Server

user = mysql
bind-address = 127.0.0.1
default-time-zone = "+07:00"
skip-name-resolve

Penjelasan

  • user=mysql โ†’ MySQL jalan pakai user mysql (aman)

  • bind-address=127.0.0.1 โ†’ hanya bisa diakses lokal

  • default-time-zone โ†’ konsisten waktu DB

  • skip-name-resolve โ†’ lebih cepat koneksi, MySQL tidak lookup DNS


๐Ÿ”Œ CONNECTIONS

max_connections = 250
wait_timeout = 180
interactive_timeout = 180
thread_cache_size = 64

Penjelasan

  • max_connections โ†’ batas koneksi bersamaan
    (150 aman untuk RAM 4GB)

  • wait_timeout โ†’ koneksi idle ditutup setelah 180 detik

  • interactive_timeout โ†’ timeout session interaktif

  • thread_cache_size โ†’ simpan thread agar koneksi baru lebih cepat


๐Ÿ—„๏ธ INNODB CORE

innodb_buffer_pool_size = 1G
innodb_buffer_pool_instances = 1

Penjelasan

  • Buffer pool = RAM cache utama tabel & index

  • 1G โ‰ˆ 25% RAM โ†’ aman & stabil

  • Instance 1 cocok untuk buffer < 1โ€“2GB


innodb_log_file_size = 128M
innodb_log_buffer_size = 8M
innodb_redo_log_capacity = 64M

Penjelasan

  • Redo log โ†’ catatan transaksi

  • Ukuran kecil โ†’ hemat disk & RAM

  • Cukup untuk web workload


innodb_flush_log_at_trx_commit = 2
innodb_flush_log_at_timeout = 2

Penjelasan

  • Data di-flush tiap 2 detik

  • Lebih cepat daripada mode aman (1)

  • Risiko kehilangan ยฑ1โ€“2 detik data saat crash


innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_flush_neighbors = 0

Penjelasan

  • O_DIRECT โ†’ MySQL langsung ke disk (hindari double cache)

  • file_per_table โ†’ tiap tabel file sendiri

  • flush_neighbors=0 โ†’ optimal SSD


โš™๏ธ INNODB I/O

innodb_io_capacity = 300
innodb_io_capacity_max = 600
innodb_read_io_threads = 3
innodb_write_io_threads = 3

Penjelasan

  • Sesuaikan dengan SSD + 3 CPU

  • Terlalu besar โ†’ CPU overload

  • Terlalu kecil โ†’ disk tidak maksimal


๐Ÿงช TEMP & MEMORY

tmp_table_size = 8M
max_heap_table_size = 8M

Penjelasan

  • Batasi ukuran tabel sementara di RAM

  • Mencegah RAM jebol saat query berat


๐Ÿง  PER CONNECTION BUFFER

sort_buffer_size = 64K
join_buffer_size = 64K
read_buffer_size = 64K
read_rnd_buffer_size = 64K

Penjelasan

  • Dialokasikan per koneksi

  • Nilai kecil โ†’ aman untuk banyak koneksi

  • Nilai besar โ†’ bahaya OOM


๐Ÿ“ฆ TABLE & PACKET

table_open_cache = 800
max_allowed_packet = 32M

Penjelasan

  • Cache tabel terbuka โ†’ query lebih cepat

  • max_allowed_packet โ†’ upload / query besar


๐Ÿ“ LOGGING (MINIMAL)

log_error = /var/log/mysql/error.log
log_error_verbosity = 2
slow_query_log = 0
general_log = 0
skip-log-bin

Penjelasan

  • Error log wajib (tapi minim)

  • Slow & general log dimatikan

  • Binlog mati โ†’ hemat disk & I/O


๐Ÿงพ SQL MODE

sql_mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

Penjelasan

  • Validasi data ketat

  • Cegah silent error

  • Aman untuk aplikasi modern


โšก PERFORMANCE

performance_schema = OFF

Penjelasan

  • Hemat ยฑ300MB RAM

  • Cocok server kecil


๐Ÿ” SECURITY & TEMP

tmpdir = /tmp
secure-file-priv = /var/lib/mysql-files

Penjelasan

  • Direktori sementara

  • Batasi akses file (keamanan)


โœ… RINGKASAN AKHIR

โœ” Ringan
โœ” Stabil
โœ” Aman untuk RAM 4GB
โœ” Cocok high traffic web
โœ” Tidak simpan log besar


FAQ Umum ?

Q1: Bisa handle 500 query simultan dengan RAM 4GB?

Tidak langsung, karena max_connections 250. Gunakan connection pooling di aplikasi โ†’ bisa mensimulasikan 500 query simultan tapi hanya 250 koneksi aktif di MySQL.

Q2: Apakah InnoDB buffer pool 1GB cukup?

Cukup untuk database aktif <1GB. Kalau data aktif lebih besar โ†’ pertimbangkan 1-2GB jika RAM tersedia.

Q3: Apa efek innodb_flush_log_at_trx_commit = 2?

  • Lebih cepat write-heavy workload, tapi ada risiko data hilang saat crash.

  • Jika durability penting โ†’ gunakan 1.

Q4: Harus aktifkan performance_schema?

  • Nonaktif (0) โ†’ hemat RAM.

  • Aktifkan hanya untuk profiling query atau debugging.

Q5: Apa pengaruh thread pool?

thread_handling = pool-of-threads โ†’ sangat membantu handling banyak koneksi. Tanpa ini, 250 koneksi = 250 thread โ†’ lebih banyak CPU context switch dan RAM overhead.