当前位置:网站首页>IP storage and query in MySQL

IP storage and query in MySQL

2022-07-06 00:58:00 Diligently_

MySQL Two methods are provided to handle this ip Address

  • inet_aton hold ip Convert to unsigned integer (4-8 position )
  • inet_ntoa The integer ip To address
  • Before inserting data , First use inet_aton hold ip Address to integer , You can save space , When displaying data , Use inet_ntoa The integer ip Address to address display
  • select 
    INET_ATON('192.168.0.1');
    
    select 
    INET_NTOA('3232235521');

select 
INET_ATON('192.168.0.1')

select 
INET_NTOA('3232235521')

 

Create table statement :

CREATE TABLE `test_ip` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `ipstr` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT 'str Type of ip',
  `ip_aton` bigint DEFAULT NULL COMMENT ' After converting to unsigned ip data ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Insert statement sql:


INSERT INTO test_ip (ipstr,ip_aton) VALUES ('192.168.0.1',INET_ATON('192.168.0.1'));

  The result after inserting is shown in the figure :

 

The query above sql:

SELECT id,ipstr,ip_aton,INET_NTOA(ip_aton) from test_ip

原网站

版权声明
本文为[Diligently_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140158175939.html