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.
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.
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.
Passwords and settings are made in your browser and sent nowhere.
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.
bash pasw-mysql-hardening.sh. At the end it shows the new settings.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.
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.
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.
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.
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.