在 MySQL 中存储 IP 地址的最有效方法 [英] Most efficient way to store IP Address in MySQL

查看:28
本文介绍了在 MySQL 中存储 IP 地址的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 中存储和检索 IP 地址的最有效方法是什么?现在我正在做:

What is the most efficient way to store and retrieve IP addresses in MySQL? Right now I'm doing:

SELECT * FROM logins WHERE ip = '1.2.3.4'

其中 ip 是 VARCHAR(15) 字段.

Where ip is a VARCHAR(15) field.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

对于 IPv4 地址,您可能想要存储它们作为 int unsigned 并使用 <代码>INET_ATON()INET_NTOA() 函数从数值返回 IP 地址,反之亦然.

For IPv4 addresses, you may want to store them as an int unsigned and use the INET_ATON() and INET_NTOA() functions to return the IP address from its numeric value, and vice versa.

示例:

SELECT INET_ATON('127.0.0.1');

+------------------------+
| INET_ATON('127.0.0.1') |
+------------------------+
|             2130706433 | 
+------------------------+
1 row in set (0.00 sec)


SELECT INET_NTOA('2130706433');

+-------------------------+
| INET_NTOA('2130706433') |
+-------------------------+
| 127.0.0.1               | 
+-------------------------+
1 row in set (0.02 sec)

这篇关于在 MySQL 中存储 IP 地址的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆