๐Ÿ‘ค MySQL Create / Drop / Show Users โ€“ Manage Database Access Accounts


๐Ÿงฒ Introduction โ€“ Why Manage MySQL Users?

In MySQL, users control who can access what within the database. Managing users correctly is crucial for:

  • ๐Ÿ”’ Securing your data
  • ๐ŸŽฏ Assigning appropriate roles and privileges
  • ๐Ÿ‘ฅ Supporting multi-user access with custom permissions

Whether you’re running a production system, test environment, or development stack, managing users ensures only authorized people or applications can access your database.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to create, show, and delete MySQL users
  • Syntax and examples for secure user management
  • Best practices for user-based security

โž• CREATE USER โ€“ Add a New Account

๐Ÿ”น Syntax

CREATE USER 'username'@'host' IDENTIFIED BY 'password';
  • 'username': The account name
  • 'host': The origin of connection ('localhost', '%', or IP)
  • 'password': Password for authentication

๐Ÿ”น Example

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongP@ssw0rd!';

โœ… Creates a user app_user that can only connect from the local server.


๐Ÿ”ธ Create a User for Remote Access

CREATE USER 'reporting'@'%' IDENTIFIED BY 'ViewOnly123!';

โœ… Allows login from any host (use cautiously in production environments).


๐Ÿ‘๏ธ SHOW USERS โ€“ List Existing Accounts

MySQL does not have a direct SHOW USERS command, but you can query the internal mysql.user table:

SELECT user, host FROM mysql.user;

๐Ÿ”น Example Output

userhost
rootlocalhost
app_userlocalhost
reporting%

โœ… Use this to audit all created users and their connection scopes.


โŒ DROP USER โ€“ Remove an Account

๐Ÿ”น Syntax

DROP USER 'username'@'host';

๐Ÿ”น Example

DROP USER 'reporting'@'%';

โœ… Completely removes the user and all their privileges.


๐Ÿ” Additional Account Management

๐Ÿ”น Rename User

RENAME USER 'old_user'@'localhost' TO 'new_user'@'localhost';

๐Ÿ”น Change Password

ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'NewStrongerP@ss!';

โœ… Use this for password rotation or recovery.


๐Ÿ“˜ Best Practices

โœ… Practice๐Ÿ’ก Why It Matters
Always specify exact host ('localhost', '%')Prevents accidental wide access
Use strong, unique passwordsProtects against brute-force or credential reuse
Create one user per application/moduleEnables precise privilege management
Donโ€™t use root in production appsLimits damage in case of compromise
Revoke and drop unused accountsMinimizes potential attack vectors

๐Ÿš€ Real-World Use Cases

ScenarioCommand Used
Set up an application DB userCREATE USER 'app_user'@'localhost' IDENTIFIED BY ...
Grant read-only access to analystGRANT SELECT ON db.* TO 'analyst'@'%';
Remove user from previous projectDROP USER 'old_dev'@'localhost';
Audit all users in productionSELECT user, host FROM mysql.user;

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Creating, listing, and deleting users is essential to secure, scalable, and maintainable MySQL operations. It allows for customized access control and clear accountability.

๐Ÿ” Key Takeaways

  • Use CREATE USER to add new accounts with defined host and password
  • Use DROP USER to delete unused or outdated accounts
  • Use SELECT FROM mysql.user to list existing users
  • Always apply the principle of least privilege

โš™๏ธ Real-World Relevance

User management is central in multi-team development, DevOps automation, cloud database security, and enterprise data governance.


โ“ FAQ โ€“ MySQL User Commands


โ“ How do I list all users in MySQL?

SELECT user, host FROM mysql.user;

โ“ What does '%' mean in 'username'@'%'?

It allows the user to connect from any host or IP address.


โ“ Can I reuse the same username for different hosts?

โœ… Yes. 'admin'@'localhost' and 'admin'@'%' are distinct users.


โ“ How do I change a userโ€™s password?

ALTER USER 'username'@'host' IDENTIFIED BY 'NewPass123!';

โ“ How do I prevent remote root access?

UPDATE mysql.user SET host = 'localhost' WHERE user = 'root';
FLUSH PRIVILEGES;

Share Now :
Share

๐Ÿ‘ค MySQL Create / Drop / Show Users

Or Copy Link

CONTENTS
Scroll to Top