How do you see which users exist in MySQL and what they're allowed to do?

BeginnerHands-onMySQL
Answer

Short answer

Query the mysql.user table for accounts, and use SHOW GRANTS FOR 'user'@'host' to see each user's permissions.

Commands

SELECT user, host, plugin, account_locked FROM mysql.user;
-- +-----------+-----------+-----------------------+
-- | user      | host      | plugin                |
-- | root      | localhost | auth_socket           |
-- | shop_app  | 10.0.%    | caching_sha2_password |

SHOW GRANTS FOR 'shop_app'@'10.0.%';
-- GRANT SELECT, INSERT, UPDATE, DELETE ON `shop`.* TO `shop_app`@`10.0.%`

SHOW GRANTS;                      -- my own permissions
SELECT CURRENT_USER();            -- which account I actually matched

Why CURRENT_USER() matters

You can log in as shop_app but match a different account row (e.g. an anonymous ''@'localhost') and get unexpected permissions. CURRENT_USER() shows the real match.

Security review checklist

  • No users with empty passwords.
  • Remote root disabled (root@'%' shouldn't exist).
  • App users don't have ALL PRIVILEGES or GRANT OPTION.
  • Anonymous users removed (mysql_secure_installation does this).

What to say in the interview

"I list accounts from mysql.user, check each with SHOW GRANTS, and use CURRENT_USER to see which account a session matched. In audits I look for remote root, empty passwords and over-privileged app users."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL