Mysql:从二进制 ip 和子网获取 IPv6 二进制网络掩码

标签 mysql mariadb ipv6 bitwise-operators

我有一个表将 IPv6 地址存储为 BINARY(16) 并将子网存储为 INT(3)

我认为通过以下方式获取网络掩码会很简单:

SELECT ~INET6_ATON('::') << (128 - subnet);

但这会返回零,事实上,当我在二进制字符串上使用它们时,所有按位运算符似乎都提供整数结果:-(

我正在使用 10.1.30-MariaDB

+-----------------+
| VERSION()       |
+-----------------+
| 10.1.30-MariaDB |
+-----------------+

非常感谢任何帮助。

编辑:我完全误解了 Maria 的版本字符串,抱歉 :-(

最佳答案

5.5 中的位操作仅限于 64 位。 (8.0放宽了限制。 5.5 已经很老了。如果升级,要么转储并重新加载,要么分 3 步升级:5.5->5.6->5.7->8.0)

您可能想要 shift >> .或者也许 (1 << amt) - 1 .示例(仅使用 64 位算法):

SELECT HEX(~((1 << 8) - 1)); --> FFFFFFFFFFFFFF00

一些 128 位操作在来自 http://mysql.rjweb.org/doc.php/ipranges 的“IPv6 引用实现”链接中可用。 .没有“转移”功能,但您可以调整技术(使用 HEX() )来实现您的目的。它确实对 IPv6 值加/减 1。这对于某些掩码构建和掩码操作很方便。

如果您想解释您将如何处理 SELECT 的结果,我也许可以给你更多的答案。

(在 5.7.11 发行说明中找到):

Bit functions and operators comprise BIT_COUNT(), BIT_AND(), BIT_OR(), BIT_XOR(), &, |, ^, ~, <<, and >>. Currently, bit functions and operators require BIGINT (64-bit integer) arguments and return BIGINT values, so they have a maximum range of 64 bits. Arguments of other types are converted to BIGINT and truncation might occur.

An extension for MySQL 8.0 changes this cast-to-BIGINT behavior: Bit functions and operators permit binary string type arguments (BINARY, VARBINARY, and the BLOB types), enabling them to take arguments and produce return values larger than 64 bits. Consequently, bit operations on binary string arguments in MySQL 5.7 might produce different results in MySQL 8.0. To provide advance notice about this potential change in behavior, the server now produces warnings for bit operations for which binary string arguments are not converted to integer in MySQL 8.0. These warnings afford an opportunity to rewrite affected statements. To explicitly produce MySQL 5.7 behavior in a way that will not change after an upgrade to 8.0, cast bit-operation binary string arguments to convert them to integer. For more information and examples, see Bit Functions and Operators.

关于Mysql:从二进制 ip 和子网获取 IPv6 二进制网络掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139818/

相关文章:

mysql - elasticsearch jdbc导入数据包含json

php - MySQL 到 JSON,如何获得正确的格式?

mysql - 如何将多行合并为一行

node.js - 如何解决同一台linux服务器上8小时mariadb和nodejs的时差

compression - IPv6地址压缩

PHP/MYSQL - 从下拉菜单中获取数据库值,然后放入同一页面的表中

java - Android Studio 无法找到显式 Activity 类

mysql 5.5 到 mariadb 10.2.13 : 4000% increase in execution time for select

c - IPv6 绑定(bind)失败

c# - 无法使用单声道连接到在 ubuntu 上运行的自己的 IPv6 TCP 服务器应用程序