3 steps to complete the method of importing pure IP data into MySQL

  • 2020-05-09 19:26:50
  • OfStack

Premise: you can use vi. Obviously, this article was tested under Linux. This article USES the MySQL command line tool, and if you don't know, phpMyAdmin should work just as well.
Step 1: download pure IP data and unzip it to ip.txt.
A: no need to elaborate on this step. If you don't know how to do this step, don't look at it.
Step 2: edit ip.txt with vi.
# vi ip.txt
Enter the following command on the vi interface:
:%s/\s\+/;/
1. Repeat the input 3 times.
Withdrawal from saving:
: wq
A: ip.txt has four columns. Start ip, end ip, region, description. Columns are separated by an unequal number of Spaces. In order to import this text file into mysql, you need to dispose of these Spaces. However, you can only remove the Spaces in the first 3 columns and keep the Spaces in the last 1 column. The command entered in vi means to replace the first and successive Spaces of each line with the character '; '.
%s stands for global search substitution. \s for space. \+ stands for match as many of the preceding characters as possible. ; Replace with '; '
Step 3: import MySQL
Create MySQL table
 
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 gargoyles, Collation for the region and comments fields is set to gbk_chinese_ci

Import ip.txt into the ip table of the ips database
Your password --local --delete -- fields-terminated-by ='; 'ips ip txt
Done!
A: mysqlimport can import a text file into a database table. This assumes that the table name is the same as the text file name and that the table fields correspond to the text file.
- fields terminated - by = '; 'means the field is used '; 'spaced apart.
--delete means, if the table already exists, empty the table before importing.

Related articles: