mysql ip存储_MySQL怎样存储IP地址

为什么要问如何存储IP

首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗?

其实做任何程序设计都要在功能实现的基础上最大限度的优化性能。而数据库设计是程序设计中不可忽略的一个重要部分,所以巧存IP地址可以一定程度获得很大提升。

利用函数算法处理

在MySQL中没有直接提供IP类型字段,但如果有两个函数可以把IP与最大长度为10位数字类型互转,所以使用int类型存储IP比varchar类型存储IP地址性能要提升很多,减少不少空间。因为varchar是可变长形,需要多余的一个字节存储长度。另外int型在逻辑运算上要比varchar速度快。

IP转数字函数inet_aton()

我们转换下几个常用的IP地址

mysql> select inet_aton('255.255.255.255');

+------------------------------+

| inet_aton('255.255.255.255') |

+------------------------------+

|                   4294967295 |

+------------------------------+

1 row in set (0.00 sec)

mysql> select inet_aton('192.168.1.1');

+--------------------------+

| inet_aton('192.168.1.1') |

+--------------------------+

|               3232235777 |

+--------------------------+

1 row in set (0.00 sec)

mysql> select inet_aton('10.10.10.10');

+--------------------------+

| inet_aton('10.10.10.10') |

+--------------------------+

|                168430090 |

+--------------------------+

1 row in set (0.00 sec)

所以IP的表字段可以设置为INT(10)就好,如果IP获取不到可以直接存0代表获取不到IP的意思

数字转IP函数inet_ntoa()

mysql> select inet_ntoa(4294967295);

+-----------------------+

| inet_ntoa(4294967295) |

+-----------------------+

| 255.255.255.255       |

+-----------------------+

1 row in set (0.00 sec)

mysql> select inet_ntoa(3232235777);

+-----------------------+

| inet_ntoa(3232235777) |

+-----------------------+

| 192.168.1.1           |

+-----------------------+

1 row in set (0.00 sec)

mysql> select inet_ntoa(168430090);

+----------------------+

| inet_ntoa(168430090) |

+----------------------+

| 10.10.10.10          |

+----------------------+

1 row in set (0.00 sec)

mysql> select inet_ntoa(0);

+--------------+

| inet_ntoa(0) |

+--------------+

| 0.0.0.0      |

+--------------+

1 row in set (0.00 sec)

注意,0转换为 0.0.0.0

整型字段的比较比字符串效率高很多,这也符合一项优化原则:字段类型定义使用最合适(最小),最简单的数据类型。

inet_aton()算法,其实借用了国际上对各国IP地址的区分中使用的ip number。

a.b.c.d 的ip number是:

a * 256的3次方 + b * 256的2次方 + c * 256的1次方 + d * 256的0次方。

来源:

http://www.qttc.net/201208193.html

http://blog.sina.com.cn/s/blog_499740cb0100giny.html

/**

* @author SunChong

*/

public class IpUtil {

/**

* 将字符串型ip转成int型ip

* @param strIp

* @return

*/

public static int Ip2Int(String strIp){

String[] ss = strIp.split("\\.");

if(ss.length != 4){

return 0;

}

byte[] bytes = new byte[ss.length];

for(int i = 0; i 

bytes[i] = (byte) Integer.parseInt(ss[i]);

}

return byte2Int(bytes);

}

/**

* 将int型ip转成String型ip

* @param intIp

* @return

*/

public static String int2Ip(int intIp){

byte[] bytes = int2byte(intIp);

StringBuilder sb = new StringBuilder();

for(int i = 0; i 

sb.append(bytes[i] & 0xFF);

if(i 

sb.append(".");

}

}

return sb.toString();

}

private static byte[] int2byte(int i) {

byte[] bytes = new byte[4];

bytes[0] = (byte) (0xff & i);

bytes[1] = (byte) ((0xff00 & i) >> 8);

bytes[2] = (byte) ((0xff0000 & i) >> 16);

bytes[3] = (byte) ((0xff000000 & i) >> 24);

return bytes;

}

private static int byte2Int(byte[] bytes) {

int n = bytes[0] & 0xFF;

n |= ((bytes[1] <

n |= ((bytes[2] <

n |= ((bytes[3] <

return n;

}

public static void main(String[] args) {

String ip1 = "192.168.0.1";

int intIp = Ip2Int(ip1);

String ip2 = int2Ip(intIp);

System.out.println(ip2.equals(ip1));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值