How are multiple my.cnf's loaded by MySQL? If I want to replicate how the MySQL/Maria client reads ~/.my.cnf, /etc/my.cnf, etc, how would I do it?

The MySQL docs give a list of paths and mention "top files are read first, files read later take precedence", is that on a key-value basis or the entire file?
The standard config file format applies. It is on group and key basis.

So if you have:

[mysql]
some_variable = 1

[client]
some_variable = 3


and after another file has

[mysql]
some_variable = 2


Then some_variable under mysql will be 2 but under client it will stay 3.


  The only relevant part for MySQL server is the [mysqld] though. 


The easiest way to determine which files are being read is to strace the mysql process on startup. 


Start MySQL with the normal init script and run ps -ef|grep mysqld.
Copy /usr/sbin/mysqld
Then run strace on the command grepping .cnf. 


You should see something like this:

$ strace /usr/sbin/mysqld --basedir=/usr --datadir=/databases/mysql/data --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysql/mysql-error.log --pid-file=/databases/mysql/data/server.local.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 2&gt;&amp;1 |grep .cnf
stat("/etc/my.cnf", 0x7fffa6edeb40)     = -1 ENOENT (No such file or directory)
stat("/etc/mysql/my.cnf", {st_mode=S_IFREG|0644, st_size=5454, ...}) = 0
open("/etc/mysql/my.cnf", O_RDONLY)     = 3
stat("/usr/etc/my.cnf", 0x7fffa6edeb40) = -1 ENOENT (No such file or directory)
stat("/root/.my.cnf", {st_mode=S_IFREG|0644, st_size=73, ...}) = 0
open("/root/.my.cnf", O_RDONLY)         = 3
stat("/root/.mylogin.cnf", 0x7fffa6edeb40) = -1 ENOENT (No such file or directory)
stat("./auto.cnf", {st_mode=S_IFREG|0660, st_size=56, ...}) = 0
open("./auto.cnf", O_RDONLY)            = 13
stat("/root/.mylogin.cnf", 0x7fffa6edeb40) = -1 EACCES (Permission denied)