Restart
With systemd you can set up your service to Restart on certain conditions:
systemctl cat mysql.service | grep Restart # Check current status. cat PATTERN... Show files and drop-ins of specified units
Probably you wish to move from on-abort to on-failure, on [Service] section.
sudo EDITOR=nano systemctl edit mysql.service
Other useful commands:
sudo systemctl daemon-reload # Reload systemd manager configuration. `systemctl edit` automatically does this for you.
systemctl cat mysql.service # unit configuration
systemd-delta # Check the whole system changes
OOMScoreAdjust
On the same place, you may also want to adjust systemd OOMScoreAdjust:
Sets the adjustment value for the Linux kernel's Out-Of-Memory (OOM) killer score for executed processes. Takes an integer between -1000 (to disable OOM killing of processes of this unit) and 1000 (to make killing of processes of this unit under memory pressure very likely).
# Useful options not previously available in [mysqld_safe]
Kernels like killing mysqld when out of memory because its big.
Lets temper that preference a little.
OOMScoreAdjust=-600
Read also this excellent answer.
You can also reduce the memory usage of MySQL/MariaDB by tuning some options like max_connections, innodb_buffer_pool_size and innodb_buffer_pool_instances (probably on /etc/mysql/mariadb.conf.d/50-server.cnf).