将旧的 INET_ATON 值转换为新的二进制 INET6_ATON 值,无需 INET6_ATON / INET6_NTOA
我们在表中已有数据,字段类型为UNSIGNED INT
其中保存了使用以下命令创建的 IPv4 数据INET_ATON()
.
但现在我们转向INET6_ATON()
在查询中并希望直接迁移数据,而不需要为转换创建额外的数据库字段。
所以我将字段类型从UNSIGNED INT
to VARBINARY(16)
.
新数据存储为二进制值INET6_ATON()
.
但是我们如何转换旧数据呢?
我已经尝试将现有值转换为整数并将它们转换为HEX
。这在保存时给了我相同的十六进制字符串INET6_ATON()
.
我是否错过了某些步骤或某些文字?
更新访客 SET ip = HEX(CAST(ip AS UNSIGNED))
二进制数据不一样。在这种情况下,它被保存为十六进制值。使用BIN()
, CONVERT()
and CAST()
没有帮助。
示例数据:
unsigned int 字段中的旧 ip 值:
2130706433 ( = ip2long('127.0.0.1') )
旧值如 varbinary 字段所示:
32313330373036343333 ( = 2130706433 )
新值如 varbinary 字段所示:
7f000001 ( = INET6_ATON('127.0.0.1') )
我们不能直接使用INET6_ATON()
/ INET6_NTOA()
用于转换。
我们是否应该使用 PHP 函数转换每一行的数据inet_top()
and inet_pton()
或者是否有一个纯粹的 SQL 解决方案,不需要一些 UDF,以便立即更新所有行?
似乎已经有一个类似的问题,但没有解决方案,并且评论中提到的解决方案产生错误的数据,并且没有提供一些工作示例代码:无需 INET6_ATON() 将 IPv6 转换为二进制 https://stackoverflow.com/questions/31226670/convert-ipv6-to-binary-without-inet6-aton#comment50461738_31226670
参考:
http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-aton http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-aton
http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-ntoa http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-ntoa
http://php.net/manual/en/function.inet-pton.php http://php.net/manual/en/function.inet-pton.php
http://php.net/manual/en/function.inet-ntop.php http://php.net/manual/en/function.inet-ntop.php