Storing IP address in MySQL database (IPv4 AND IPv6)

前端 未结 5 2334
时光取名叫无心
时光取名叫无心 2021-02-15 17:54

Ok, now I\'m aware that similar questions have probably been asked a million times but I\'m a real novice at this and I\'d really appreciate your help here.

Basically, I

5条回答
  •  自闭症患者
    2021-02-15 18:57

    You are right but in MySQL 5.7 it is possible to store IPV6 in decimal format. also you can convert to ipv6 to decimal and vice versa:

    MySQL 5.0 IPV4

    select INET_ATON('192.168.0.1') returns int value: 3232235521

    MySQL 5.7 IPV6

    select cast(conv(substr(HEX(INET6_ATON('0:0:0:0:0:FFFF:C0A8:0001')), 1, 16), 16, 10) as decimal(65))*18446744073709551616 + 
           cast(conv(substr(HEX(INET6_ATON('0:0:0:0:0:FFFF:C0A8:0001')), 17, 16), 16, 10) as decimal(65)) 
    

    returns bigint value: 281473913978881

提交回复
热议问题