How to save space for IP address storage in MySQL

Source: Internet
Author: User
As a developer, when you use MySQL to store your database, you must consider using the most appropriate field type to create a database table. Www.2cto.com, but if one of your statements writes IP varchar (15) not null default ''comment 'user IP address', you have not considered it. In fact, there is a more space-saving way to store users' IP addresses. (This is a variable, but for most Chinese IP addresses, it is generally 10 bytes or more ). In fact, MySQL has special fields and functions to access the user's IP address, that is, int unsigned. You are not mistaken, that is, there is no matching int type. In MySQL, you can: Select inet_aton ('2017. 168.23.4 '); the value of this IP address is 3232241412. You can also select inet_ntoa (3232241412) in MySQL. The IP address corresponding to this value is: 192.168.23.4 because they all follow a conversion algorithm: A * 256*256*256 + B * 256*256 + C * 256 + D provides a rough Java method to convert an IP address to a long one: public static long iptolong (string strip) throws exception {long [] IP = new long [4]; // locate the IP address string first. int position1 = strip. indexof (". "); int position2 = strip. indexof (". ", position1 + 1); int position3 = strip. indexof (". ", position2 + 1); // set each. string to integer IP Address [0] = long. parselong (strip. substring (0, position1); IP [1] = long. parselong (strip. substring (position1 + 1, position2); IP [2] = long. parselong (strip. substring (position2 + 1, position3); IP [3] = long. parselong (strip. substring (position3 + 1); Return (IP [0] <24) + (IP [1] <16) + (IP [2] <8) + IP [3];} then provides a rough Java method to convert long into an IP string: public static string longtoip (long longip) {stringbuffer sb = new stringbuffer (""); // move the 24-bit Sb directly to the right. append (string. valueof (longip >>> 24); sb. append (". "); // change the height of 8 to 0, and then shift the 16-bit sb to the right. append (string. valueof (longip & 0x00ffffff) >>> 16); sb. append (". "); // move the 16-digit height to 0, and then shift the 8-digit sb to the right. append (string. valueof (longip & 0x0000ffff) >>> 8); sb. append (". "); // set the height to 24 position 0 sb. append (string. valueof (longip & 0x000000ff); return sb. tostring ();} to: http://www.2cto.com/database/201211/168801.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.