Edmhs
Edmhs

Reputation: 3731

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

Does MySQL have built in function to get host name?

Similar to

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

Edit:

select current_user(); //returns [email protected].%

Last symbol is % -- why?

Upvotes: 6

Views: 9047

Answers (7)

dotancohen
dotancohen

Reputation: 31511

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                  |
+---------------------------------------+

Upvotes: 1

Joe Holt
Joe Holt

Reputation: 711

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.

Upvotes: 3

Joao Costa
Joao Costa

Reputation: 2833

SELECT @@hostname;

--mysql 4.1 didn't have this one.

Upvotes: 7

Nishant
Nishant

Reputation: 55866

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

Upvotes: 3

ajreal
ajreal

Reputation: 47321

select current_user(); returns [email protected].% 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.

Upvotes: 3

cristian
cristian

Reputation: 8744

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

Upvotes: 2

Naveed
Naveed

Reputation: 42143

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

Upvotes: 2

Related Questions