How to import pure IP data to MySQL in three steps

Source: Internet
Author: User

Premise: You will use VI. Obviously, this article is tested in Linux. This document uses the MySQL command line tool. If you do not, you can use phpMyAdmin.
Step 2: Download the pure real IP data and decompress it into ip.txt.
Explanation: you do not need to explain this step. If you do not know this step, you will not need to read it below.
Step 2: Use VI to edit ip.txt.
# Vi ip.txt
On the VI interface, enter the following command:
: % S/\ s \ + /;/
Input three times in total.
Disk logout:
: WQ
Detailed Description: ip.txt has four columns. Start IP address, end IP address, and region, respectively. Use unequal spaces to separate columns. To import the text file to MySQL, you need to remove these spaces. However, only spaces in the first three columns can be processed, and spaces in the last column must be retained. The command entered in VI is to replace the first and consecutive spaces in each line with the character ';'.
% S indicates replacement of global search. \ S stands for spaces. \ + Indicates matching as many characters as possible .; Replace ';'
Step 2: Import MySQL
Create a MySQL table CopyCode The Code is as follows: Create Table 'ips '. 'IP '(
'IP _ start' varchar (16) not null,
'IP _ end' varchar (16) null,
'Region' varchar (128) null,
'Comments' varchar (512) null
) Engine = MyISAM

To avoid garbled characters, set collation of the region and comments fields to gbk_chinese_ci.

Import ip.txt to the IP address table of the IPs Database
# Mysqlimport-p your password -- local -- delete -- fields-terminated-by = '; 'ips ip.txt
Done!
Description: mysqlimport can import a text file to a database table. The premise is that the table name is the same as the text file name, and the table field corresponds to the text file.
-- Fields-terminated-by = ';' indicates that fields are separated.
-- Delete indicates that if a table already exists, data in the table is cleared before import.

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.