Wednesday 24 October 2018

How to get a list of MySQL user accounts

I'm using the MySQL command line utility and can navigate through a database. Now I need to see a list of user accounts. How can I do this?

I'm using MySQL version 5.4.1.

Answers:
Use this query:
SELECT User FROM mysql.user;
Which will output a table like this:
+-------+
| User  |
+-------+
| root  |
+-------+
| user2 |
+-------+
You can group by the User column if you'd only like to see unique usernames.



A user account comprises the username and the host level access.
Therefore, this is the query that gives all user accounts
SELECT CONCAT(QUOTE(user),'@',QUOTE(host)) UserAccount FROM mysql.user;



If you are referring to the actual MySQL users, try:
select User from mysql.user;



MySQL stores the user information in its own database. The name of the database is MySQL. Inside that database, the user information is in a table, a dataset, named user. If you want to see what users are set up in the MySQL user table, run the following command:
SELECT User, Host FROM mysql.user;

+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| root             | localhost |
| root             | demohost  |
| root             | 127.0.0.1 |
| debian-sys-maint | localhost |
|                  | %         |
+------------------+-----------+



The mysql.db table is possibly more important in determining user rights. I think an entry in it is created if you mention a table in the GRANT command. In my case the mysql.users table showed no permissions for a user when it obviously was able to connect and select, etc.
mysql> select * from mysql.db;
mysql> select * from db;
+---------------+-----------------+--------+-------------+-------------+-------------+--------
| Host          | Db              | User   | Select_priv | Insert_priv | Update_priv | Del...



I use this to sort the users, so the permitted hosts are more easy to spot:
mysql> SELECT User,Host FROM mysql.user ORDER BY User,Host;



$>  mysql -u root -p -e 'Select user from mysql.user' > allUsersOnDatabase.txt
Executing this command on linux prompt will first ask for the password of mysql root user, on providing correct password it will print all the database users to the text file.

0 comments:

Post a Comment