mysql中IP地址的存储

IP:如192.168.12.145,在存储时,若是采用varchar进行存储,存在两个主要缺点:

  1. 存储空间占用较大;
  2. 查询检索较慢;

解决方式:

  1. 存储时:将字符串类型的IP转换为整型进行存储;
  2. 查询时:将整型的IP转换为字符串

Mysql自带的IP转换语句

  • inet_aton:将ip地址转换成数字型
  • inet_ntoa:将数字型转换成ip地址

示例1:

//使用inet_aton函数,将字符串IP转换为整型;
mysql> select inet_aton('73.115.134.73') as ip;
+------------+
| ip |
+------------+
| 1232307785 |
+------------+ //使用inet_ntoa函数,将整型IP转换为字符串;
mysql> select inet_ntoa(1232307785) as ip;
+---------------+
| ip |
+---------------+
| 73.115.134.73 |
+---------------+

示例2:


//不进行转换,查询结果为整型
mysql> select src_ip from natTable limit 5;
+------------+
| src_ip |
+------------+
| 1232307785 |
| 1232285337 |
| 1232323310 |
| 1232325234 |
| 1232326662 |
+------------+ //通过inet_ntoa函数进行转换,查询结果为IP格式的字符串
mysql> select inet_ntoa(src_ip) from natTable limit 5;
+-------------------+
| inet_ntoa(src_ip) |
+-------------------+
| 73.115.134.73 |
| 73.115.46.153 |
| 73.115.194.238 |
| 73.115.202.114 |
| 73.115.208.6 |
+-------------------+

自定义转换函数:

如:将1232307785转换为73.116.134.73

DELIMITER $$
CREATE FUNCTION natAndImDbTest11.ipBigIntToString (
ip bigint
)
RETURNS CHAR(15)
BEGIN
DECLARE o1 INT;
DECLARE o2 INT;
DECLARE o3 INT;
DECLARE o4 INT;
DECLARE ip_new_varchar VARCHAR(15); IF (ip > 4294967295) then
RETURN '255.255.255.255';
END if; IF (ip <= 0) then
RETURN '0.0.0.0' ;
END if; SET o1 = ip / 16777216;
SET ip = ip % 16777216 ; SET o2 = ip / 65536 ;
SET ip = ip % 65536 ; SET o3 = ip / 256 ;
SET ip = ip % 256 ; SET o4 = ip ;
SET ip_new_varchar = concat( cast(o1 as char(3)),'.',cast(o2 as char(3)),'.',cast(o3 as char(3)),'.',cast(o4 as char(3))); RETURN (ip_new_varchar);
END;
$$
DELIMITER ;

测试:

use natAndImDbTest11;
mysql> select ipBigIntToString(1232307785);
+------------------------------+
| ipBigIntToString(1232307785) |
+------------------------------+
| 73.116.134.73 |
+------------------------------+

其他说明

删除自定义方法

drop FUNCTION if exists natAndImDbTest11.ipBigIntToString;

自定义方法的局限

本打算使用 ipBigIntToString 代替 inet_ntoa 进行查询,发现不行:

mysql> select ipBigIntToString(src_ip) from natTable limit 5;
ERROR 5 (HY000): The query includes syntax that is not supported by the Infobright Optimizer. Either restructure the query with supported syntax, or enable the MySQL Query Path in the brighthouse.ini file to execute the query with reduced performance.
mysql>

可能有其他方式可以使用ipBigIntToString替代inet_ntoa,但是目前自己未成功;

【mysql】IP地址整数int和varchar的转换的更多相关文章

  1. IP地址转换为Int

    1.转换类 import com.google.common.base.Strings; import java.security.InvalidParameterException; import ...

  2. IP地址和int互转

    /** * @author: yqq * @date: 2019/5/8 * @description: ip地址与int之间互换 * https://mp.weixin.qq.com/s?__biz ...

  3. PHP中IP地址与整型数字互相转换详解

    这篇文章主要介绍了PHP中IP地址与整型数字互相转换详解,本文介绍了使用PHP函数ip2long与long2ip的使用,以及它们的BUG介绍,最后给出自己写的两个算法,需要的朋友可以参考下 IP转换成 ...

  4. IP地址在mysql的存储(IP地址和int的转换)

    PHP echo ip2long('192.168.1.38'); 输出:3232235814 MYSQL SELECT INET_ATON('192.168.1.38'); 输出:323223581 ...

  5. IP地址的存储和使用

    ip地址使用int类型存储,用INET_NTOA()和INET_ATON()转换 mysql'),inet_aton('127.0.0.1'); +-------------------------+ ...

  6. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  7. 批处理快速更改ip地址

    在各种网络中切换,windows更换ip地址步骤:  进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...

  8. C#根据IP地址和子网掩码计算广播地址

    using System.Net; /// <summary> /// 获得广播地址 /// </summary> /// <param name="ipAdd ...

  9. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

随机推荐

  1. each的break

    $.each var arr = [1, 2, 'test', 3, 4, 5, 6] // break $.each(arr, function(index, value) { if (value ...

  2. # 20155219实验二 Java面向对象程序设计

    20155219实验二 Java面向对象程序设计 一.实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5 ...

  3. 如何更改Apache的根目录指向

    更改Apache的默认网站根目录地址方法如下: 0,先找到主目录下的apache文件,然后进行下面操作 1.找到 DocumentRoot “X:/Apache/htdocs” 将“X:/Apache ...

  4. jmeter定时器

    一.定时器的作用 1.定时器是在每个sampler(采样器)之前执行的,而不是之后(无论定时器位置在sampler之前还是下面): 2.当执行一个sampler之前时,所有当前作用域内的定时器都会被执 ...

  5. js中将一个字一个字的打印出来

    第一种方式: setTimeout(function(){ var cc=document.createTextNode(ss[i]) content.appendChild(cc) },3000)

  6. C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. shell-dict-uniq-count

    shell dict  #!/bin/bash result_file="a" declare -A mydict :>${result_file} total=`cat $ ...

  8. Calendar类的使用——闰年的计算

    1.Calendar类: 是一个抽象类,需要导入java.util.Calendar包. Calendar c = Calendar.getInstance(); getInstance直接返回的是子 ...

  9. What are long running processes?

    转自:https://blog.bernd-ruecker.com/what-are-long-running-processes-b3ee769f0a27 Some communities have ...

  10. C# to IL 4 Keywords and Operators(关键字和操作符)

    Code that is placed after the return statement never gets executed. In the first programgiven below, ...