在 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)
相关文章