Just came across this problem myself. As the comments suggest, the correct solution is to use INET6_NTOA
if you're on MySQL 5.6.3+. It handles IPv4 and IPv6 addresses, but the format for IPv4 is different to that produced by INET_ATON
(which only handles IPv4). The latter uses the same format as the PHP function ip2long
. Here's a reference table of equivalent functions:
PHP | MySQL
-----------|-------------
ip2long | INET_ATON
long2ip | INET_NTOA
inet_pton | INET6_ATON
inet_ntop | INET6_NTOA
Having said all that, it does appear to be possible to convert INET6_*
addresses using the INET_*
functions. As noted in this question you can convert from an IP string to binary format using this:
UNHEX(HEX(CAST(INET_ATON(ipaddress) AS UNSIGNED)))
-- same as
INET6_ATON(ipaddress)
With a bit of testing I figured out how to do the reverse:
INET_NTOA(CONV(HEX(ipaddress), 16, 10))
-- same as
INET6_NTOA(ipaddress)
Hopefully this is useful to someone.