The mysql_secure_installation script is the easiest five minutes you will ever spend on a new MySQL host, and the one most often skipped. This article walks every prompt, explains what is happening underneath, and lists the gaps the script does not close — the part you finish by hand or by automation.
How to verify
Before running the script, confirm the state the package install left behind. A fresh Ubuntu apt install leaves anonymous accounts, a test database, and root reachable from any host in some package builds.
sudo mysql -e "SELECT user, host, plugin FROM mysql.user;"
sudo mysql -e "SHOW DATABASES;"
sudo mysql -e "SELECT user, host FROM mysql.user WHERE user='' OR host='%';"
If you see rows with an empty user field or host = '%' for the root account, the script has more to do. After running it, repeat these queries and confirm those rows are gone.
What’s happening
The script is a small shell wrapper around a handful of SQL statements. It does six things in order: prompt for the root password (or set one if not already set), install the validate_password component, change the root password if you say yes, drop anonymous accounts, restrict root to local connections, drop the test database and its broad grants, and flush privileges so the changes take effect immediately. None of it is magic — you can do all of it by hand in mysql -u root. The script just makes it easy to apply consistently across hosts.
The default authentication plugin matters. MySQL 8 ships with caching_sha2_password as the default, but Ubuntu’s packaging leaves root@localhost on auth_socket. The script does not change that — it asks for a password, and if you provide one it leaves auth_socket in place and the password is for the new validated password use. Many operators are confused by this; understand it once and move on.
The procedure
-
Run the script. It must run as a user who can
sudo mysql(or you can authenticate with the current root password).sudo mysql_secure_installation -
Answer the validate_password prompt. It will ask if you want to install VALIDATE PASSWORD COMPONENT. On production hosts: yes. Choose level 2 (STRONG) — eight or more characters, mixed case, numeric, special, and not in the common-password dictionary.
-
Set or replace the root password. If root has no password (the default on Ubuntu), it will ask you to set one. If it already has one, it will ask if you want to change it. Pick a long random string from a password manager and store it in a vault — you will need it for emergency recovery, not for daily admin.
-
Remove anonymous users — say yes. Anonymous accounts let any local user connect without credentials. There is no production use for them.
-
Disallow root login remotely — say yes. Root should never log in from a non-localhost address. Application users connect with their own scoped accounts.
-
Remove the test database — say yes. The
testschema has a broadtest%grant by default that lets any user touch any database matching the pattern. Drop it. -
Reload privilege tables — say yes. This is
FLUSH PRIVILEGES— apply the changes without restarting the daemon.
What the script does not do
The script is a starting point, not a finished job. After it runs, complete the following by hand or via configuration management:
-- Confirm no '%' hosts remain for any account except where you explicitly need them
SELECT user, host FROM mysql.user WHERE host = '%';
-- Confirm no user has empty authentication string
SELECT user, host, authentication_string FROM mysql.user WHERE authentication_string = '';
-- List accounts with SUPER, FILE, PROCESS, RELOAD — should be tightly held
SELECT GRANTEE, PRIVILEGE_TYPE FROM information_schema.USER_PRIVILEGES
WHERE PRIVILEGE_TYPE IN ('SUPER','FILE','PROCESS','RELOAD','SHUTDOWN');
Then complete this hardening list:
- Bind to a specific interface. Default
bind-addressafter apt install is127.0.0.1, which is correct for app-on-same-host. For multi-host topologies, bind to a private-subnet address and never to0.0.0.0. - Require TLS for non-localhost users. In MySQL 8:
ALTER USER 'app'@'10.0.0.%' REQUIRE SSL;and ensure the daemon has a cert (/etc/mysql/ssl/server-cert.pem,/etc/mysql/ssl/server-key.pem,/etc/mysql/ssl/ca.pem). - Audit logging. The community build does not include the MySQL Enterprise Audit plugin. Use the general log temporarily for forensic capture, or wire a parser (
pt-query-digest) to the slow-query log for steady-state visibility. - Lock down the OS account. The
mysqluser should not have a shell —usermod -s /usr/sbin/nologin mysql. Confirm the data directory is owned bymysql:mysqlwith0700on/var/lib/mysql. - Backups before exposing the port. A fresh install with no backup that gets pwned in week two has nothing to roll back to. See mysql-mysqldump-backup-restore for the first-day backup pattern.
Common pitfalls
- The validate_password component, once installed, rejects every weak password — including the one you might set for a local test account. Either set strong passwords everywhere or uninstall the component on dev hosts:
UNINSTALL COMPONENT 'file://component_validate_password';. - The script’s “remove anonymous users” step does not remove anonymous accounts created later by
CREATE USER ''@'somehost'. Periodically re-run the verification query. mysql_secure_installationdoes not change the auth plugin. If you needcaching_sha2_passwordforroot@localhost, do it withALTER USERafterwards.- Running the script without
sudoon a host where root uses auth_socket will prompt for a password that does not exist — you have to usesudo mysql_secure_installation. - On a rebuild of an existing instance (data directory restored from backup), the script’s password prompts may fail silently if the old user table is in place. Restore the data, then connect with the historical root password and reapply the hardening SQL by hand.
In the engagements we run, mysql_secure_installation runs from an Ansible role at install time, with the validate_password level and a vaulted root password baked into the playbook so every host comes up identical. We carry that operating model under Managed Operations — the install-day hardening is part of the runbook, not a tribal afterthought.