Okey, so I’ve tested a few more configurations of the mysql.service file that systemd uses to decide how to start and stop the database.
[Service]
Restart=on-failure
Type=simple
ExecStartPre=/usr/lib/mysql/mysql-systemd-helper install default
ExecStartPre=/usr/lib/mysql/mysql-systemd-helper upgrade default
ExecStart=/usr/lib/mysql/mysql-systemd-helper start default
ExecStartPost=/usr/lib/mysql/mysql-systemd-helper wait default
PIDFile=/var/lib/mysql/mysqld.pid
[FONT=courier new]ExecStop=<cmd to kill mysqld>
KillSignal=SIGTERM.
KillMode=process | none | mixed | process-group
[/FONT]
So, the indented keywords are not in the default file. Their purpose, afaik, is
PIDFile= Normally systemd guesses which pid is the “mainpid”, and will kill mainpid(and childs) depending on the setting of KillMode.
Standard setup, systemd will guess that mainpid is the pid of the script “mysqld-system-helper start”, which actually spawns the mysqld server without “&”, so it sticks around until mysqld is terminated.
Default, **KillMode **is set to “control-group”, which should cause systemd to send SIGTERM to ALL started processes, wait a long time, and then send SIGKILL if things dont terminate. But they do terminate, in a second or two. But mysql.error log does NOT indicate that innodb was shutdown properly.
I DO actually get a " /usr/sbin/mysqld: Normal shutdown" in the mysql.error, but not the same output as when I manually use “kill -SIGTERM <pid>”.
What I have to do, to make it work (that is, NOT to have mysql complain on corrupt innodb tables during startup), is to set ExecStop to a command that manually sends SIGTERM to the mysqld process.
If I do this I normally also have to set KillMode= to none.
This because if you for some reason actually want to terminate mysqld with kill/pkill, then systemd will ALSO(when noticing that mysql terminated) run the configured ExecStop command itself(why?). If this ExecStop-command just runs “kill -SIGTERM <PID>” and that pid has already terminated the kill command fails, making systemd believe that something is wrong with the service and tries to restart it again. Sure, special case by still.
If I do this, mysqld stops properly. I can - if I want for some unknown reason - still have KillMode set to “process” if I want, but then I have to remove the PidFile setting so that systemd will guess that the main pid is the script that started mysqld, and send its stop-signal there. If PidFile is still set to the true mysqld-parent-process pid, systemd will stop it in an way that WILL corrupt my innodb…which really makes me wonder about the signal systemd is sending. Turning on debugging on systemd doesnt indicate which.
So the strange thing here is that when I tweak the above ExecStop=<manual kill cmd> + KillMode=none, I get one SIGTERM sent to the parent mysqld process, and nothing more, which gives my a clean shutdown…
And this result - *unless I am STILL missing something from reading man-pages on systemd.{kill|service|unit} *- is exacty what I ***should ***be getting if i just modified the default mysql.service at the top to:
[Service]
Restart=on-failure
Type=simple
ExecStartPre=/usr/lib/mysql/mysql-systemd-helper install default
ExecStartPre=/usr/lib/mysql/mysql-systemd-helper upgrade default
ExecStart=/usr/lib/mysql/mysql-systemd-helper start default
ExecStartPost=/usr/lib/mysql/mysql-systemd-helper wait default
PIDFile=/var/lib/mysql/mysqld.pid // Send signal to mysqld, not to helper-script.
KillMode=process // Only send signal to parent process not to ALL.
I even added KillSignal to force the signal to be SIGTERM, which already is the default…but the above WILL give me a corrupt innodb.
My only conclusion is that systemd is NOT sending SIGTERM, but SIGKILL?? But that just doesnt make sense >:(
TQ