我应该为MySQL存储哪种类型的IP地址? [英] What type should I store IP addresses for MySQL?
本文介绍了我应该为MySQL存储哪种类型的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我打算使用varchar(20)
,但是我想知道如果我应该做INT并去除句点该怎么办.哪个会更好,为什么呢?
I was going to use varchar(20)
, but I was wondering what should if I should do INT and strip off the periods instead. What would be better and why?
推荐答案
我认为您仅对IPv4地址感兴趣,而对IPv6不感兴趣.
I presume you're only interested in IPv4 addresses, not IPv6.
我会在列中使用INT UNSIGNED
,然后使用INET_ATON
和INET_NTOA
在文本表示形式和int值之间来回转换.
I would use an INT UNSIGNED
for the column, and then use INET_ATON
and INET_NTOA
to convert back and forth between the textual representation and the int value.
mysql> SELECT INET_ATON('192.168.10.50');
+----------------------------+
| INET_ATON('192.168.10.50') |
+----------------------------+
| 3232238130 |
+----------------------------+
1 row in set (0.00 sec)
mysql> SELECT INET_NTOA(3232238130);
+-----------------------+
| INET_NTOA(3232238130) |
+-----------------------+
| 192.168.10.50 |
+-----------------------+
1 row in set (0.00 sec)
这篇关于我应该为MySQL存储哪种类型的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文