On my laptop, I use MySQL and PostgreSQL only for testing. I do not need them up until I start programming, which might be hours after startup. But starting the services manually and typing in my sudo password is a (minor) annoyance.
I read that systemd supports starting services only when the port for that service is accessed. But a quick Google search seems to indicate that socket-based activation is not yet supported in PG & MySQL.
I realize I can hack this using shell scripts or wait for the maintainers to fix the services, but I am looking for a better way now (for educational purposes).
The Question: How can I achieve on-demand startup of such services in a way that either utilizes systemd features or is recommended as a Linux "best practice"?
Some thoughts:
- Is there a service I can install that handles auto-starting and auto-stopping services based on conditions (such as a particular process running)?
- Is there a proxy service that gets activated by a socket and in turn launches the target service?
systemd 229, Kubuntu 16.04, MySQL 5.7, PostgreSQL 9.5
Update: The Answer:
How I used systemd-socket-proxyd as suggested by Siosm:
/etc/mysql/mysql.conf.d/mysqld.cnf
port = 13306
/etc/systemd/system/proxy-to-mysql.socket
[Socket]
ListenStream=0.0.0.0:3306
[Install]
WantedBy=sockets.target
/etc/systemd/system/proxy-to-mysql.service
[Unit]
Requires=mysql.service
After=mysql.service
[Service]
# note: this path may vary
ExecStart=/lib/systemd/systemd-socket-proxyd 127.0.0.1:13306
PrivateTmp=no
PrivateNetwork=no
Reload/ stop/ start as needed:
sudo systemctl daemon-reload
sudo systemctl enable proxy-to-mysql.socket
sudo systemctl start proxy-to-mysql.socket
sudo systemctl stop mysql.service # for testing
Test:
sudo systemctl status proxy-to-mysql.socket # should be ACTIVE
sudo systemctl status proxy-to-mysql # should be INACTIVE
sudo systemctl status mysql # should be INACTIVE
telnet 127.0.0.1 3306
sudo systemctl status proxy-to-mysql # should be ACTIVE
sudo systemctl status mysql # should be ACTIVE