• 请不要在回答技术问题时复制粘贴 AI 生成的内容
kisshere
V2EX  ›  程序员

用户 ip 字段,在 MySQL 中到底该以何种类型存?

  •  
  •   kisshere · Jul 30, 2019 · 4966 views
    This topic created in 2663 days ago, the information mentioned may be changed or developed.

    要包含 ipv4 和 ipv6,网上查了很多人建议 ipv4 用 php 的 ip2long 存成整数型,但是 ipv6 呢?怎样设计一个类型,能同时存 ipv4 和 ipv6,方便极速检索

    Supplement 1  ·  Jul 30, 2019
    ip 字段要索引,方便极速查询
    14 replies  •  2019-07-30 16:39:59 +08:00
    reus
    reus
    Jul 30, 2019
    1
    换 PostgreSQL,用 cidr 类型。
    reus
    reus
    Jul 30, 2019
    2
    mysql 就用 binary(16)
    shoaly
    shoaly
    Jul 30, 2019❤️1
    3
    问一下 存成 字符串 有啥弊端?
    William911
    William911
    Jul 30, 2019
    4
    有这么纠结吗? 纯展示需要就存字符串得了
    jinhan13789991
    jinhan13789991
    Jul 30, 2019 via Android
    5
    转二进制字符串~
    agdhole
    agdhole
    Jul 30, 2019 via Android
    6
    int32
    nlimpid
    nlimpid
    Jul 30, 2019
    7
    如果只有 ipv4,用 int 就够了。
    如果同时有 ipv4 和 ipv6,可以使用 varbinary(16),mysql 有内置的方法处理 ip 类型,见 https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html
    Dragonish3600
    Dragonish3600
    Jul 30, 2019 via iPhone
    8
    Discuz 做法是使用 varchar(45)
    hmxxmh
    hmxxmh
    Jul 30, 2019
    9
    @shoaly 万物皆可字符串
    julyclyde
    julyclyde
    Jul 30, 2019
    10
    @ladypxy discuz 基本上是各种负面例子的集合
    wangkai0351
    wangkai0351
    Jul 30, 2019
    11
    @hmxxmh 一生二,二生三,三生万物?
    GG668v26Fd55CP5W
    GG668v26Fd55CP5W
    Jul 30, 2019 via iPhone
    12
    kiss 原则,keep it simple stupid …
    mostkia
    mostkia
    Jul 30, 2019
    13
    有 ipv6,感觉字符串好一些吧,同时限制一下数据长度即可。
    hikarugo
    hikarugo
    Jul 30, 2019
    14
    怎么简单怎么来,纯展示字符串最直接,如果要解析,那么自己掂量看是把解析业务放在数据库做还是代码层做。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2034 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 01:09 · PVG 09:09 · LAX 17:09 · JFK 20:09
    ♥ Do have faith in what you're doing.