Remove Anonymous User Accounts
By default, some MySQL installations create anonymous user accounts, which have an empty username. These accounts pose a security risk because they can allow unauthorized access. It is a critical security practice to check for and remove them if they exist.
You can identify anonymous users by querying the mysql.user table for entries with a blank username.
SELECT User, Host FROM mysql.user WHERE User = '';
If anonymous users exist, this query will list them. A fresh installation on Ubuntu might include one, but newer versions of MySQL/MariaDB often don't have anonymous users by default.
If you see results like:
+------+-----------+
| User | Host |
+------+-----------+
| | localhost |
+------+-----------+
1 row in set (0.00 sec)
Then you should remove the anonymous user with the DROP USER statement, specifying both the username (which is an empty string '') and the host:
DROP USER ''@'localhost';
You should see this confirmation:
Query OK, 0 rows affected (0.01 sec)
However, if your query returns an empty set like:
Empty set (0.00 sec)
This means there are no anonymous users in your database, which is already secure. In this case, if you try to drop a non-existent anonymous user, you'll get an error:
ERROR 1396 (HY000): Operation DROP USER failed for ''@'localhost'
This error is expected if no anonymous user exists, and you can safely proceed.
After checking for anonymous users and removing them if necessary, flush the privileges to ensure any changes are applied:
FLUSH PRIVILEGES;
Verify again that no anonymous users remain by running the SELECT query:
SELECT User, Host FROM mysql.user WHERE User = '';
This query should return an empty set, confirming that no anonymous users exist:
Empty set (0.00 sec)
You have successfully checked for and addressed a potential security vulnerability from your server. You can now exit the MySQL shell.
exit;