简体   繁体   English

在 mysql 中检索客户端 ip 地址

[英]Retrieve client ip address in mysql

I'm trying to get with a simple SQL statement the IP address of the client.我正在尝试使用简单的 SQL 语句获取客户端的 IP 地址。 I do not want to use PHP or other techniques.我不想使用 PHP 或其他技术。 Only pure SQL.只有纯SQL。 When I use当我使用

SELECT USER();

I get我明白了

dbouser@host.i.do.not.care.of

When I use当我使用

SELECT CURRENT_USER();

I get我明白了

dbouser@%

But how do I get the plain IP?但是我如何获得普通的 IP? Thanks a lot in advance.提前非常感谢。

You will only get the IP address of the client process communicating with MySQL.您只会获得与 MySQL 通信的客户端进程的 IP 地址。 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.将在当前连接上为您提供连接到 mysql 服务器的主机名(或 IP 地址,如果未启用名称解析,通常不启用)。

To get the IP address only without the port number.仅获取不带端口号的 IP 地址。

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

SELECT REVERSE(SUBSTRING_INDEX(REVERSE(USER()),'@',1)) as ip;
SELECT SUBSTRING(USER(), LOCATE('@', USER())+1) as ip;

@mvf - instead of reverse you could do: @mvf - 而不是反向你可以这样做:

SELECT SUBSTRING_INDEX(USER(), '@', -1) AS ip;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM