How do I store an IP Address in MySQL? INET_AtoN!

girl working on laptop

One of the most popular questions out there is, “How do I store an IP Address in MySQL?”.   The fastest and most compact way to store an IP address in the format “10.51.1.10” is as an unsigned integer.  If you store the actual values as a char(15), you will require 11 extra bytes per row.  In addition, using IP as a condition when querying is much faster when using an unsigned integer.  Testing has shown that range queries can result in as much as a 35 million times improvement over storing an IP address as a character string.  Yes, that is 35 million!

Simply define IP as an INT UNSIGNED.

Use MySQL’s built-in function INET_ATON() to convert the string representation of the IP Address into an unsigned Integer.  INET_NTOA() will perform the reverse translation.

This will result in space savings, and faster queries, who could ask for more than that?

One thought on “How do I store an IP Address in MySQL? INET_AtoN!

  1. pussy

    you are a fool. you posted this crap with practical codes and made me waste my mb…. go and learn

Leave a Reply

Your email address will not be published. Required fields are marked *