Skip to content

自建 IP 地址数据库

本文介绍如何下载和构建自己的 IP 地址数据库,包括 IP 地理位置库和 ASN 自治系统号库。


目录


数据来源

推荐使用 IPin.io,提供ASN和IP地理位置数据下载,国内用户可直接访问。

具体下载页面:


ASN 数据库下载

bash
# IPv6
curl -fL -OJ 'https://ipin.io/download/export?type=ipv6&format=json'
curl -fL -OJ 'https://ipin.io/download/export?type=ipv6&format=csv'
curl -fL -OJ 'https://ipin.io/download/export?type=ipv6&format=sql'

# IPv4
curl -fL -OJ 'https://ipin.io/download/export?type=ipv4&format=json'
curl -fL -OJ 'https://ipin.io/download/export?type=ipv4&format=csv'
curl -fL -OJ 'https://ipin.io/download/export?type=ipv4&format=sql'
bash
# IPv6
wget --content-disposition 'https://ipin.io/download/export?type=ipv6&format=json'
wget --content-disposition 'https://ipin.io/download/export?type=ipv6&format=csv'
wget --content-disposition 'https://ipin.io/download/export?type=ipv6&format=sql'

# IPv4
wget --content-disposition 'https://ipin.io/download/export?type=ipv4&format=json'
wget --content-disposition 'https://ipin.io/download/export?type=ipv4&format=csv'
wget --content-disposition 'https://ipin.io/download/export?type=ipv4&format=sql'

# 断点续传
wget -c -O asn_ipv6_prefixes.json.gz 'https://ipin.io/download/export?type=ipv6&format=json'
wget -c -O asn_ipv4_prefixes.json.gz 'https://ipin.io/download/export?type=ipv4&format=json'
bash
# IPv6
aria2c -x16 -s16 -k1M -o asn_ipv6_prefixes.json.gz 'https://ipin.io/download/export?type=ipv6&format=json'
aria2c -x16 -s16 -k1M -o asn_ipv6_prefixes.csv.gz  'https://ipin.io/download/export?type=ipv6&format=csv'
aria2c -x16 -s16 -k1M -o asn_ipv6_prefixes.sql.gz  'https://ipin.io/download/export?type=ipv6&format=sql'

# IPv4
aria2c -x16 -s16 -k1M -o asn_ipv4_prefixes.json.gz 'https://ipin.io/download/export?type=ipv4&format=json'
aria2c -x16 -s16 -k1M -o asn_ipv4_prefixes.csv.gz  'https://ipin.io/download/export?type=ipv4&format=csv'
aria2c -x16 -s16 -k1M -o asn_ipv4_prefixes.sql.gz  'https://ipin.io/download/export?type=ipv4&format=sql'
powershell
# IPv6
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv6&format=json" -OutFile "asn_ipv6_prefixes.json.gz"
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv6&format=csv"  -OutFile "asn_ipv6_prefixes.csv.gz"
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv6&format=sql"  -OutFile "asn_ipv6_prefixes.sql.gz"

# IPv4
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv4&format=json" -OutFile "asn_ipv4_prefixes.json.gz"
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv4&format=csv"  -OutFile "asn_ipv4_prefixes.csv.gz"
Invoke-WebRequest -Uri "https://ipin.io/download/export?type=ipv4&format=sql"  -OutFile "asn_ipv4_prefixes.sql.gz"

格式说明

格式说明
JSON数组形式,含 start_ipend_ipasncountry_codeorganization
CSV首行表头,RFC 4180 转义
SQL建表 + INSERT 语句,表名 asn_ipv4_prefixesasn_ipv6_prefixes

IP 地理位置数据库下载

更新时间

数据更新日期:2025-10-28

bash
# IPv6
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.json.gz'
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.csv.gz'
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.sql.gz'

# IPv4
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.json.gz'
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.csv.gz'
curl -fL -OJ 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.sql.gz'
bash
# IPv6
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.json.gz'
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.csv.gz'
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.sql.gz'

# IPv4
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.json.gz'
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.csv.gz'
wget --content-disposition 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.sql.gz'

# 断点续传
wget -c -O ipv4_ip_location.json.gz 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.json.gz'
wget -c -O ipv6_ip_location.json.gz 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.json.gz'
bash
# IPv6
aria2c -x16 -s16 -k1M -o ipv6_ip_location.json.gz 'https://ipin.io/exports/20251028_204109/ipv6_ip_location.json.gz'
aria2c -x16 -s16 -k1M -o ipv6_ip_location.csv.gz  'https://ipin.io/exports/20251028_204109/ipv6_ip_location.csv.gz'
aria2c -x16 -s16 -k1M -o ipv6_ip_location.sql.gz  'https://ipin.io/exports/20251028_204109/ipv6_ip_location.sql.gz'

# IPv4
aria2c -x16 -s16 -k1M -o ipv4_ip_location.json.gz 'https://ipin.io/exports/20251028_204109/ipv4_ip_location.json.gz'
aria2c -x16 -s16 -k1M -o ipv4_ip_location.csv.gz  'https://ipin.io/exports/20251028_204109/ipv4_ip_location.csv.gz'
aria2c -x16 -s16 -k1M -o ipv4_ip_location.sql.gz  'https://ipin.io/exports/20251028_204109/ipv4_ip_location.sql.gz'
powershell
# IPv6
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv6_ip_location.json.gz" -OutFile "ipv6_ip_location.json.gz"
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv6_ip_location.csv.gz"  -OutFile "ipv6_ip_location.csv.gz"
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv6_ip_location.sql.gz"  -OutFile "ipv6_ip_location.sql.gz"

# IPv4
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv4_ip_location.json.gz" -OutFile "ipv4_ip_location.json.gz"
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv4_ip_location.csv.gz"  -OutFile "ipv4_ip_location.csv.gz"
Invoke-WebRequest -Uri "https://ipin.io/exports/20251028_204109/ipv4_ip_location.sql.gz"  -OutFile "ipv4_ip_location.sql.gz"

格式说明

格式说明
JSON数组形式,含 ip_fromip_tocountry_coderegioncitylatitudelongitude
CSV首行表头,RFC 4180 转义
SQL建表 + INSERT 语句,表名 geoip_ranges

JSON 示例

查看示例
json
[
  {
    "ip_from": "1.0.0.0",
    "ip_to": "1.0.0.255",
    "country_code": "AU",
    "region": "Queensland",
    "city": "South Brisbane",
    "latitude": -27.47480,
    "longitude": 153.01700
  }
]

CSV 示例

查看示例
csv
ip_from,ip_to,country_code,region,city,latitude,longitude
1.0.0.0,1.0.0.255,AU,Queensland,South Brisbane,-27.47480,153.01700

数据库建表语句

ASN 表

sql
CREATE TABLE asn_ipv4_prefixes (
  start_ip      VARCHAR(39) NOT NULL,
  end_ip        VARCHAR(39) NOT NULL,
  asn           INT UNSIGNED NOT NULL,
  country_code  CHAR(2),
  organization  VARCHAR(255),
  INDEX idx_asn (asn),
  INDEX idx_country (country_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE asn_ipv6_prefixes (
  start_ip      VARCHAR(39) NOT NULL,
  end_ip        VARCHAR(39) NOT NULL,
  asn           INT UNSIGNED NOT NULL,
  country_code  CHAR(2),
  organization  VARCHAR(255),
  INDEX idx_asn (asn),
  INDEX idx_country (country_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

IP 地理位置表

sql
CREATE TABLE geoip_ranges (
  ip_from      VARCHAR(39)  NOT NULL,
  ip_to        VARCHAR(39)  NOT NULL,
  country_code CHAR(2),
  region       VARCHAR(100),
  city         VARCHAR(100),
  latitude     DECIMAL(9,5),
  longitude    DECIMAL(9,5),
  PRIMARY KEY (ip_from, ip_to),
  INDEX idx_country (country_code),
  INDEX idx_city (city)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

导入提示

  • SQL 文件可直接导入 MySQL:mysql -u username -p database_name < file.sql
  • JSON/CSV 文件可使用 LOAD DATA INFILE 或程序导入
  • 建议导入前先创建索引,导入完成后再建主键可提高速度