burntsugar
burntsugar

Reputation: 58080

How can I get a list of user accounts using the command line in MySQL?

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.

Upvotes: 1427

Views: 1465525

Answers (17)

Tamil 07
Tamil 07

Reputation: 1

I can get the password with this query

SELECT username, password enter code hereFROM mysql.user;

But the passwords are encrypted, what type of encryption is that and how to decrypt using dictionary attack which is best tool

Upvotes: 0

Javier G.Raya
Javier G.Raya

Reputation: 250

To see your users, it would be to use the mysql database.

USE mysql;

And then make the select.

SELECT user,host FROM user;

Another option is to put the BD.Table.

For example :

SELECT user,host FROM mysql.user;

Upvotes: 1

sandip divekar
sandip divekar

Reputation: 1836

Log in to MySQL as root and type the following query:

select User from mysql.user;

+------+
| User |
+------+
| amon |
| root |
| root |
+------+

Upvotes: 19

Arun Karnawat
Arun Karnawat

Reputation: 581

SELECT User FROM mysql.user;

Use the above query to get the MySQL users.

Upvotes: 1

drmaa
drmaa

Reputation: 3684

$>  mysql -u root -p -e 'Select user from mysql.user' > allUsersOnDatabase.txt

Executing this command on a Linux command line prompt will first ask for the password of MySQL root user. On providing the correct password it will print all the database users to the text file.

Upvotes: 6

Nikhil Chavda
Nikhil Chavda

Reputation: 141

This displays the list of unique users:

SELECT DISTINCT User FROM mysql.user;

Upvotes: 12

Nicolas Manzini
Nicolas Manzini

Reputation: 8546

To avoid repetitions of users when they connect from a different origin:

select distinct User from mysql.user;

Upvotes: 52

Armin Nehzat
Armin Nehzat

Reputation: 418

Peter and Jesse are correct, but just make sure you first select the "mysql" database.

use mysql;
select User from mysql.user;

That should do your trick.

Upvotes: 12

George Claghorn
George Claghorn

Reputation: 26545

Use this query:

SELECT User FROM mysql.user;

Which will output a table like this:

+-------+
| User  |
+-------+
| root  |
+-------+
| user2 |
+-------+

As Matthew Scharley points out in the comments on this answer, you can group by the User column if you'd only like to see unique usernames.

Upvotes: 1905

Alper t. Turker
Alper t. Turker

Reputation: 35249

I found his one more useful as it provides additional information about DML and DDL privileges

SELECT user, Select_priv, Insert_priv , Update_priv, Delete_priv, 
       Create_priv, Drop_priv, Shutdown_priv, Create_user_priv 
FROM mysql.user;

Upvotes: 4

VPK
VPK

Reputation: 3090

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 |
|                  | %         |
+------------------+-----------+

Upvotes: 35

Tobias Holm
Tobias Holm

Reputation: 261

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;

Upvotes: 14

Brad Dre
Brad Dre

Reputation: 3866

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...

Upvotes: 17

RolandoMySQLDBA
RolandoMySQLDBA

Reputation: 44363

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;

Upvotes: 127

spkane
spkane

Reputation: 6764

I find this format the most useful as it includes the host field which is important in MySQL to distinguish between user records.

select User,Host from mysql.user;

Upvotes: 468

Etzeitet
Etzeitet

Reputation: 2125

SELECT * FROM mysql.user;

It's a big table so you might want to be more selective on what fields you choose.

Upvotes: 24

Jesse Vogt
Jesse Vogt

Reputation: 16539

If you are referring to the actual MySQL users, try:

select User from mysql.user;

Upvotes: 31

Related Questions