Fail2ban is not able to connect to mysql under reboot prosess, both shutdown and startup process, Can't connect to MySQL server on '127.0.0.1' (111)

I'm running a LAMP setup on Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-135-generic x86_64).

I have integrated fail2ban and db using documentation from . Everything works perfect when server is up and running, but at some reason fail2ban is not able to connect to the database(mysql) when rebooting. I'am able to connect manually to mysql using the same parameter as in fail2ban_banned_db script, both from "127.0.0.1" and "localhost". Ban and unban to db is working normally, also the crontab script unbanning from db.

I get this error both on shutdown process and startup process.

Under shutdown:

2021-02-01 22:26:59,412 fail2ban.actions [20149]: NOTICE [wp-admin] Unban 93.114.234.242
2021-02-01 22:26:59,433 fail2ban.utils [20149]: Level 39 7f2980c48100 -- exec: /usr/local/bin/fail2ban_banned_db unban 93.114.234.242
2021-02-01 22:26:59,434 fail2ban.utils [20149]: ERROR 7f2980c48100 -- stderr: "ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)"
2021-02-01 22:26:59,434 fail2ban.utils [20149]: ERROR 7f2980c48100 -- returned 1
2021-02-01 22:26:59,434 fail2ban.actions [20149]: ERROR Failed to execute unban jail 'wp-admin' action 'banned_db' info 'ActionInfo({'ip': etc...

Under startup:

2021-02-01 22:28:41,968 fail2ban.utils [1104]: ERROR 7f595c169248 -- stderr: "ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)"
2021-02-01 22:28:41,968 fail2ban.utils [1104]: ERROR 7f595c169248 -- returned 1
2021-02-01 22:28:41,968 fail2ban.actions [1104]: ERROR Failed to execute ban jail 'badbot' action 'banned_db' info 'ActionInfo({'ip': etc....

It's like mysql goes down before fail2ban under the shutdown process and start up after fail2ban under startup process, but this is only my guess, not have any clue about whats happening. I don't want to tamper to much with the server installation.

Hope you can help me with this problem.

Thanks

1 Answer

Your /var/log/syslog should show the exact order in which services are started and shut down. That said, systemd will often start and stop multiple services in parallel, which can create this kind of issue. Fortunately, you can tell systemd about dependencies with the After directive.

Here's how:

  1. Open Terminal (If it's not already open)

  2. Look for a fail2ban.service file in the systemd directories. This will probably be in /lib/systemd/system, but could be in /etc/systemd/system.

  3. Open the fail2ban.service file with sudo

  4. Add the following line to the end of the [Unit] section:

    After=mysql.service
  5. Save the file.

This should tell systemd that fail2ban requires MySQL, so should shutdown beforehand and start up afterwards 👍🏻

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like