3.0 KiB
Understanding the Basics
With a 32 GB RAM server, your primary goal is to maximize the use of available memory to reduce disk I/O, which is the slowest part of database operations. The main setting to focus on is the size of the InnoDB buffer pool.
Open your MySQL configuration file, which is typically my.cnf. Common locations include:
/etc/mysql/my.cnf/etc/my.cnf/etc/mysql/mysql.conf.d/mysqld.cnf
Always make a backup of the original file before making any changes.
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak
sudo nano /etc/mysql/my.cnf
Key Settings for 32 GB RAM
Add or adjust the following lines under the [mysqld] section of the configuration file. These settings are optimized for a dedicated database server. If your server hosts other resource-intensive applications, you should slightly decrease these values.
1. InnoDB Buffer Pool Size
The InnoDB buffer pool is the most critical setting. It's the cache for your data and indexes. A larger buffer pool means MySQL can keep more of your active dataset in fast RAM, drastically improving performance.
For a 32 GB server, allocating about 70-80% of the total RAM is ideal. This leaves enough memory for the operating system and other processes.
innodb_buffer_pool_size = 24G
2. Query Cache
The query cache is known to cause performance issues on busy servers, and it has been removed in MySQL 8.0. It's best to disable it on modern systems.
query_cache_type = 0
query_cache_size = 0
3. Thread Cache Size
The thread cache stores threads for reuse, which avoids the overhead of creating new threads for each connection. A higher value is beneficial for a server with many concurrent connections.
thread_cache_size = 256
4. Max Connections
Set max_connections to a value that reflects the expected number of concurrent users or application connections. A value of 500 is a good starting point for a busy server.
max_connections = 500
Complete my.cnf Example
Here is a summary of the recommended settings. Copy and paste these into your my.cnf file under the [mysqld] section.
[mysqld]
# Basic settings
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
# Memory & InnoDB settings
innodb_buffer_pool_size = 24G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_flush_method = O_DIRECT
# Concurrency & Performance
max_connections = 500
thread_cache_size = 256
# Query Cache (recommended to disable)
query_cache_type = 0
query_cache_size = 0
# MyISAM (if applicable)
key_buffer_size = 128M
# Logging
log_error = /var/log/mysql/error.log
Applying the Changes
After saving your changes, you must restart the MySQL service to apply the new settings.
sudo systemctl restart mysql
You can then check the status of the service to ensure it started successfully.
sudo systemctl status mysql