Files
Epicnabbo-Catalogus-Updated…/extra tuts/Best mysql 24gb ram vps settings.md
T
2025-09-21 13:18:46 +02:00

119 lines
3.7 KiB
Markdown

### Understanding the Basics
The standard MySQL settings are often not optimized for a server with 24 GB of RAM. By adjusting a few key values, you can significantly improve your database's performance. The most important settings to optimize are memory-related, as your server has plenty of it. A buffer that is too small can cripple performance, while one that is too large can lead to excessive use of swap space.
You'll need to open your MySQL configuration file, which is typically named `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.
```bash
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak
sudo nano /etc/mysql/my.cnf
```
-----
### Key Settings for 24 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** with 24 GB of RAM. If you also use the server for other purposes, you may want to lower the values.
#### 1\. InnoDB Buffer Pool Size
This is the single most important setting. The **InnoDB buffer pool** is the cache for your database. A larger buffer pool means MySQL can keep more of your data in fast RAM, reducing the need to read from the disk.
A good rule of thumb is to allocate 70-80% of your total RAM. For a 24 GB server, a value between 16 and 19 GB is excellent.
```ini
innodb_buffer_pool_size = 16G
```
#### 2\. Query Cache
The `query_cache` stores the results of previously executed queries. While this can be useful for sites with many identical, repeated queries, it often becomes a performance bottleneck on busy servers. The query cache was deprecated in MySQL 5.7.20 and completely removed in MySQL 8.0. **It's best practice to disable it.**
```ini
query_cache_type = 0
query_cache_size = 0
```
#### 3\. Thread Cache Size
The `thread_cache_size` determines how many threads MySQL keeps in a cache for reuse. This prevents the overhead of constantly creating new threads. A higher value is beneficial for a busy server.
```ini
thread_cache_size = 128
```
#### 4\. Max Connections
Set `max_connections` to a value that suits the expected number of concurrent users. The default of 151 is often too low for a busy website.
```ini
max_connections = 500
```
#### 5\. MyISAM Key Buffer Size
If you're still using MyISAM tables, you should set the `key_buffer_size`. If you're exclusively using InnoDB (which is the default and recommended), this value is not as important.
```ini
key_buffer_size = 128M
```
-----
### Complete `my.cnf` Example
Here is a summary of the recommended settings. You can copy and paste this into your `my.cnf` file under the `[mysqld]` section.
```ini
[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 = 16G
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 = 128
# 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.
```bash
sudo systemctl restart mysql
```
You can then check the status of the service to ensure it started successfully.
```bash
sudo systemctl status mysql
```
By applying these settings, your database will make much more efficient use of the 24 GB of RAM, resulting in significantly better responsiveness and throughput for your application.