1409

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
burntsugar
  • 57,360
  • 21
  • 58
  • 81
  • @Mustapha Why the title change? This answers here are SQL you can run from anywhere, not just a command line. And what does that tag add to the question? – Rup May 14 '19 at 09:13
  • The purpose of my edit was to maintain consistency between the title and the description, but I think you have a good point. Make your edit Mr. @Rup – mhadidg May 14 '19 at 13:11

16 Answers16

1888

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
George Claghorn
  • 26,261
  • 3
  • 48
  • 48
  • 47
    I think it may be necessary to group on `User` too, to only get unique user values, since there's a seperate row for each `user`@`host` entry. – Matthew Scharley Jul 16 '09 at 04:22
  • 5
    How to find the same info without sql query ?? – Divyanshu Das Oct 21 '13 at 12:20
  • 8
    @barrycarter `DELETE FROM mysql.user;` better have `WHERE user='someuser' and host='somehost';` If you do `DELETE FROM mysql.user;`, all users are gone. Logins after the next mysql restart or `FLUSH PRIVILEGES;` eliminate users from memory. Here is an example of one of my posts on doing `DELETE FROM mysql.user` responsibly : http://dba.stackexchange.com/questions/4614/cannot-drop-anonymous-user-from-mysql-user/4615#4615 – RolandoMySQLDBA May 04 '14 at 11:43
  • @RolandoMySQLDBA I probably should've added a smiley face to my last comment :) I was joking, and wondering if deleting all users would delete ALL access to the db, including the access to restore users (ie, pulling the rug out from under your own feet). –  May 04 '14 at 14:47
  • Is it also possible to view the tables each user has rights to? – Geoffrey Jan 29 '15 at 09:06
  • 3
    @Geoffrey `SHOW GRANTS FOR 'user'@'host';` – fancyPants Apr 28 '15 at 08:13
  • 5
    instead of grouping you can just use `DISTINCT` keyword: `SELECT DISTINCT user FROM mysql.user;` – user2683246 Jan 25 '16 at 16:46
  • 1
    @MatthewScharley: Arguably grouping is wrong because those _are_ distinct users. – Lightness Races in Orbit Jun 16 '16 at 18:45
459

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;
spkane
  • 6,177
  • 2
  • 18
  • 18
  • 1
    Just curious. When will this `host` come into play when working with mysql databases? [ Mysql Noob] – Prabhakar Mar 12 '16 at 14:36
  • 7
    @Packer the `host` comes into play when you are connecting from a different server. It is possible to grant different access to `'packer'@'example.com'` and `'packer'@'google.com'` – Ray Baxter Apr 25 '16 at 06:15
126

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;
RolandoMySQLDBA
  • 43,883
  • 16
  • 91
  • 132
  • 7
    This is basically the same as [spkane's answer](http://stackoverflow.com/a/11367654/111424). What is the benefit of concatenating the user and host columns? – Iain Samuel McLean Elder Oct 14 '13 at 21:00
  • 8
    One example: the `user@host` format is used for setting passwords. Omitting the host from the [`SET PASSWORD`](http://dev.mysql.com/doc/refman/5.0/en/set-password.html) command produces an error. `SET PASSWORD FOR wordpressuser = PASSWORD('...');` produces the error `ERROR 1133 (42000): Can't find any matching row in the user table`. Include the host and it works. `SET PASSWORD FOR wordpressuser@localhost = PASSWORD('...');` produces `Query OK, 0 rows affected (0.00 sec)`. – Iain Samuel McLean Elder Oct 14 '13 at 21:21
  • 3
    Best answer, whatever the naysayers may complain about. Only thing i'd change were appending an `ORDER BY user` to it. – sjas May 22 '15 at 10:30
52

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

select distinct User from mysql.user;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
33

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 |
|                  | %         |
+------------------+-----------+
VPK
  • 3,010
  • 1
  • 28
  • 35
31

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

select User from mysql.user;
Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
24
SELECT * FROM mysql.user;

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

Etzeitet
  • 1,995
  • 2
  • 18
  • 22
  • 2
    I got 3 root users with different hosts. `localhost`, `127.0.0.1` and `::1`. Which one must I keep and what must I delete? Thanks! – emotality Jan 28 '15 at 12:03
  • 4
    If you don't want people to connect via the network, the old standard was to delete all of these. Nowadays, though, it seems the recommendation is to keep localhost ones, as they are not accessible over the network anyway; this means you should instead *keep* all of them. – trysis Mar 21 '15 at 13:52
19

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

select User from mysql.user;

+------+
| User |
+------+
| amon |
| root |
| root |
+------+
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sandip divekar
  • 1,628
  • 5
  • 22
  • 40
  • I'd say +1 for mentioning logging in as root. I tried without doing so and it didn't work ;) – leenephi Mar 25 '14 at 22:27
  • You need to give privileges to user1 to display user list. Without root user you will get error. So first provide the privileges. Log in as root user then type command `GRANT SELECT ON mysql.user TO 'user1'@'localhost';` now login as user1 and type command `select User from mysql.user;` You will see user list displayed. :) +1 Enjoy – sandip divekar Mar 26 '14 at 09:16
16

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...
Palec
  • 12,743
  • 8
  • 69
  • 138
Brad Dre
  • 3,580
  • 2
  • 19
  • 22
13

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;
Tobias Holm
  • 251
  • 2
  • 6
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Armin Nehzat
  • 418
  • 6
  • 13
  • 23
    It's not necessery to `use mysql;` in case you scope the table to the mysql database like you did. You can just `select User from mysql.user;` – vitaLee Mar 26 '13 at 19:45
  • 5
    Adding `use mysql;` is just so you can use `select User from user;` instead `select User from mysql.user;` since it is usually a one time query, there is no need to use the mysql db – Dan Oct 24 '13 at 11:25
  • 1
    after `use mysql` if you had used `select user from user;` than that could have been something, but instead you are using `mysql.user`, which makes using `use mysql` at the beginning unnecessary. – Sнаđошƒаӽ Feb 13 '16 at 04:36
12

This displays the list of unique users:

SELECT DISTINCT User FROM mysql.user;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikhil Chavda
  • 141
  • 1
  • 4
6
$>  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
4

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;
Community
  • 1
  • 1
Alper t. Turker
  • 34,230
  • 9
  • 83
  • 115
1
SELECT User FROM mysql.user;

Use the above query to get the MySQL users.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arun Karnawat
  • 575
  • 10
  • 21
1

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;
Javier G.Raya
  • 230
  • 1
  • 3
  • 15