Store both IPv4 and IPv6 address in one column

I want to be able to store both IPv4 and IPv6 addresses in my table. What is the most efficient way to store a user's IP address, whether it is an IPv4 or IPv6 address?

This will be used in a production environment, therefore suggestions regarding future evidence are preferred.

+7
mysql ipv4 ipv6
source share
2 answers

I would recommend storing each address in IPv6 format. There is an official mapping for this: IPv4-address IPv6 with mapping . It works as follows:

Take, for example, the IPv4 address 192.0.2.44
The IPv4 mapped IPv6 address will be ::ffff:192.0.2.44
Which can also be written as ::ffff:c000:022c ( 192 decimal c0 hexadecimal, etc.)

You can use the inet_pton() function to parse such addresses, and on my local system, the inet_ntop() function also outputs them in the most readable format ( ::ffff:192.0.2.44 ). Thus, you have only one format for working in your application.

Also see this related answer .

+7
source share

Why should it be one column? A few suggestions ...

You have 2 columns, one for IPv4, one for IPv6.

Store the IP address in one column and enter another column that basically contains a boolean, whether the IPv4 address or not ...

+1
source share

All Articles