在MySQL中存储IPv6地址 [英] Storing IPv6 Addresses in MySQL

查看:61
本文介绍了在MySQL中存储IPv6地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如"具有ipv6功能的inet_aton和inet_ntoa函数 ",目前没有用于存储IPv6地址的MySQL函数.建议用于存储/插入的数据类型/功能是什么? (我不打算将它们存储为字符串).我也不想将IPv6地址分为2个INT.

As has been requested in "ipv6-capable inet_aton and inet_ntoa functions needed", there is currently no MySQL function for storing IPv6 addresses. What would be the recommended data type/function for storing/inserting? (I don't intend to store them as a string). I also don't want to separate the IPv6 address into 2 INT's.

推荐答案

如何:

BINARY(16)

那应该足够有效.

如该错误报告中所述,当前在MySQL服务器中没有将文本IPv6地址从二进制转换为二进制的功能.您要么需要在应用程序中执行此操作,要么可能需要在MySQL服务器中创建UDF(用户定义的函数).

Currently there is no function to convert textual IPv6 addresses from/to binary in the MySQL server, as noted in that bug report. You either need to do it in your application or possibly make a UDF (User-Defined Function) in the MySQL server to do that.

更新:

MySQL 5.6.3支持IPv6地址,请参见以下内容:"

MySQL 5.6.3 has support for IPv6 addresses, see the following: "INET6_ATON(expr)".

数据类型是VARBINARY(16),而不是我之前建议的BINARY(16).这样做的唯一原因是MySQL函数可同时用于IPv6和IPv4地址. BINARY(16)适用于仅存储IPv6地址并节省一个字节.同时处理IPv6和IPv4地址时,应使用VARBINARY(16).

The data type is VARBINARY(16) instead of BINARY(16) as I suggested earlier. The only reason for this is that the MySQL functions work for both IPv6 and IPv4 addresses. BINARY(16) is fine for storing only IPv6 addresses and saves one byte. VARBINARY(16) should be used when handling both IPv6 and IPv4 addresses.

MySQL和MariaDB的较早版本的实现,请参见以下内容:"使用IPV6功能扩展MYSQL 5 ".

An implementation for older versions of MySQL and MariaDB, see the following: "EXTENDING MYSQL 5 WITH IPV6 FUNCTIONS".

这篇关于在MySQL中存储IPv6地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆