http://www.maxmind.com/app/csv
▷ Instructions on how to use our CSV databases with a SQL database.
▷ Download the latest GeoLite Country CSV Format
GeoIP Country CSV Text Files
MaxMind GeoIP databases are available in a Comma Separated Value (CSV) format, in addition to the binary format. These CSV files generally contain IP Address range and geographical data for all publicly assigned IPv4 addresses.
Due to the large size of geolocation databases, we generally recommend using our binary format with one of our APIs, since they are highly optimized for speed and disk space. On the other hand, if you have a requirement to import the data into a SQL database, the CSV format is recommended. We have listed some guidelines for importing and querying the data with a SQL database.
- Beginning IP Address
- Ending IP Address
- Beginning IP Number*
- Ending IP Number*
- ISO 3166 Country Code
- Country Name
This is an sample of how the CSV file is structured:
"begin_ip","end_ip","begin_num","end_num","country","name" "61.88.0.0","61.91.255.255","1029177344","1029439487","AU","Australia" "61.92.0.0","61.93.255.255","1029439488","1029570559","HK","Hong Kong" "61.94.0.0","61.94.7.255","1029570560","1029572607","ID","Indonesia" |
ipnum = 16777216*w + 65536*x + 256*y + z (1) where IP Address = w.x.y.z The reverse of this formula is w = int ( ipnum / 16777216 ) % 256; x = int ( ipnum / 65536 ) % 256; y = int ( ipnum / 256 ) % 256; z = int ( ipnum ) % 256; Where % is the mod operator. |
sub numToStr { my ($ipnum) = @_; my $z = $ipnum % 256; $ipnum >>= 8; my $y = $ipnum % 256; $ipnum >>= 8; my $x = $ipnum % 256; $ipnum >>= 8; my $w = $ipnum % 256; return "$w.$x.$y.$z"; } |
SQL Query
SELECT ip_country FROM geoip WHERE 404232216 BETWEEN begin_ip_num AND end_ip_num LIMIT 1 |
SELECT ip_country FROM geoip WHERE 404232216 >= begin_ip_num AND 404232216 <= end_ip_num LIMIT 1 |
404232216 = 16777216*24 + 65536*24 + 256*24 + 24 |
First create the SQL table as follows.
CREATE TABLE `geo_blocks` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `location_id` INT(10) UNSIGNED NOT NULL, `ip_start` INT(10) UNSIGNED NOT NULL, `ip_end` INT(10) UNSIGNED NOT NULL, `index_geo` INT(10) UNSIGNED NOT NULL, PRIMARY KEY (`id`), INDEX `idx_start` (`ip_start`), INDEX `idx_end` (`ip_end`), INDEX `idx_geo` (`index_geo`) ); update `geo_blocks` set `index_geo` = (`ip_end` - mod(`ip_end`, 65536));To select the location, run:
SELECT ... FROM geo_blocks ... WHERE index_geo = INET_ATON("92.229.175.253")-(INET_ATON("92.229.175.253")%65536) AND INET_ATON("92.229.175.253") BETWEEN ip_start AND ip_end;Contributed by Kamil Kubica
Guides Please see our GeoIP Resources page for links on how to load the GeoIP files into MySQL, Oracle, PostgreSQL, and MS Access.
'ETC' 카테고리의 다른 글
MSSQL MDF, LDF 파일을 이용해서 복원 및 이전, 스키마 일괄변경 (0) | 2011.04.25 |
---|---|
윈도우 원격 접속시 로컬디스크 연결 및 테스트 2차 도메인 세팅 (0) | 2011.04.19 |
IE8 개발자도구 안보일때 (0) | 2010.11.24 |
DBDesigner4와 Mysql5 연동 (0) | 2010.05.31 |
Wisdows 7 에서 80번 포트 종료 (0) | 2010.04.27 |