Mysql – Systemd boot ordering: Wait for thesql.sock

MySQLsystemd

I have an openvpn tunnel which runs an --up script which queries a local mysql database for IP routing information and then applies the routes. However, since both the openvpn and mysql systemd services are configured with the After=network.target directive, I have a race condition: /var/run/mysqld/mysqld.sock might exist when the --up script runs, or it might not. If the sock file doesn't exist, then my script dies and the system doesn't initialize properly.

I thought that changing my openvpn.service directive to After=network.target mysql.service and running systemctl daemon-reload would do the trick. However, I added ls -lh /var/run/mysqld/mysqld.sock to the beginning of my --up script, and it still sometimes reports ls: cannot access '/var/run/mysqld/mysqld.sock': No such file or directory.

My guess is that systemd's is not using the sock file to determine whether or not mysqld has finished loading but rather some criteria that happens before the sock file is created.

Is there a way to tell systemd to not recognize mysql.service as having started until the sock file has been created?

Best Answer

i had the same problem, start some other service after mysql started

First i disable my service

systemctl disable rfid-server

After that i modified my rfid-server.service file

[Unit]
Description=rfid server
After=mysql.service

[Service]
ExecStart=/opt/rfid-server-service/deploy/start.sh

[Install]
WantedBy=multi-user.target mysql.service

An the i re-enable my service

systemctl enable /complete/path/to/service

Created symlink from /etc/systemd/system/multi-user.target.wants/rfid-server.service to /opt/rfid-server-service/rfid-server.service.
Created symlink from /etc/systemd/system/mysql.service.wants/rfid-server.service to /opt/rfid-server-service/rfid-server.service.
Created symlink from /etc/systemd/system/rfid-server.service to /opt/rfid-server-service/rfid-server.service.

As you can see, it create the symlink to mysql.service.wants