Skip to content

Database audit fix · Authentication

How to Remove MySQL Anonymous Users and Accounts

An anonymous MySQL account is a row in mysql.user with a blank username, so a client can connect with any name that does not otherwise exist. Count them with SELECT COUNT(*) FROM mysql.user WHERE user='' and drop every host variant.

Hiren KalariyaHiren KalariyaLast reviewed mysql-anonymous-users
CtrlOpsMySQL Anonymous UsersHIGH

What did your audit say?

FAIL · when the audit printed

Count > 0

Anyone can connect without credentials. The count is reported; run mysql_secure_installation or drop each blank-username row by hand.

Severity
High
Needs root or sudo
Yes
Results it can return
FAIL · SKIP · PASS
Check in this audit
2 of 8

Every threshold on this page is transcribed from the database-security-authentication audit script that CtrlOps runs, and a build check fails if the two ever disagree. See all 8 Authentication checks, or how the audit runs.

Runs SELECT COUNT(*) FROM mysql.user WHERE user='' against the live server over the privileged socket connection. Any row with an empty user name is an anonymous account: clients can connect with any username that does not otherwise exist and get that account's privileges. The threshold is zero: a count of 0 is the PASS, any count above 0 is the FAIL, and the number itself is quoted in the result message. It reads the account table rather than a config file, so it always reflects the accounts that exist right now.

Runs only when MySQL/MariaDB is detected (HAS_MY=1) and the script can query it as root over the socket (MYOK=1). Without that socket access the check SKIPs rather than guessing. It reads the server's own mysql.user table, so it reflects the live account list, not a config file. Containerised MySQL is not covered.

What each result means
ResultWhenWhat it means
FAILCount > 0Anyone can connect without credentials. The count is reported; run mysql_secure_installation or drop each blank-username row by hand.
PASSCount = 0Every connection has to name a real account.
SKIPMYOK is 0 (no root socket access, or server down)Nothing was measured. The account list was never read, so no claim is made either way.

MySQL's access-control stage 1 matches the most specific user@host row; an anonymous row ''@'localhost' shadows real accounts and grants access with no credentials. mysql_secure_installation removes them. CIS MySQL: "Ensure anonymous accounts are removed".

  • Older source/tarball installs and some cloud images ship ''@'localhost' and ''@'<hostname>'. Modern packages do not.
  • The test database grants were historically writable by anonymous users (see the Insecure Database Defaults check).
  • No known false positives: an empty user name is never legitimate.
  1. Count the anonymous accounts

    Any count above zero means connections are accepted with no username. List the rows too, because an installer usually leaves one per hostname.

    sql
    SELECT COUNT(*) FROM mysql.user WHERE user='';SELECT user, host FROM mysql.user WHERE user='';

  2. List the passwordless named accounts

    The same pass is worth spending on named accounts with an empty password. Socket-authenticated accounts and the packaged system accounts are excluded, because neither is a finding.

    sql
    SELECT CONCAT(user,'@',host) FROM mysql.userWHERE authentication_string=''  AND plugin NOT IN ('auth_socket','unix_socket')  AND user NOT IN ('','mysql.sys','mysql.session','mysql.infoschema','mariadb.sys','debian-sys-maint');

  3. Run the hardening script

    Run mysql_secure_installation and answer yes to "Remove anonymous users". It removes anonymous accounts, drops the test database and disables remote root in one pass.

  4. Fix what is left by hand

    Give a named account a password if something still uses it, drop it if nothing does, and repeat the DROP USER for every host returned in step 1.

    sql
    ALTER USER 'appuser'@'localhost' IDENTIFIED BY 'long-random-password';DROP USER ''@'localhost';   -- repeat for each host returnedFLUSH PRIVILEGES;

sh
sudo mysql -N -e "SELECT COUNT(*) FROM mysql.user WHERE user=''"# expected: 0# Any client using a made-up username must now be refused:mysql -u nosuchuser -h 127.0.0.1 -e 'SELECT 1'# expected: ERROR 1045

Debugging

  • MYOK=0. Run sudo mysql -N -e 'SELECT 1'; if that fails the check cannot run. Causes: server stopped, wrong socket path, or no usable sudo (see Script context).
  • You dropped ''@'localhost' but not ''@'<hostname>'. List them all with SELECT user,host FROM mysql.user WHERE user=''; and drop each host variant, then FLUSH PRIVILEGES.
  • sudo mysql -N -e "SELECT COUNT(*) FROM mysql.user WHERE user=''" is the entire test. Any non-zero count is the FAIL.
  • There are no known false positives here: an empty user name is never a legitimate account. If the count is above zero, the rows exist; list them with SELECT user,host FROM mysql.user WHERE user=''; and drop each one.

Sources

Next

Re-run the Authentication audit after applying the fix and confirm this check moves to PASS. CtrlOps runs all 8 checks over your existing SSH connection and scores the result, so the change is visible without reading another config file.

Download for MacDownload for WindowsAll 8 Authentication fixes

14-day free trial, no credit card required.

Explore article with AI tools

Written by

Hiren Kalariya

Co-Founder & CEO, TST Technology

Hiren Kalariya is the Co-Founder and CEO of TST Technology and the product lead behind CtrlOps, a local-first desktop app that makes Linux server management fast and easy for developers and DevOps teams. Over the past five years he has shipped 60+ production systems, and he still tests every tool he writes about on real servers.

Stop juggling tools. Start managing fleets.

PuTTY
Termius
MobaXterm
WinSCP
SecureCRT
Royal TS
Warp
CtrlOps14 tools left
Webmin
aaPanel
RunCloud
Dokploy
IP spreadsheet
bash (2)
sticky notes

One app. All your servers. No context switching. Free to start, free to leave.

Start instantly · No credit card · Cancel anytime

Your privacy preferences. We use product analytics to improve CtrlOps. Google Analytics cookies and live chat stay off until you allow them. Cookie policy