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
rootdisabled (root@'%'shouldn't exist). - App users don't have
ALL PRIVILEGESorGRANT OPTION. - Anonymous users removed (
mysql_secure_installationdoes 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."
More on MySQL
How do you set up MySQL and log in to it?BeginnerHow do you create a database, a table and see data in MySQL?BeginnerHow do you create a MySQL user for an application and give it permissions?BeginnerHow do you back up and restore a MySQL database?BeginnerWhat is a relational database?BeginnerWhat is the difference between a database, a table, a row and a column?Beginner