what should be the ideal size for storing IPv4, IPv6 addresses as a string in the MySQL database. should varchar(32) be sufficient?
IPv6 is written in hexadecimal notation, separated into 8 groups of 16 bits by the colons, thus (8 x 16 = 128) bits in total.
IPv6 increases the size of the IP address from the 32 bits that compose an IPv4 address to 128 bits.
IPv4 addresses have 4 bytes (32 bits) whereas IPv6 has 16 bytes (128 bits) in length. These bytes are typically called octets and for the sake of readability, these bytes, bits, and octets are written in what's called dotted decimal.
The IPv6 address space is 128-bits (2128) in size, containing 340,282,366,920,938,463,463,374,607,431,768,211,456 IPv6 addresses. A bit is a digit in the binary numeral system, the basic unit for storing information.
Assuming textual representation in a string :
xxx.xxx.xxx.xxx
format, 12+3 separators)Those are the maximum length of the string.
Alternatives to storing as string:
INT UNSIGNED
is common along with INET_ATON
and INET_NTOA
to handle the conversion from address to number, and from number to addressSELECT INET_ATON('209.207.224.40'); -> 3520061480 SELECT INET_NTOA(3520061480); -> '209.207.224.40'
BIGINT
(8 bytes), this however will use two fields.If you're storing them as strings rather than bit patterns:
IPv4 addresses consist of four 3-digit decimal characters with three .
separators, so that only takes 15 characters such as 255.255.255.255
.
IPv6 addresses consist of eight 4-digit hex characters with seven :
separators, so that takes 39 characters such as 0123:4567:89ab:cdef:0123:4567:89ab:cdef
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With