0

I need to edit the mysql.service file to make MySQL automatically restart when it crashes. I'm working with a pretty much stock Ubuntu 18.04 server install and followed these instructions https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04 for a pretty much stock MySQL install.

My concern is I'm not sure where to edit mysql.service. If I do this:

sudo updatedb
locate mysql.service

I get this:

/etc/systemd/system/multi-user.target.wants/mysql.service
/lib/systemd/system/mysql.service
/var/lib/lxcfs/cgroup/blkio/system.slice/mysql.service
/var/lib/lxcfs/cgroup/cpu,cpuacct/system.slice/mysql.service
/var/lib/lxcfs/cgroup/devices/system.slice/mysql.service
/var/lib/lxcfs/cgroup/memory/system.slice/mysql.service
/var/lib/lxcfs/cgroup/name=systemd/system.slice/mysql.service
/var/lib/lxcfs/cgroup/pids/system.slice/mysql.service
/var/lib/systemd/deb-systemd-helper-enabled/mysql.service.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/mysql.service

This is strange because I'm used to systemd .service files being located in /etc/systemd/system. Since the path is similar, I would be inclined to suppose that /lib/systemd/system/mysql.service is probably the file I should edit. However if anybody could provide some additional information on the following that would be great:

  1. Is /lib/systemd/system/mysql.service the file I should be editing?

  2. Why are there so many mysql.service files? This seems to cause confusion.

  3. What is multi-user.target.wants ? Should I change the mysql.service file in there also?

  4. Some simple googling reveals that lxcfs is short for Linux Containers, should I change any of the mysql.service files in there ?

  • Additional information request. Post on pastebin.com and share links OR here. A) complete (not edited) my.cnf or my.ini Text results of: B) SHOW GLOBAL STATUS; after minimum 24 hours UPTIME C) SHOW GLOBAL VARIABLES; D) complete MySQLTuner report AND Optional very helpful information, if available includes - htop OR top OR mytop for most active apps, ulimit -a for a linux/unix list of limits, iostat -xm 5 3 for an idea of IOPS by device, df -h for a linux/unix free space list by device, for server tuning analysis when done editing OS and running at least 7 days. – Wilson Hauck Jan 6 at 19:41
2

The files in /lib/systemd/system should not be edited by the admin directly and should be left as they are. Those files belong to an installed package and updates and/or changes could break things.


To modify existing systemd files and configuration like the .service files, systemd provides the /etc/systemd/system path, which overrides files and settings found in /lib/systemd/system.
So, if you e.g. have two .service files as follows,

/etc/systemd/system/mysql.service
/lib/systemd/system/mysql.service

the one located at /etc/systemd/system/mysql.service will be used when entering commands like systemctl [start|stop|enable|disable] mysql.service.


Additionally, systemd provides drop-in ".d" directories which allows to just change or add a single option of the .service file in /lib/systemd/system. You can use systemctl edit mysql.service to create such a drop-in .d directory.
All you need to enter then is the section (e.g. [Service], [Unit], ... ) and the option you want to change.
Since you want to change the restart behaviour, do a systemctl edit mysql.service and enter the lines as follows.

[Service]
Restart=on-failure

This will create a folder

/etc/systemd/system/mysql.service.d

and the override file

/etc/systemd/system/mysql.service.d/override.conf

which contains the lines you entered earlier. You can also manually create the files and folders.

I would prefer this method over copying the whole .service file, as the package updates bring changes, you only have to take care of the single changes you have altered.


In any case, when working with systemd and doing changes to configuration files, one has to enter

systemctl daemon-reload 

to activate the changes.


Do not change anything else like lxcfs related things. The multi-user.target.wants is just an organizational method of systemd to group services together that shall be started for the multi-user target. Targets in systemd replaces the old init 0 1 2 3 4 5 6 method.
Some of the double mysql.service files, will be symbolic links that point to your actual .service file.


Please also see the manpages as follows.

man systemctl
man systemd.unit
man systemd.service
man systemd.target

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.