Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve client ip address in mysql

Tags:

sql

mysql

I'm trying to get with a simple SQL statement the IP address of the client. I do not want to use PHP or other techniques. Only pure SQL. When I use

SELECT USER();

I get

[email protected]

When I use

SELECT CURRENT_USER();

I get

dbouser@%

But how do I get the plain IP? Thanks a lot in advance.

like image 586
DaJunkie Avatar asked Sep 03 '25 15:09

DaJunkie


2 Answers

You will only get the IP address of the client process communicating with MySQL. Assuming this is what you want:

select host from information_schema.processlist WHERE ID=connection_id();

Will give you the host name (or IP address if name resolution is not enabled, which it is usually not) connecting to the mysql server on the current connection.

like image 72
MarkR Avatar answered Sep 05 '25 06:09

MarkR


To get the IP address only without the port number.

select SUBSTRING_INDEX(host,':',1) as 'ip' from information_schema.processlist WHERE ID=connection_id();

like image 34
Ali Avatar answered Sep 05 '25 05:09

Ali