如何在 MySQL 中使用命令行获取用户帐户列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1135245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a list of user accounts using the command line in MySQL?
提问by burntsugar
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?
我正在使用 MySQL 命令行实用程序并且可以浏览数据库。现在我需要查看用户帐户列表。我怎样才能做到这一点?
I'm using MySQL version 5.4.1.
我正在使用 MySQL 版本5.4.1。
回答by George Claghorn
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 Usercolumn if you'd only like to see unique usernames.
正如 Matthew Scharley在对此答案的评论中指出的那样,User如果您只想查看唯一的用户名,则可以按列分组。
回答by spkane
I find this format the most useful as it includes the host field which is important in MySQL to distinguish between user records.
我发现这种格式最有用,因为它包含主机字段,这在 MySQL 中对于区分用户记录很重要。
select User,Host from mysql.user;
回答by RolandoMySQLDBA
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;
回答by Nicolas Manzini
to avoid repetitions of users when they connect from different origin:
避免用户从不同来源连接时重复:
select distinct User from mysql.user;
回答by VPK
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:
MySQL 将用户信息存储在自己的数据库中。数据库的名称是MySQL. 在该数据库中,用户信息位于一个名为 的表中,即一个数据集user。如果要查看 MySQL 用户表中设置了哪些用户,请运行以下命令:
SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| root | localhost |
| root | demohost |
| root | 127.0.0.1 |
| debian-sys-maint | localhost |
| | % |
+------------------+-----------+
回答by Jesse Vogt
If you are referring to the actual MySQL users, try:
如果您指的是实际的 MySQL 用户,请尝试:
select User from mysql.user;
回答by Etzeitet
SELECT * FROM mysql.user;
It's a big table so you might want to be more selective on what fields you choose.
这是一张大表,因此您可能希望对选择的字段更具选择性。
回答by sandip divekar
Login to mysql as root and type following query
以 root 身份登录 mysql 并键入以下查询
select User from mysql.user;
+------+
| User |
+------+
| amon |
| root |
| root |
+------+
回答by Brad Dre
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.db 表在确定用户权限方面可能更重要。我认为如果您在 GRANT 命令中提到一个表,就会在其中创建一个条目。在我的情况下,当 mysql.users 表显然能够连接和选择等时,它没有显示用户的权限。
mysql> select * from mysql.db;
mysql> select * from db;
+---------------+-----------------+--------+-------------+-------------+-------------+--------
| Host | Db | User | Select_priv | Insert_priv | Update_priv | Del...
回答by Tobias Holm
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;

