原文:https://www.jb51.net/article/29962.htm

当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用inet_aton()和inet_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,同时效率也高很多

mysql> create table jackbillow (ip int unsigned, name char(1)); 
Query OK, 0 rows affected (0.02 sec)

mysql> insert into jackbillow values(inet_aton('192.168.1.200'), 'A'), (inet_aton('200.100.30.241'), 'B'); 
Query OK, 2 rows affected (0.00 sec) 
Records: 2 Duplicates: 0 Warnings: 0

mysql> insert into jackbillow values(inet_aton('24.89.35.27'), 'C'), (inet_aton('100.200.30.22'), 'D'); 
Query OK, 2 rows affected (0.00 sec) 
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from jackbillow; 
+------------+------+ 
| ip | name | 
+------------+------+ 
| 3232235976 | A | 
| 3362004721 | B | 
| 408494875 | C | 
| 1690836502 | D | 
+------------+------+ 
4 rows in set (0.00 sec)

mysql> select * from jackbillow where ip = inet_aton('192.168.1.200'); 
+------------+------+ 
| ip | name | 
+------------+------+ 
| 3232235976 | A | 
+------------+------+ 
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow; 
+----------------+ 
| inet_ntoa(ip) | 
+----------------+ 
| 192.168.1.200 | 
| 200.100.30.241 | 
| 24.89.35.27 | 
| 100.200.30.22 | 
+----------------+ 
4 rows in set (0.00 sec)

当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用inet_aton()和inet_ntoa()函数,来存储IP地址效率很高,适用unsigned int 就可以满足需求,不需要使用bigint,只需要4个字节,节省存储空间,同时效率也高很多。

如果IP列有索引,可以使用下面方式查询:

mysql> select inet_aton('100.200.30.22'); 
+----------------------------+ 
| inet_aton('100.200.30.22') | 
+----------------------------+ 
| 1690836502 | 
+----------------------------+ 
1 row in set (0.00 sec)

mysql> select * from jackbillow where ip=1690836502; 
+------------+------+ 
| ip | name | 
+------------+------+ 
| 1690836502 | D | 
+------------+------+ 
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip),name from jackbillow where ip=1690836502; 
+---------------+------+ 
| inet_ntoa(ip) | name | 
+---------------+------+ 
| 100.200.30.22 | D | 
+---------------+------+ 
1 row in set (0.00 sec)

对于LIKE操作,可以使用下面方式:

mysql> select inet_ntoa(ip) from jackbillow; 
+----------------+ 
| inet_ntoa(ip) | 
+----------------+ 
| 192.168.1.200 | 
| 200.100.30.241 | 
| 24.89.35.27 | 
| 100.200.30.22 | 
| 192.168.1.100 | 
| 192.168.1.20 | 
| 192.168.2.20 | 
+----------------+ 
7 rows in set (0.00 sec)

mysql> select inet_aton('192.168.1.0'); 
+--------------------------+ 
| inet_aton('192.168.1.0') | 
+--------------------------+ 
| 3232235776 | 
+--------------------------+ 
1 row in set (0.00 sec)

mysql> select inet_aton('192.168.1.255'); 
+----------------------------+ 
| inet_aton('192.168.1.255') | 
+----------------------------+ 
| 3232236031 | 
+----------------------------+ 
1 row in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow where ip between 3232235776 and 3232236031; 
+---------------+ 
| inet_ntoa(ip) | 
+---------------+ 
| 192.168.1.200 | 
| 192.168.1.100 | 
| 192.168.1.20 | 
+---------------+ 
3 rows in set (0.00 sec)

mysql> select inet_ntoa(ip) from jackbillow where ip between inet_aton('192.168.1.0') and inet_aton('192.168.1.255'); 
+---------------+ 
| inet_ntoa(ip) | 
+---------------+ 
| 192.168.1.200 | 
| 192.168.1.100 | 
| 192.168.1.20 | 
+---------------+ 
3 rows in set (0.00 sec)

利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法的更多相关文章

  1. mysql 使用inet_aton和inet_ntoa处理ip地址数据

    mysql 使用inet_aton和inet_ntoa处理ip地址数据 mysql提供了两个方法来处理ip地址 inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转 ...

  2. 【mysql】MySQL存储IP地址

    为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...

  3. MySQL怎样存储IP地址

    为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...

  4. MySQL如何有效的存储IP地址

    前几天,阿淼的一个朋友去面试,他回来告诉我,面试官问他 IP 地址是怎么存在数据库的?他当时也没多想,直接就回答的存字符串啊(心想:这么简单的问题,怕不是看不起我吧) 前面这段权当看看,毕竟 IP地址 ...

  5. MySQL和PHP中以整型存储IP地址

    正文:将IP地址以整型存储 一般我们在数据库中会用到ip地址用来查记录的等等,而ip地址是分为四段的,一般是用varchar或char类型存储.但是其实有更好的存储方法就是以整型存储IP地址. 因为c ...

  6. MySQL怎样存储IP地址 IP转数字 互转

    MySQL怎样存储IP地址 - cn三少 - 博客园 https://www.cnblogs.com/cnsanshao/p/3326648.html

  7. 如何在数据库中存储IP地址

    最近改一个比较老的web系统,该系统是通过账号或者ip地址(白名单)验证限制访问权限的. 由于运营的时间比较长了,发现进入网站巨卡... 原因就是:之前的数据库(sqlserver)存储ip地址是用的 ...

  8. mysql 存储ip地址

    mysql提供了两个方法来处理ip地址: inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转为电地址 插入数据前,先用inet_aton把ip地址转为整型,可以节 ...

  9. MySQL存储IP地址操作

    数据库数据表创建语法: DROP TABLE IF EXISTS `admin`; CREATE TABLE IF NOT EXISTS `admin`( `adminid` INT UNSIGNED ...

随机推荐

  1. Python - Django - 中间件 process_template_response

    process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生 ...

  2. LeetCode_461. Hamming Distance

    461. Hamming Distance Easy The Hamming distance between two integers is the number of positions at w ...

  3. Docker是什么?

    Docker是什么? Docker是一个虚拟环境容器,可以将你的环境.代码.配置文件等一并打包到这个容器中,并发布和应用到任意平台中.比如,你在本地部署了git,jenkins等,可以将其与插件一并打 ...

  4. ssh连接的原理

    ssh是linux系统中的一个远程连接工具,也是一种网络协议,通过各种加密算法达到安全连接的效果.若能使用ssh连接到另外一台机器上,我们就可以认为是安全的.本节主要介绍的是ssh连接的原理以及ssh ...

  5. IDEA更改JavaScript版本

    最好改两个地方 File -> File -> -- --

  6. Java基础知识点总结(三)

    figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...

  7. centos实现三个节点高可用

    centos实现三个节点高可用 使用的资源为keepalived和nginx 高可用主机IP地址 192.168.136.131 192.168.136.133 192.168.136.134 ngi ...

  8. sql特殊日期

    --a. 本月的第一天 select dateadd(mm, datediff(mm,0,getdate()), 0) as 本月的第一天 --b. 本月的最后一天 select dateadd(ms ...

  9. 全栈项目|小书架|微信小程序-书籍详情功能实现

    效果图 实现分析 从效果图上分析,书籍详情是通过点击首页的item后进入. 进入详情页之后页面顶部显示书籍的相关信息,同时判断用户是否登录,未登录则弹出一个授权登录窗口. 点击登录之后即可加载出用户评 ...

  10. 使用Dapper查询记录是否存在

    /// <summary> /// Dapper数据访问抽象基础类 /// </summary> public class DapperHelper { public stat ...