6

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 symbol is % -- why?

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
Edmhs
  • 3,645
  • 27
  • 39

7 Answers7

7

SELECT @@hostname;

--mysql 4.1 didn't have this one.

Joao Costa
  • 2,563
  • 1
  • 21
  • 15
3

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.

Joe Holt
  • 661
  • 5
  • 3
3

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
Nishant
  • 54,584
  • 13
  • 112
  • 127
3
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.

ajreal
  • 46,720
  • 11
  • 89
  • 119
2

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().

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 1
    select current_user(); returns user@10.0.3.% last simbol is % why ?? – Edmhs Jan 06 '11 at 14:11
  • Where your database is hosted. On your local machine or somewhere else on the network. And what type of network you are using. Is it virtual environment? – Naveed Jan 06 '11 at 14:18
2

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

cristian
  • 8,676
  • 3
  • 38
  • 44
  • this returns me 10.43.0.% with % in the end why? – Edmhs Jan 06 '11 at 14:13
  • % at the end means that you can connect from every ip in the range 10.43.0.1 .. 10.43.0.255 (i'm not sure, this is what i believe to be) – cristian Jan 06 '11 at 14:23
  • The "10.43.0.%" is part of your current user, it's what you have granted access to, and is stored in the mysql tables. (that is, user@10.43.0.% is the user as mysql knows it from its user tables, the IP part is not taken directly from the connection you have to the server) – nos Jan 06 '11 at 14:51
  • select @@hostname; is the solution – panofish Mar 24 '16 at 17:15
1

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                  |
+---------------------------------------+
dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • Note that this answer answers the originally asked question `Does Mysql have build in function to get host name?` and that the question and title have since been edited to a completely different question. – dotancohen Mar 24 '16 at 17:33