Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type should I store IP addresses for MySQL?

Tags:

mysql

I was going to use varchar(20), but I was wondering what should if I should do INT and strip off the periods instead. What would be better and why?

like image 851
Strawberry Avatar asked Jan 04 '11 02:01

Strawberry


People also ask

What is data type for IP address in MySQL?

MySQL. While MySQL doesn't have a data type for IP addresses, it's still easy to compare IPs in MySQL. Using inet_aton you can convert your IP addresses to integers before you compare.

Where should I store my IP address?

We can store an IP address with the help of INT unsigned. While using INSERT, include INET_ATON() and with SELECT, include INET_NTOA().

What data type is an IP address?

IP Network Address Data Types. IPV4 and IPV6 are abstract data types that store IPv4 and IPv6 host addresses, respectively, in binary format. IPV4 is a 4-byte host address in dotted-decimal notation (four decimal numbers, each ranging from 0 to 255, separated by dots). For example: 172.16.

How does MySQL store client IP address?

In this post, we will investigate how to discover the answer to Mysql Store Ip Address using the computer language. -- `ip_address` int(4) unsigned NOT NULL INSERT INTO my_table (`ip_address`) VALUES (INET_ATON("127.0. 0.1")); SELECT INET_NTOA(ip_address) as ip FROM my_table; -- php <? php var_dump(ip2long('123.63.


1 Answers

I presume you're only interested in IPv4 addresses, not IPv6.

I would use an INT UNSIGNED for the column, and then use INET_ATON and INET_NTOA to convert back and forth between the textual representation and the int value.

mysql> SELECT INET_ATON('192.168.10.50');
+----------------------------+
| INET_ATON('192.168.10.50') |
+----------------------------+
|                 3232238130 |
+----------------------------+
1 row in set (0.00 sec)

mysql> SELECT INET_NTOA(3232238130);
+-----------------------+
| INET_NTOA(3232238130) |
+-----------------------+
| 192.168.10.50         |
+-----------------------+
1 row in set (0.00 sec)
like image 68
Ozone Avatar answered Sep 20 '22 15:09

Ozone