1.业务中IP地址字段推荐使用int类型,不推荐用char(15)
因为int只占4字节,可以用如下函数相互转换,而char(15)占用至少15字节。一旦表数据行数到了1亿,那么要多用1.1G存储空间!
SQL:select inet_aton('192.168.2.12'); select inet_ntoa(3232236044);
Php: ip2long(‘192.168.2.12’); long2ip(3530427185);
2.创建数据库
2.1char类型
CREATE TABLE `login_log_char` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`login_ip` char(15) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='登录日志';
INSERT INTO `test`.`login_log_char` (`id`, `username`, `login_ip`) VALUES ('1', '1', '192.168.1.1');
select * from login_log_char
2.2.int类型
注意这里unsigned ,否则插入数据报错,要么改为bigint
CREATE TABLE `login_log_int` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`login_ip` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='登录日志';
INSERT INTO `login_log_int` (`username`, `login_ip`)
VALUES
(
'1',
inet_aton('192.168.1.1')
);
select *,INET_NTOA(login_ip) from login_log_int
3.函数
select inet_aton('192.168.2.12');
select inet_ntoa(3232236044);