mariadb 中的ip地址数据类型引入的较晚,相对于pg来说,本文主要来看下mariadb中的ip数据类型和使用示例。

IPv4 数据类型

从 mariadb 10.10 开始,引入了新的数据类型 INET4,用于存储 IPv4 地址,作为4字节的二进制字符串。

使用演示如下:

MariaDB [test]> create or replace table t1 (a inet4);
Query OK, 0 rows affected (0.032 sec)

MariaDB [test]> INSERT INTO t1 VALUES('0.0.0.0'), ('255.10.0.0'), ('255.255.255.255');
Query OK, 3 rows affected (0.005 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [test]> INSERT INTO t1 VALUES (0xa0000001), (0xf0000000), (0xff000001);
Query OK, 3 rows affected (0.001 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [test]> select hex(a), a from t1;
+----------+-----------------+
| hex(a)   | a               |
+----------+-----------------+
| 00000000 | 0.0.0.0         |
| FF0A0000 | 255.10.0.0      |
| FFFFFFFF | 255.255.255.255 |
| A0000001 | 160.0.0.1       |
| F0000000 | 240.0.0.0       |
| FF000001 | 255.0.0.1       |
+----------+-----------------+
6 rows in set (0.001 sec)

IPv4 函数

mariadb中提供了几个内置函数用于处理ip地址,如 is_ipv4, is_ipv4_compat, is_ipv4_mapped, inet_aton, inet_ntoa ,下面将逐一演示。

is_ipv4

is_ipv4函数用于验证地址有效性,如果表达式是有效的IPv4地址,则返回1,否则返回0。

在确定IPv4地址的有效性方面,IS_IPV4()比INET_ATON()严格,但与INET6_ATON()一样严格。 这意味着如果IS_IPV4返回1,当传递给INET_ATON()时,相同的表达式将始终返回非null结果,但反过来可能不适用。

演示如下:

MariaDB [test]> SELECT IS_IPV4('10.0.1.1');
+---------------------+
| IS_IPV4('10.0.1.1') |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.000 sec)

MariaDB [test]> SELECT IS_IPV4('1110.0.1.1');
+-----------------------+
| IS_IPV4('1110.0.1.1') |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set (0.000 sec)

is_ipv4_compat

如果给定的数字二进制字符串IPv6地址(如INET6_ATON()返回)是ipv4兼容的,则返回1,否则返回0。

演示如下:

MariaDB [test]> SELECT IS_IPV4_COMPAT(INET6_ATON('::10.0.1.1'));
+------------------------------------------+
| IS_IPV4_COMPAT(INET6_ATON('::10.0.1.1')) |
+------------------------------------------+
|                                        1 |
+------------------------------------------+
1 row in set (0.000 sec)

MariaDB [test]> SELECT IS_IPV4_COMPAT(INET6_ATON('::48f3::d432:1431:ba23:846f'));
+-----------------------------------------------------------+
| IS_IPV4_COMPAT(INET6_ATON('::48f3::d432:1431:ba23:846f')) |
+-----------------------------------------------------------+
|                                                         0 |
+-----------------------------------------------------------+
1 row in set (0.000 sec)

is_ipv4_mapped

如果给定的数字二进制字符串IPv6地址(如INET6_ATON()返回)是有效的ipv4映射地址,则返回1,否则返回0。

演示如下:

MariaDB [test]> SELECT IS_IPV4_MAPPED(INET6_ATON('::10.0.1.1'));
+------------------------------------------+
| IS_IPV4_MAPPED(INET6_ATON('::10.0.1.1')) |
+------------------------------------------+
|                                        0 |
+------------------------------------------+
1 row in set (0.000 sec)

MariaDB [test]> SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:10.0.1.1'));
+-----------------------------------------------+
| IS_IPV4_MAPPED(INET6_ATON('::ffff:10.0.1.1')) |
+-----------------------------------------------+
|                                             1 |
+-----------------------------------------------+
1 row in set (0.000 sec)

inet_aton

给定IPv4网络地址的点分隔四边形表示为字符串,返回一个表示该地址的数值的整数。 地址可以是4字节或8字节地址。 如果无法理解参数,则返回NULL。

演示如下:

MariaDB [test]> SELECT INET_ATON('192.168.1.1');
+--------------------------+
| INET_ATON('192.168.1.1') |
+--------------------------+
|               3232235777 |
+--------------------------+
1 row in set (0.000 sec)

计算过程为:

192 x 256^3 + 168 x 256^2 + 1 x 256 + 1

inet_ntoa

给定一个网络字节顺序(4或8字节)的数字IPv4网络地址,以字符串的形式返回该地址的点分隔四边形表示形式。

演示如下:

MariaDB [test]> SELECT INET_NTOA(3232235777);
+-----------------------+
| INET_NTOA(3232235777) |
+-----------------------+
| 192.168.1.1           |
+-----------------------+
1 row in set (0.000 sec)

小结

本文针对mariadb的ipv4相关内容逐一做了说明及演示。