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
Oleg Pryadko
  • 2,330
  • 7
  • 25
  • 33
  • 1
    Answers should only be _in answers_. You should not edit an answer into a question -- if something isn't covered by the existing answers, use the "Add an Answer" button to add your own. Reference: [What to do when OP answers his/her own question in an edit?](https://meta.stackexchange.com/questions/74101/what-to-do-when-op-answers-his-her-own-question-in-an-edit), and [Should I update my question's code to include the correct answer?](https://meta.stackexchange.com/questions/41700/should-i-update-my-questions-code-to-include-the-correct-answer) on [meta.se]. – Charles Duffy Sep 21 '21 at 14:49

2 Answers2

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.

ankostis
  • 491
  • 5
  • 11
Timothée Ravier
  • 912
  • 8
  • 15
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

JdeBP
  • 66,967
  • 12
  • 159
  • 343
  • 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. – Oleg Pryadko Mar 20 '17 at 00:14
  • Mariadb now supports socket activation. It is currently only available in the alpha release [10.6.0](https://mariadb.com/kb/en/mariadb-1060-release-notes/) that was released 26 Apr 2021. The Jira issue [MDEV-5536](https://jira.mariadb.org/browse/MDEV-5536) is now marked as _fixed_ – Erik Sjölund May 16 '21 at 12:26