Tối ưu MariaDB trên Ubuntu 24.04

Mặc định database của bạn khi cài vào sẽ không hỗ trợ lưu cache các truy vấn từ database nên điều đó có thể sẽ làm website bạn hơi chậm.

Tối ưu MariaDB trên Ubuntu 24.04

Tối ưu MariaDB

Mở tập tin /etc/mysql/mariadb.cnf và đặt nội dung dưới vào cuối:

[mysqld]
# Cơ bản
basedir                         = /usr
bind_address                    = 127.0.0.1
pid_file                        = /run/mysqld/mysqld.pid
port                            = 3306
socket                          = /run/mysqld/mysqld.sock
tmpdir                          = /tmp

# Query cache (chỉ nên dùng với MySQL < 5.7, không khuyến khích nếu bạn dùng InnoDB nhiều)
query_cache_type                = 0
query_cache_size                = 0

# Kết nối
connect_timeout                 = 60

# InnoDB tối ưu
innodb_buffer_pool_size         = 128M
innodb_log_file_size            = 64M
innodb_log_buffer_size          = 8M

innodb_flush_log_at_trx_commit  = 2
innodb_flush_neighbors			= 0

# Log và các giá trị khác
low_priority_updates            = 1
disable_log_bin                 = 1
slow_query_log                  = 1
slow_query_log_file             = /var/lib/mysql/mysql_slow.log
long_query_time                 = 1
log_error                       = /var/lib/mysql/mysql_error.log

Hoặc nếu máy chủ bạn có dư dả tài nguyên, có thể tăng các giá trị lên, ví dụ máy chủ 8 CPU và 16 GB RAM:

[mysqld]
# Cơ bản
basedir                         = /usr
bind_address                    = 127.0.0.1
port                            = 3306
socket                          = /run/mysqld/mysqld.sock
pid_file                        = /run/mysqld/mysqld.pid
tmpdir                          = /tmp

# InnoDB tối ưu
innodb_buffer_pool_size         = 8G
innodb_buffer_pool_instances    = 8
innodb_log_file_size            = 1G
innodb_log_buffer_size          = 64M
innodb_flush_log_at_trx_commit  = 2
innodb_flush_neighbors          = 0
innodb_io_capacity              = 1000
innodb_io_capacity_max          = 2000
innodb_read_io_threads          = 8
innodb_write_io_threads         = 8
innodb_file_per_table           = 1
innodb_stats_on_metadata        = 0

# Query cache (chỉ nên dùng với MySQL < 5.7, không khuyến khích nếu bạn dùng InnoDB nhiều)
query_cache_type                = 0
query_cache_size                = 0

# Timeout & kết nối
connect_timeout                 = 10
wait_timeout                   = 180
interactive_timeout            = 180
max_connections                = 200

# Log
slow_query_log                 = 1
slow_query_log_file            = /var/lib/mysql/mysql_slow.log
long_query_time                = 1
log_error                      = /var/lib/mysql/mysql_error.log

# Khác
tmp_table_size                 = 128M
max_heap_table_size            = 128M
table_open_cache               = 4096
table_definition_cache         = 4096
open_files_limit               = 65535
max_allowed_packet             = 256M
sort_buffer_size               = 2M
read_buffer_size               = 2M
read_rnd_buffer_size           = 4M
join_buffer_size               = 4M

# Binary log (nên bật nếu cần replicate hoặc backup binary log theo thời gian)
# Nếu không dùng thì có thể tắt như bạn đã làm
skip_log_bin

Nhớ khởi động lại MariaDB:

systemctl restart mariadb

Chúc các bạn thành công!

Bình Luận


  • Không có bình luận.