将IPv4,IPv6地址存储为字符串的大小 [英] Size for storing IPv4, IPv6 addresses as a string
本文介绍了将IPv4,IPv6地址存储为字符串的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MySQL数据库中以字符串形式存储IPv4,IPv6地址的理想大小应该是多少. varchar(32)是否足够?
what should be the ideal size for storing IPv4, IPv6 addresses as a string in the MySQL database. should varchar(32) be sufficient?
推荐答案
假定字符串中的文本表示形式:
-
IPv4的
- 15个字符(
xxx.xxx.xxx.xxx
格式,12 + 3 分隔符) -
45个字符
- 15 characters for IPv4 (
xxx.xxx.xxx.xxx
format, 12+3 separators) - 45 characters for IPv6
这些是字符串的最大长度.
Those are the maximum length of the string.
作为字符串存储的替代方法:
- IPv4是32位,因此可以容纳4个字节的MySQL数据类型可以使用,
INT UNSIGNED
与INET_ATON
和INET_NTOA
共同使用可以处理从地址到数字以及从数字到数字的转换.地址
- IPv4 is 32-bits, so a MySQL data type that can hold 4 bytes will do, using
INT UNSIGNED
is common along withINET_ATON
andINET_NTOA
to handle the conversion from address to number, and from number to address
SELECT INET_ATON('209.207.224.40');
-> 3520061480
SELECT INET_NTOA(3520061480);
-> '209.207.224.40'
- 不幸的是,对于IPv6,MySQL的数据类型不是16个字节,但是可以将IPv6转换为规范形式,然后将其分成2个
BIGINT
(8个字节),但是它将使用两个字段. - For IPv6, unfortunately MySQL does not have a data type that is 16 bytes, however one can put the IPv6 into a canonical form, then separate them into 2
BIGINT
(8 bytes), this however will use two fields.
这篇关于将IPv4,IPv6地址存储为字符串的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文