How to Display MySQL Users: A Beginner's Guide

How to Display MySQL Users: A Beginner's Guide

Main Point

This tutorial aims to explain how to display a list of all users in a MySQL database using SQL commands. It provides a straightforward approach for beginners to understand user management in MySQL.

Key Concepts

  • MySQL Users: In MySQL, users are accounts that can connect to the database, each with different permissions.
  • User Privileges: Users can be granted various levels of access to databases, tables, and other resources within MySQL.

How to Show Users

To view the list of MySQL users, you can use the following SQL command:

SELECT User, Host FROM mysql.user;

Explanation of the Command:

  • SELECT: Specifies the columns you want to retrieve from the database.
  • User: Displays the usernames of the MySQL users.
  • Host: Indicates the host from which the user is allowed to connect to the MySQL server.
  • FROM mysql.user: Specifies the table (user) in the mysql database where user information is stored.

Example

Output: The output will display a list of users along with their corresponding hosts, like this:

+-------+-----------+
| User  | Host      |
+-------+-----------+
| root  | localhost |
| user1 | %         |
+-------+-----------+

Execute the Command: After logging in, run the command to see the users:

SELECT User, Host FROM mysql.user;

Log in to MySQL: Use the command line or a MySQL client to log in:

mysql -u root -p

Conclusion

The SHOW USERS command is an essential tool for managing user accounts in MySQL. By understanding how to retrieve user information, beginners can better manage database permissions and security.