开发者

Why does the MySQL built-in function 'current_user()' return a hostname containing a '%' symbol?

开发者 https://www.devze.com 2023-02-03 08:36 出处:网络
Does MySQL have built in function to get host name? Similar to select user(); //this returnsuser@userip

Does MySQL have built in function to get host name?

Similar to

select user(); //this returns  user@userip

Edit:

select current_user(); //returns user@10.0.3.%

Last sym开发者_StackOverflowbol is % -- why?


SELECT @@hostname;

--mysql 4.1 didn't have this one.


select current_user(); returns user@10.0.3.% last simbol is % why ??

the % is the record in mysql.user that match your current login

which can be derived from

select concat(user, '@', host) from mysql.user;

the % is determined by host value.


wouldn't his work?

select substring_index(user(),'@', -1) as hostname;

The above is wrong, it returns the user's IP not host's. I was fooled by testing on local. Sorry about that.


I guess this returns host name, but this wouldn't be useful unless you are ready to grep, pipe and cut Just a FYI:

C:\>mysqladmin -u username -pmypassword -h dev.naishelabs.com version

mysqladmin  Ver 8.41 Distrib 5.0.22, for Win32 on ia32
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version          5.0.77
Protocol version        10
Connection              dev.naishelabs.com via TCP/IP
TCP port                3306
Uptime:                 73 days 5 hours 7 min 45 sec


If you want the hostname of the database server, you can use SHOW VARIABLES:

mysql> SHOW VARIABLES LIKE 'hostname';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname      | munda |
+---------------+-------+
1 row in set (0.00 sec)

It's not a built-in function so it can't be used in a SELECT statement.


Are you looking for CURRENT_USER function.

Returns the user name and host name combination for the MySQL account that the server used to authenticate the current client. This account determines your access privileges.

The value of CURRENT_USER() can differ from the value of USER().


You can use user() and current_user() functions. If you want only hostname do something like select substr(current_user(),LOCATE('@', current_user())+1) AS localhost;
You can find details here


The @@hostname variable contains the system hostname:

$ cat /etc/hostname
bruno

$ hostname
bruno

$ mysql
mysql> SELECT @@hostname;
+------------+
| @@hostname |
+------------+
| bruno      |
+------------+

Note that this can be combined and used in other queries:

mysql> SELECT name, @@hostname FROM people;
+-------+-------------+
| name  | @@hostname  |
+-------+-------------+
| Dotan | bruno       |
+-------+-------------+

mysql> SELECT CONCAT('I am on server ', @@hostname);
+---------------------------------------+
| CONCAT('I am on server ', @@hostname) |
+---------------------------------------+
| I am on server bruno                  |
+---------------------------------------+
0

精彩评论

暂无评论...
验证码 换一张
取 消