我必须将IPv4 / v6地址和网络(CIDR表示法)存储在MySQL数据库中.不幸的是,我被限制使用Mysql.
The inet type holds an IPv4 or IPv6 host address, and optionally its
subnet, all in one field. The subnet is represented by the number of
network address bits present in the host address (the “netmask”). If
the netmask is 32 and the address is IPv4, then the value does not
indicate a subnet, only a single host. In IPv6, the address length is
128 bits, so 128 bits specify a unique host address.
The input format for this type is address/y where address is an IPv4
or IPv6 address and y is the number of bits in the netmask. If the /y
portion is missing, the netmask is 32 for IPv4 and 128 for IPv6, so
the value represents just a single host. On display, the /y portion is
suppressed if the netmask specifies a single host.
我也知道以下问题“Storing IP address”,但它不涉及网络CIDR表示法.
如何在Mysql 5.5中实现等效的inet类型?
> 3列:网络(2xBIGINT),CIDR(TINYINT)?
>字符串? (@Phil)
解决方法:
第一:MySQL中没有相当于PostgreSql的inet类型.
您应该只使用字符串类型来存储所有类型的IP地址.
如果您需要以特定格式使用它们,请在将它们插入数据库之前进行转换.
如果你需要用它们进行计算,正如你提到的INET_ATON()和INET_NTOA()是需要使用的函数.
标签:mysql,database-design,datatypes,network
来源: https://codeday.me/bug/20190806/1598822.html