MySQL uses inet_aton and inet_ntoa to process IP address data

MySQL uses inet_aton and inet_ntoa to process IP address data

This article will introduce how to save IP address data in a database using a suitable format and to easily compare IP addresses.

1. Save the IP address to the database

The IP address is saved in the database, and the field is generally defined as:

`ip` char(15) NOT NULL,

Because the maximum length of an IP address (255.255.255.255) is 15, a 15-bit char is sufficient.

Create table user

CREATE TABLE `user` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(30) NOT NULL,
 `ip` char(15) NOT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB;

Insert some data

INSERT INTO `user` (`id`, `name`, `ip`) VALUES
(2, 'Abby', '192.168.1.1'),
(3, 'Daisy', '172.16.11.66'),
(4, 'Christine', '220.117.131.12');

2. MySQL inet_aton and inet_ntoa methods

MySQL provides two methods to handle IP addresses

inet_aton converts ip to unsigned integer (4-8 bits)

inet_ntoa converts the integer IP address to an address

Before inserting the data, use inet_aton to convert the IP address to an integer to save space, because char(15) occupies 16 bytes.

When displaying data, use inet_ntoa to convert the integer IP address into an address for display.

example:

CREATE TABLE `user` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(100) NOT NULL,
 `ip` int(10) unsigned NOT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB;

Insert some data

INSERT INTO `user` (`id`, `name`, `ip`) VALUES
(2, 'Abby', inet_aton('192.168.1.1')),
(3, 'Daisy', inet_aton('172.16.11.66')),
(4, 'Christine', inet_aton('220.117.131.12'));

mysql> select * from `user`;
+----+-----------+------------+
| id | name | ip |
+----+-----------+------------+
| 2 | Abby | 3232235777 |
| 3 | Daisy | 2886732610 |
| 4 | Christine | 3698688780 |
+----+-----------+------------+

The query is displayed as the electrical address

mysql> select id,name,inet_ntoa(ip) as ip from `user`;
+----+-----------+----------------+
| id | name | ip |
+----+-----------+----------------+
| 2 | Abby | 192.168.1.1 |
| 3 | Daisy | 172.16.11.66 |
| 4 | Christine | 220.117.131.12 |
+----+-----------+----------------+

3. Comparison method

If you need to find users in a certain network segment (for example: 172.16.11.1 ~ 172.16.11.100), you can use PHP's ip2long method to convert the IP address into an integer and then compare it.

<?php
$ip_start = '172.16.11.1';
$ip_end = '172.16.11.100';

echo 'ip2long(ip_start):'.sprintf('%u',ip2long($ip_start)); // 2886732545
echo 'ip2long(ip_end):'.sprintf('%u',ip2long($ip_end)); // 2886732644
?>

Query:

mysql> select ip,name,inet_ntoa(ip) as ip from `user` where ip>=2886732545 and ip<=2886732644;
+------------+-------+---------------+
| ip | name | ip |
+------------+-------+---------------+
| 2886732610 | Daisy | 172.16.11.66 |
+------------+-------+---------------+

Note: When using the ip2long method to convert the IP address to an integer, a negative number may appear for a large IP. For the cause and solution, please refer to my other article: " Detailed discussion of the cause and solution of negative numbers in php ip2long "

4. Summary

1. Save the IP address to the database using the unsigned int format. When inserting, use the inet_aton method to convert the IP address into an unsigned integer to save storage space.

2. Use inet_ntoa to convert the integer IP address to an address when displaying.

3. When php ip2long converts IP to integer, you need to pay attention to negative numbers.

The above example of MySQL using inet_aton and inet_ntoa to process IP address data is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to store IP addresses in MySQL
  • MySQL cross-database table replication example (in the same IP address)
  • PHP method, Asp method, MsSQL method, MySQL method to convert IP address into integer
  • How to use MySQL's inet_aton() and inet_ntoa() functions to store IP addresses
  • How to access MySql through IP address

<<:  Detailed explanation of several ways to create objects and object methods in js

>>:  Introduction to Linux File Compression and Packaging

Recommend

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance h...

Record a troubleshooting record of high CPU usage of Tomcat process

This article mainly records a tomcat process, and...

Connector configuration in Tomcat

JBoss uses Tomcat as the Web container, so the co...

How to build Jenkins+Maven+Git continuous integration environment on CentOS7

This article takes the deployment of Spring boot ...

JavaScript DOMContentLoaded event case study

DOMContentLoaded Event Literally, it fires after ...

The easiest way to reset mysql root password

My mysql version is MYSQL V5.7.9, please use the ...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

Detailed explanation of MySQL database isolation level and MVCC

Table of contents 1. Isolation Level READ UNCOMMI...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

Detailed explanation of Docker working mode and principle

As shown in the following figure: When we use vir...