MySQL 在mysql中检索客户端IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6263453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:09:34  来源:igfitidea点击:

Retrieve client ip address in mysql

mysqlsql

提问by DaJunkie

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

我正在尝试使用简单的 SQL 语句获取客户端的 IP 地址。我不想使用 PHP 或其他技术。只有纯 SQL。当我使用

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.

但是我如何获得普通IP?非常感谢。

回答by MarkR

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

您只会获得与 MySQL 通信的客户端进程的 IP 地址。假设这是你想要的:

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 地址,通常不会)。

回答by Ali

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 SUBSTRING_INDEX(host,':',1) as 'ip' from information_schema.processlist WHERE ID=connection_id();

回答by filimonov

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

回答by Eric Beauchemin

@mvf - instead of reverse you could do:

@mvf - 你可以这样做而不是反向:

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