Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size for storing IPv4, IPv6 addresses as a string

Tags:

mysql

what should be the ideal size for storing IPv4, IPv6 addresses as a string in the MySQL database. should varchar(32) be sufficient?

like image 380
user339108 Avatar asked Aug 11 '10 04:08

user339108


People also ask

How long is IPv6 as a string?

IPv6 is written in hexadecimal notation, separated into 8 groups of 16 bits by the colons, thus (8 x 16 = 128) bits in total.

What is the size of IPv4 address and IPv6 address?

IPv6 increases the size of the IP address from the 32 bits that compose an IPv4 address to 128 bits.

What are the length bytes of addressing for IPv4 and IPv6?

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.

How much address space size does IPv6 use?

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.


2 Answers

Assuming textual representation in a string :

  • 15 characters for IPv4 (xxx.xxx.xxx.xxx format, 12+3 separators)
  • 45 characters for IPv6

Those are the maximum length of the string.

Alternatives to storing as string:

  • IPv4 is 32-bits, so a MySQL data type that can hold 4 bytes will do, using INT UNSIGNED is common along with INET_ATON and INET_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' 
  • 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.
like image 84
bakkal Avatar answered Oct 14 '22 15:10

bakkal


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.

like image 24
paxdiablo Avatar answered Oct 14 '22 14:10

paxdiablo