Where is my.cnf? MySQL and MariaDB config files explained
Find the MySQL or MariaDB config files on Ubuntu, Debian, AlmaLinux and Rocky Linux, learn the order they load in, and confirm which settings are really active.
- Beginner
- Linux VPS
- 5 min read
Guides often say "add this line to my.cnf", but on a modern Linux server my.cnf is usually just a list of other files to read. Edit the wrong one and your change either does nothing or gets overwritten by a package update. This guide shows where MySQL and MariaDB look for settings, which file you should edit, and how to confirm a change took effect.
Before you start
- MySQL or MariaDB installed on your Linux VPS.
- A user with
sudorights. - Know which server you run.
mysql --versionshowsMariaDBin the output for MariaDB, and just a version number for MySQL.
How MySQL and MariaDB read config files
The server reads a fixed list of option files at startup. Settings in files read later override the same setting from earlier files. Ask the server binary for its own list:
mysqld --verbose --help 2>/dev/null | grep -A1 "Default options"On recent MariaDB releases the binary is mariadbd, so run the same command with mariadbd if mysqld is not found. Typical output:
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnfInside those files, two directives pull in more files:
- `!include /path/file.cnf` reads one extra file.
- `!includedir /path/dir/` reads every
.cnffile in that folder.
Within each file, settings sit under group headers. The server reads [mysqld] (and [mariadb] on MariaDB). Client tools such as mysql and mysqldump read [client]. A line under the wrong header is ignored by the server.
Ubuntu and Debian
Both MySQL and MariaDB packages use /etc/mysql/. /etc/mysql/my.cnf is a short file, sometimes a symlink, that includes folders.
MySQL
/etc/mysql/my.cnfincludes/etc/mysql/conf.d/and/etc/mysql/mysql.conf.d/.- The main server settings are in
/etc/mysql/mysql.conf.d/mysqld.cnf.
MariaDB
/etc/mysql/my.cnfpoints to/etc/mysql/mariadb.cnf, which includes/etc/mysql/conf.d/and/etc/mysql/mariadb.conf.d/.- The main server settings are in
/etc/mysql/mariadb.conf.d/50-server.cnf.
AlmaLinux and Rocky Linux
The RHEL family uses /etc/my.cnf, which includes /etc/my.cnf.d/.
- MariaDB from AppStream: server settings in
/etc/my.cnf.d/mariadb-server.cnf. - MariaDB from the MariaDB repository: server settings in
/etc/my.cnf.d/server.cnf. - MySQL from AppStream: server settings in
/etc/my.cnf.d/mysql-server.cnf.
If you are unsure, list the folder and search for the [mysqld] header:
grep -rl "^\[mysqld\]" /etc/my.cnf /etc/my.cnf.d/ /etc/mysql/ 2>/dev/nullWhich file should you edit?
Put your own changes in a new file in the server's include folder rather than editing the packaged file. Package updates then cannot overwrite them, and you can see all your changes in one place. Name it so it sorts last:
- Ubuntu or Debian, MySQL:
/etc/mysql/mysql.conf.d/99-custom.cnf - Ubuntu or Debian, MariaDB:
/etc/mysql/mariadb.conf.d/99-custom.cnf - AlmaLinux or Rocky Linux:
/etc/my.cnf.d/99-custom.cnf
Example content, with values you should adjust for your server's RAM:
[mysqld]
bind-address = 127.0.0.1
max_connections = 100
innodb_buffer_pool_size = 512MSet each option in one file only. MariaDB reads included folders in alphabetical order, but do not rely on ordering to resolve conflicts between files.
Apply and check the change
A typo in a config file can stop the database from starting, which takes your sites offline. Keep a copy of any file before you edit it, and restart during a quiet period.
On MySQL 8.0 and later, test the config before restarting:
sudo mysqld --validate-configNo output means the config is valid. MariaDB has no equivalent flag, so restart and check the status straight away. Restart the service; its name differs by system:
sudo systemctl restart mysql # MySQL on Ubuntu and Debian
sudo systemctl restart mysqld # MySQL on AlmaLinux and Rocky Linux
sudo systemctl restart mariadb # MariaDB everywhereCheck it works
The config files show what you asked for. The running server shows what it is actually using. Check the live value:
SHOW GLOBAL VARIABLES LIKE 'max_connections';
SHOW GLOBAL VARIABLES LIKE 'innodb_buffer_pool_size';The buffer pool size is shown in bytes, so 536870912 equals 512M.
To see every option the server would read from all files, merged, run:
my_print_defaults mysqldOn MySQL, also check for settings saved with SET PERSIST. They are stored in mysqld-auto.cnf in the data folder, usually /var/lib/mysql/, and override the .cnf files:
SELECT * FROM performance_schema.persisted_variables;Troubleshooting
- Change has no effect: the line is under the wrong group header, a later file sets the same option, or a
SET PERSISTvalue overrides it. Check withmy_print_defaultsandpersisted_variables. - Server fails to start after an edit: read
sudo journalctl -u mysql -n 50(ormysqldormariadb). The log usually names the unknown option or bad value. Fix it or restore your copy. - `unknown variable` error: the option does not exist in your version, or it is MySQL-only or MariaDB-only. Check the reference for your exact version.
- `mysqld: command not found`: the binary is in
/usr/sbin, which may not be in your path. Use/usr/sbin/mysqldor/usr/sbin/mariadbd. - Client keeps asking for a password: a
[client]section in~/.my.cnfcan hold credentials for that Linux user only. Keep that file atchmod 600.
Next steps
- Back up before larger tuning changes: Back up and restore MySQL with mysqldump.
- Keep an eye on memory after raising buffer sizes: Monitor Linux server resources.
- Still choosing an engine? Read MySQL vs MariaDB.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Databases
Reset a Forgotten MySQL Root Password on Linux
Recover access to MySQL or MariaDB on a Linux VPS by starting the server with skip-grant-tables, setting a new root password and restarting normally.
- Intermediate
- Linux VPS
- 5 min read
Databases
MySQL Point-in-Time Recovery Using Binary Logs
Recover a MySQL or MariaDB database to an exact moment after a bad write or dropped table by restoring a full backup and replaying binary logs up to the incident.
- Advanced
- Linux VPS
- 7 min read
Databases
How to create a database and user for a web app in MySQL or MariaDB
Set up a utf8mb4 database and a dedicated user with a strong password for WordPress, Laravel or any web app, then test the login before you configure the app.
- Beginner
- Linux VPS
- 4 min read