在 mysql 表中存储 IP 地址的理想数据类型是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8157324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:43:46  来源:igfitidea点击:

What is the ideal datatype to store IP address in a mysql table?

mysqlsql

提问by Robbie Dc

Possible Duplicate:
What type should I store IP addresses for MySQL?

可能的重复:
我应该为 MySQL 存储什么类型的 IP 地址?

One single datatype which can accommodate both IPv4 and IPv6 addresses. I want to store IP addresses to limit the number of profiles that a person can make from his/her machine in my website.

一种可以同时容纳 IPv4 和 IPv6 地址的单一数据类型。我想存储 IP 地址以限制一个人可以在我的网站上使用他/她的机器制作的配置文件数量。

采纳答案by knittl

IPv4 addresses use 32 bits (4 bytes), IPv6 addresses use 128 bits (16 bytes). Thus, you can store their binary representation in BINARY(16)or VARBINARY(16)fields.

IPv4 地址使用 32 位(4 字节),IPv6 地址使用 128 位(16 字节)。因此,您可以将它们的二进制表示存储在BINARY(16)VARBINARY(16)字段中。

Also see my answer to the question IP address storing in mysql database. It also provides comments why you would choose one over the other.

另请参阅我对问题IP 地址存储在 mysql 数据库中的回答。它还提供了为什么你会选择一个而不是另一个的评论。

回答by Gigamegs

A trie datatype would be optimal. There is uncompressed and compressed data struture from that type.

特里数据类型将是最佳的。有来自该类型的未压缩和压缩数据结构。