# The MariaDB configuration file

#

# The MariaDB/MySQL tools read configuration files in the following order:

# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.

# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,

# 2. "/etc/mysql/conf.d/*.cnf" to set global options.

# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.

# 4. "~/.my.cnf" to set user-specific options.

#

# If the same option is defined multiple times, the last one will apply.

#

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/


#

# This group is read both by the client and the server

# use it for options that affect everything

#

[client-server]

# Port or socket location where to connect

# port = 3306

socket = /run/mysqld/mysqld.sock


# Import all .cnf files from configuration directory

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mariadb.conf.d/


[mysqld]

# -------------- 메모리 관련 최적화 --------------

innodb_buffer_pool_size = 20G       # 전체 RAM의 30~35% (InnoDB 데이터 캐싱)

innodb_log_file_size = 256M         # InnoDB 로그 파일 크기

innodb_log_buffer_size = 64M        # InnoDB 로그 버퍼 크기

innodb_flush_log_at_trx_commit = 1  # 트랜잭션 안정성 확보


# -------------- 연결 및 쿼리 성능 최적화 --------------

max_connections = 300               # 동시 접속자 수 제한

thread_cache_size = 200             # 스레드 캐시 최적화

table_open_cache = 8000             # 테이블 캐시 개수 증가

query_cache_type = 0                # Query Cache는 비활성화 (MariaDB에서는 비효율적)


# -------------- 임시 테이블 및 정렬 최적화 --------------

tmp_table_size = 256M

max_heap_table_size = 256M

sort_buffer_size = 4M

join_buffer_size = 8M


# -------------- 기타 성능 최적화 --------------

key_buffer_size = 1024M              # MyISAM 테이블이 있다면 적절한 값 설정

read_buffer_size = 8M

read_rnd_buffer_size = 16M


# my.cnf 파일에 추가

slow_query_log = 1

slow_query_log_file = /var/log/mysql/mariadb-slow.log

long_query_time = 1  # 1초 이상 걸리는 쿼리 기록


# 성능 모니터링 활성화

performance_schema = ON


# InnoDB 최적화 추가 설정

innodb_buffer_pool_instances = 8  # buffer_pool 분할 (병렬 접근 개선)

innodb_read_io_threads = 8

innodb_write_io_threads = 8

innodb_purge_threads = 4

innodb_adaptive_hash_index = ON


# 테이블 통계 최적화

innodb_stats_on_metadata = 0


# 동시성 관련 설정

innodb_lock_wait_timeout = 50

innodb_deadlock_detect = ON


# 슬로우 쿼리 로그 추가 설정

log_queries_not_using_indexes = 1

log_slow_admin_statements = 1

log_slow_verbosity = full # query_plan,explain 에서 변경함


# 시스템 변수 추가

# 시스템 변수 최적화

interactive_timeout = 600

wait_timeout = 600

max_allowed_packet = 64M