如何在mysql命令中更改默认主机?

我想为特定用户显示授权,所以我输入这样的代码.

mysql> show grants for username@localhost;

但通常我只在localhost中使用mysql.如何设置默认主机名,以便我可以像这个表达式一样使用?

mysql> show grants for username;

最佳答案 你不能这样做.由于
SHOW GRANTS依赖于
GRANT语法本身,它将使用由MySQL定义的用户命名规则 – 并且它明确定义如果您没有在GRANT语句中为用户指定主机名部分 – 它将被视为%和否另一种方式:

Account Names and Passwords

The user value indicates the MySQL account to which the 07001
statement applies. To accommodate granting rights to users from
arbitrary hosts, MySQL supports specifying the user value in the form
user_name@host_name. If a user_name or host_name value is legal as an
unquoted identifier, you need not quote it. However, quotation marks
are necessary to specify a user_name string containing special
characters (such as “-”), or a host_name string containing special
characters or wildcard characters (such as “%”); for example,
‘test-user’@’%.com’. Quote the user name and host name separately.

You can specify wildcards in the host name. For example,
user_name@’%.example.com’ applies to user_name for any host in the
example.com domain, and user_name@’192.168.1.%’ applies to user_name
for any host in the 192.168.1 class C subnet.

和:

The simple form user_name is a synonym for user_name@’%’.

(来自相应的手册页).更具体地说明命名规则 – 请参阅此manual page(在定义帐户名称时,您会找到有关未指定主机名的相同答案)

点赞