13

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
1

2 Answers 2

11

You may use the systemd-socket-proxyd tool to forward traffic from a local socket to MySQL or PostgreSQL with socket-activation. See systemd-socket-proxyd(8) for examples, and read this SO reply for a concrete example for --user systemd.

5

socket-based activation is not yet supported in PostgreSQL & MySQL.

The question is the answer. You have already found the better way, mentioned it in the question, and then stated that it has not been implemented. Oracle closed the issue saying that socket activation (really, using already-opened listening file descriptors instead of opening its own, as far as the server program is concerned) has been implemented when it has not been. MariaDB has a still-open issue. As for PostgreSQL, you are in the same boat as Christoph Berg waiting for this to be implemented.

Further reading

2
  • Thank you, but the whole point of my question is to figure out how to achieve this now, for any such services. I will clarify this in my question. Commented Mar 20, 2017 at 0:14
  • Mariadb now supports socket activation. It is currently only available in the alpha release 10.6.0 that was released 26 Apr 2021. The Jira issue MDEV-5536 is now marked as fixed Commented May 16, 2021 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .