MySQL and MariaDB hardening

my.cnf settings, needless accounts and an application user with just the rights it needs — in one script.
All accounts are saved before changes; rollback is one command.

MySQL and MariaDB hardening

Builds a my.cnf settings file, SQL and a ready script: the server listens only where needed, requires TLS, does not read or write files anywhere, and needless accounts — anonymous ones and root with remote access — are removed. The application gets its own user with only the rights it needs. Before any change the script saves all accounts; the rollback is below.

Server settings

Accounts

localhost — from this server, an IP address — from the application server, 10.0.0.% — from a subnet.
0 — no limit.
User password
Made in your browser and sent nowhere. Save it in the application settings — it is in the script, but there is no way to find it out later.

Result

1. Check what is there now (changes nothing)
2. Hardening script — run it on the server
Separately: settings file and SQL

    
SQL (run as root)
Rollback

Passwords and settings are made in your browser and sent nowhere.

What the script does

Right after installation MySQL and MariaDB are set up for convenience, not security: MariaDB allows LOAD DATA LOCAL and writing files to any folder, MySQL listens for X Protocol on all addresses, older installs keep anonymous users, a test database and root reachable from anywhere, and applications often connect as root. The tool builds a settings file that sits next to the main my.cnf without touching it, SQL for the accounts and a script that applies it all — taking into account the differences between MySQL and MariaDB, Debian and RHEL.

How to use it

  1. Choose the server, the system and where the database must be reachable from. If only a site on the same server uses it, keep “Only from this server”.
  2. Run the commands from “Check what is there now” on the server: they change nothing and show users, rights and current settings.
  3. Tick what you need and set the application database and user names. The password is made in the browser — save it.
  4. Download the script and run it on the server: bash pasw-mysql-hardening.sh. At the end it shows the new settings.
  5. Check the site or application. If something does not work, run the commands from “Rollback”.

Why this beats mysql_secure_installation

mysql_secure_installation only removes anonymous users, the test database and remote root and sets the root password. Here you also get server settings, an application user with rights only to its database, a delay and fail2ban against password guessing, backups by a separate read-only user — and everything is saved before changes. Close port 3306 with the firewall — firewall rules generator; for PostgreSQL there is the pg_hba.conf generator.

Frequently asked questions

Will the script break my site?

Three things break most often: skip_name_resolve when users are set up with host names; removing remote root when the application connects as root over the network; and TLS when a remote client cannot encrypt. The first check command shows all of this in advance, and the script saves all accounts before changing anything.

How do the settings for MySQL and MariaDB differ?

MySQL has X Protocol and the connection_control plugin, its password policy is the validate_password component, and TLS is on from the start. In MariaDB the password policy is the simple_password_check plugin, there is the server_audit log, and before 11.4 TLS needs your own certificate. Paths to settings files and logs differ too — the tool fills in the right ones.

Why doesn't the application user get all rights?

If an SQL injection on the site succeeds, the attacker gets exactly the application user's rights. With rights only to its own database and no FILE, SUPER or GRANT, it cannot read server files, touch other databases or create an admin for itself.

How do I undo the changes?

Delete the settings file with the command from “Rollback” and restart the server — all settings come back. Removed accounts are in /root/pasw-mysql-backup: their CREATE USER and GRANT lines can be run again. The rollback also removes the MySQL password policy and the backups.

More tools

Linux servers

sshd and nginx audit
Find dangerous settings in an existing config
Log analysis
Password guessing, logins and scanners in logs
First server setup
One script: user, SSH, firewall, fail2ban
TLS configuration
HTTPS for nginx, Apache, HAProxy
HTTP security headers
HSTS, CSP, Permissions-Policy and a CSP check
nginx config generator
Proxy, static, PHP, HTTPS and limits
.htaccess generator
HTTPS, www, .env and .git, folder password
Let’s Encrypt certificate
certbot commands with automatic renewal