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
.
Join Stack Overflow to learn, share knowledge, and build your career.
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
.
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.
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 4:22
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 : dba.stackexchange.com/questions/4614/…
– RolandoMySQLDBA
May 4 '14 at 11:43
DISTINCT
keyword: SELECT DISTINCT user FROM mysql.user;
– user2683246
Jan 25 '16 at 16:46
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;
host
come into play when working with mysql databases? [ Mysql Noob]
– Prabhakar Undurthi
Mar 12 '16 at 14:36
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 6:15
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;
user@host
format is used for setting passwords. Omitting the host from the SET PASSWORD
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
ORDER BY user
to it.
– sjas
May 22 '15 at 10:30
to avoid repetitions of users when they connect from different origin:
select distinct 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 |
| | % |
+------------------+-----------+
If you are referring to the actual MySQL users, try:
select User from mysql.user;
SELECT * FROM mysql.user;
It's a big table so you might want to be more selective on what fields you choose.
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
Login to mysql as root and type following query
select User from mysql.user;
+------+
| User |
+------+
| amon |
| root |
| root |
+------+
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 9: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...
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;
Peter and Jesse are correct but just make sure you first select the mysql DB.
use mysql;
select User from mysql.user;
that should do your trick
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
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
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 4:36
$> 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.
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;