Wednesday, March 30, 2022
MariaDB Best practices for Optimization & Tuning
Increase Open Files Limit
Increase Open Files Limit
To ensure good server performance, the total number of client connections, database files, and log files must not exceed the maximum file descriptor limit on the operating system (ulimit -n). Linux systems limit the number of file descriptors that any one process may open to 1,024 per process. On active database servers (especially production ones) it can easily reach the default system limit.
To increase this, edit /etc/security/limits.conf and specify or add the following:
1 2 3 | mysql soft nofile 65535 mysql hard nofile 65535 |
This requires a system restart. Afterwards, you can confirm by running the following:
1 2 3 4 5 6 7 | $ ulimit -Sn 65535 $ ulimit -Hn 65535 |
Optionally, you can set this via mysqld_safe if you are starting the mysqld process thru mysqld_safe,
1 2 3 | [mysqld_safe] open_files_limit=4294967295 |
or if you are using systemd,
1 2 3 4 5 6 7 8 9 10 11 | sudo tee /etc/systemd/system/mariadb .service.d /limitnofile .conf <<EOF [Service] LimitNOFILE=infinity EOF sudo systemctl daemon-reload |
Max Connections - mariadb/mysql
Max Connections
Max connection parameter in MySQL shows how many concurrent connections can be initiated on your MariaDB server. default is 151.
First thing to decide is what new maximum value you want to set for max_connections
Considerations to take into account when increasing the number of MySQL/MariaDB connections.
The maximum number which can be supported by the system will depend on:
1)The amount of available RAM.
2)How much RAM each connection takes (simple queries will require less RAM than more labor-intensive connections).
3)The acceptable response time.
According to the MySQL documentation, most Linux systems should be able to support 500-1000 connections without difficulty.
Systems that get too busy can return the too_many_connections error.
When the number of threads_connected exceeds the max_connections server variable, it's time to make a change.
Viewing the threads_connected status variable shows only the current number of connections,
but it's more useful to see what the value has peaked at, and this is shown by the max_used_connections status variable.
This error may be a symptom of slow queries and other bottlenecks,
but if the system is running smoothly this can be addressed by increasing the value of max_connections.
What Is The Source Of The Database Connections?
Use the ‘SHOW processlist’ SQL command to show you which threads are currently running.
It will, for instance, provide the following details:
- User – The MySQL user who issued the statement.
- Host – The hostname of the client issuing the statement.
- Command – The SQL command that is executed.
The ‘show processlist SQL command:
SHOW processlist
How to change max_connections
The max_connections variable will need to be changed in two places:
- Update the my.cnf file, so that the new value is used if the MySQL/MariaDB server is restarted.
- Use the SET GLOBAL command to update the value on the running MySQL/MariaDB server.
Fortunately, by using this method, you will not need to restart MySQL/MariaDB, and therefore will not need to experience any downtime.
Show the Current max_connections Value
To see the current number of max_connections log in to the MySQL/MariaDB command line client with the command:
mysql -u root -p
Use the command:
SHOW variables;
This will output a list of all of the variables which are set for MySQL/MariaDB. Scroll up through the list to find the value for max_connections.
Update my.cnf
Open the file /etc/my.cnf for editing with the command:
sudo nano /etc/my.cnf
Directly beneath the first line:
[mysqld]
Add a line:
max_connections=[desired new maximum number]
For example, to set max_connections to 200, the first two lines of the file will read:
[mysqld] max_connections=200
Save and exit the file.
SET GLOBAL
Log in to the MySQL/MariaDB command line client with the command:
mysql -u root -p
Set the new max_connections value with the command:
SET GLOBAL max_connections=[desired new maximum number];
For example, to set max_connections to 200, the command is:
SET GLOBAL max_connections=200;
Exit MySQL/MariaDB with the command:
quit;