官方文档 搬运 MAXMIND IP定位 mysql导入 简单使用

本文主要是介绍官方文档 搬运 MAXMIND IP定位 mysql导入 简单使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方文档地址:

官方文档       

文件下载

1. 导入mysql可能报错

Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

查看配置

    SHOW GLOBAL VARIABLES LIKE '%secure%';

secure_file_priv 原来是NULL 依旧报错 

 mysql --help | grep my.cnf                                                            /etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf#修改配置
vim /opt/homebrew/etc/my.cnf#追加或修改
secure-file-priv = "/"#重启mysql
mysql.server restart

将 GeoIP2 和 GeoLite2 数据库导入 MySQL

在本页

  • 下载并提取数据库
    • 保持数据库更新
    • 提取 CSV 文件
  • 为网络数据创建表
  • 转换网络字段
  • 架构
    • 将数据加载到网络表中
    • 通过查询来测试我们的表
    • 对表格进行排序以便更快地进行搜索
    • 分解查询以加快搜索速度
  • 可选:创建位置数据表
    • 位置表架构
    • 将数据加载到位置表中
    • 查询我们的表格

本指南将向您展示如何将 GeoIP2 或 GeoLite2 数据库导入 MySQL,以便在您的服务器上轻松查询和操作它们。

导入 CSV 数据库包括下载数据库、提取数据库、创建表来保存数据以及为加快查询速度而对这些表进行索引。

下载并提取数据库

首先,请确保您已下载要导入的 GeoIP2 或 GeoLite2 数据库的最新版本。您可以 通过您的帐户门户下载数据库。CSV 格式的数据库以单个 zip 文件的形式提供。有关存档的 zip 结构和内容的详细信息,请参阅我们的 CSV 数据库文档。在本教程中,我们将使用 GeoIP2 City CSV 文件,但您可以获取有关 我们的任何 CSV 格式数据库的信息并相应地调整以下说明。

保持数据库更新

如果您要导入数据库以供持续使用,您将需要 自动下载和提取 CSV 文件的过程 ,以确保您的数据库始终是最新的。

提取 CSV 文件

下载数据库后,将 zip 文件解压到所需目录中。如果要导入 GeoIP2 City 数据库,您将获得许多文件。在本教程中,我们将使用以下文件:

  • GeoIP2-City-Blocks-IPv4.csv
  • GeoIP2-City-Blocks-IPv6.csv
  • GeoIP2-City-Locations-en.csv

如果您使用英语以外的语言,则可以Locations从 zip 存档中选择适当的文件。例如,如果您想将中文位置名称加载到 MySQL,则可以使用 GeoIP2-City-Locations-zh-CN.csv而不是GeoIP2-City-Locations-en.csv。我们关于 CSV 格式数据库的文档包括 GeoIP2 和 GeoLite2 数据库中当前包含的所有位置文件的列表。

为网络数据创建表

GeoIP2-City-Blocks-IPv4.csv首先我们创建一个表来保存和中包含的网络信息 GeoIP2-City-Blocks-IPv6.csv

转换网络字段

您可以在GeoIP2 和 GeoLite2 CSV 数据库文件部分Blocks中找到这些文件架构的完整描述 。如果您使用的是其他数据库,则可以找到相应数据库的Blocks文件架构,并调整表格以满足该结构。

数据库的字段network使用 CIDR 表示法。遗憾的是,MySQL 不提供处理该格式数据的任何功能,因此我们必须先将网络转换为其他格式,以便以后轻松查询。我们选择将网络表示为一对 IP 地址,它们分别是网络中的第一个和最后一个地址。我们将转换此字段,以便这两个 IP 地址都表示为十六进制数。

我们可以使用 数据库转换工具 将此字段转换为十六进制数。下载程序并将其安装到提取 CSV 文件的同一目录中后,即可运行它:


$ ./geoip2-csv-converter -block-file GeoIP2-City-Blocks-IPv4.csv -include-hex-range -output-file GeoIP2-City-Blocks-IPv4-Hex.csv
$ ./geoip2-csv-converter -block-file GeoIP2-City-Blocks-IPv6.csv -include-hex-range -output-file GeoIP2-City-Blocks-IPv6-Hex.csv

架构

现在我们可以创建一个名为的表geoip2_network来保存我们刚刚转换的数据。我们将使用类型来表示 IP 地址varbinary(16),该类型足够大,可以表示 128 位(16 字节)IPv6 地址。

create table geoip2_network (network_start varbinary(16) not null,network_end varbinary(16) not null,geoname_id int,registered_country_geoname_id int,represented_country_geoname_id int,is_anonymous_proxy bool,is_satellite_provider bool,postal_code text,latitude float,longitude float,accuracy_radius int,is_anycast bool,index(network_start),index(network_end)
);

network_start请注意,我们为和 添加了两个单独的索引network_end。如果我们在这两列上都使用复合索引,我们将无法加快稍后使用的查询速度。

将数据加载到网络表中

GeoIP2-City-Blocks-IPv4.csv我们现在可以将和 的内容导入GeoIP2-City-Blocks-IPv6.csv到我们刚刚创建的表中。

我们首先加载转换后的 IPv6 数据:

load data infile '/var/maxmind/GeoIP2-City-Blocks-IPv6-Hex.csv'
into table geoip2_network
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows
(@network_start, @network_end, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
set network_start = unhex(@network_start),network_end = unhex(@network_end),geoname_id = nullif(@geoname_id, ''),registered_country_geoname_id = nullif(@registered_country_geoname_id, ''),represented_country_geoname_id = nullif(@represented_country_geoname_id, ''),is_anonymous_proxy = nullif(@is_anonymous_proxy, ''),is_satellite_provider = nullif(@is_satellite_provider, ''),postal_code = nullif(@postal_code, ''),latitude = nullif(@latitude, ''),longitude = nullif(@longitude, ''),accuracy_radius = nullif(@accuracy_radius, '');

我们可以用同样的方式加载转换后的IPv4数据:

load data infile '/var/maxmind/GeoIP2-City-Blocks-IPv4-Hex.csv'
into table geoip2_network
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows
(@network_start, @network_end, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
set network_start = unhex(@network_start),network_end = unhex(@network_end),geoname_id = nullif(@geoname_id, ''),registered_country_geoname_id = nullif(@registered_country_geoname_id, ''),represented_country_geoname_id = nullif(@represented_country_geoname_id, ''),is_anonymous_proxy = nullif(@is_anonymous_proxy, ''),is_satellite_provider = nullif(@is_satellite_provider, ''),postal_code = nullif(@postal_code, ''),latitude = nullif(@latitude, ''),longitude = nullif(@longitude, ''),accuracy_radius = nullif(@accuracy_radius, '');

请注意,即使 CSV 文件中有表中缺少的额外列,数据也会被导入。

通过查询来测试我们的表

所有内容加载完毕后,我们现在可以在数据库中查找 IP 地址。由于我们将 IP 地址表示为varbinary(16),因此我们首先必须使用 MySQL 的内置inet6_aton函数将我们感兴趣的 IP 地址的文本表示转换为相同类型。

select geoname_id, registered_country_geoname_id, represented_country_geoname_id,postal_code, latitude, longitude, accuracy_radius
from geoip2_network
where inet6_aton('214.0.0.0') between network_start and network_end
limit 1;
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
|    6252001 |                       6252001 |                           NULL | NULL        |   37.751 |   -97.822 |            1000 |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
1 row in set (0.03 sec)

虽然这会产生正确的结果,但我们注意到查询性能可以更好。让我们改进它。

对表格进行排序以便更快地进行搜索

加快查询速度的一种方法是添加order by network_end如下内容:

select geoname_id, registered_country_geoname_id, represented_country_geoname_id,postal_code, latitude, longitude, accuracy_radius
from geoip2_network
where inet6_aton('214.0.0.0') between network_start and network_end
order by network_end
limit 1;
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
|    6252001 |                       6252001 |                           NULL | NULL        |   37.751 |   -97.822 |            1000 |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
1 row in set (0.00 sec)

虽然这解决了我们对上一个查询的性能担忧,但对于 GeoIP2 数据库中不包含的地址,此查询的性能仍然很差:

elect geoname_id, registered_country_geoname_id, represented_country_geoname_id,postal_code, latitude, longitude, accuracy_radius
from geoip2_network
where inet6_aton('127.0.0.1') between network_start and network_end
order by network_end
limit 1;
Empty set (4.45 sec)

分解查询以加快搜索速度

我们可以解决这个问题,将查询分成两部分,这样 MySQL 就能更有效地使用我们创建的索引:

select geoname_id, registered_country_geoname_id, represented_country_geoname_id,postal_code, latitude, longitude, accuracy_radius
from (select *from geoip2_networkwhere inet6_aton('214.0.0.0') >= network_startorder by network_start desclimit 1
) net
where inet6_aton('214.0.0.0') <= network_end;
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
|    6252001 |                       6252001 |                           NULL | NULL        |   37.751 |   -97.822 |            1000 |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
1 row in set (0.00 sec)
select geoname_id, registered_country_geoname_id, represented_country_geoname_id,postal_code, latitude, longitude, accuracy_radius
from (select *from geoip2_networkwhere inet6_aton('127.0.0.1') >= network_startorder by network_start desclimit 1
) net
where inet6_aton('127.0.0.1') <= network_end;
Empty set (0.00 sec)

使用该构造可以为所有地址提供良好的查询性能,无论 GeoIP2 数据库是否包含有关这些地址的任何信息。根据您的应用程序,您可能需要考虑将这种复杂性/冗长性封装在一个函数中。或者,MySQL 还提供可用于实现类似性能的空间数据类型,同时允许更自然地表达查询。

可选:创建位置数据表

如果postal_codelatitudelongitudeaccuracy_radius是我们感兴趣的所有内容,那么到此为止,我们的应用程序将能够轻松查询所需的内容。但是,GeoIP2 数据库提供了额外的位置信息。请注意geoname_id我们网络表中的字段。此字段可用于从我们之前下载的文件中查找有关地理位置的其他信息Locations。接下来我们将这些数据加载到 MySQL 中。

位置表架构

我们首先像以前一样创建一个表。与文件一样Blocks,GeoIP2 和 GeoLite2 城市位置文件的架构可以在 数据库文档的 CSV 部分中找到。

我们将此表命名为geoip2_location

create table geoip2_location (geoname_id int not null,locale_code text not null,continent_code text,continent_name text,country_iso_code text,country_name text,subdivision_1_iso_code text,subdivision_1_name text,subdivision_2_iso_code text,subdivision_2_name text,city_name text,metro_code int,time_zone text,is_in_european_union bool,primary key (geoname_id, locale_code(5))
);

将数据加载到位置表中

然后,我们geoip2_locationLocationsCSV 文件填充表格。在本例中,我们将从 填充表格GeoIP2-City-Locations-en.csv。使用带有后缀的文件-en将为我们提供英文的地理位置名称:

导入城市CSV
load data infile '/var/lib/mysql-files/GeoIP2-City-Locations-en.csv'
into table geoip2_location
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (geoname_id, locale_code, continent_code, continent_name,@country_iso_code, @country_name, @subdivision_1_iso_code, @subdivision_1_name,@subdivision_2_iso_code, @subdivision_2_name, @city_name, @metro_code, @time_zone,is_in_european_union
)
set country_iso_code = nullif(@country_iso_code, ''),country_name = nullif(@country_name, ''),subdivision_1_iso_code = nullif(@subdivision_1_iso_code, ''),subdivision_1_name = nullif(@subdivision_1_name, ''),subdivision_2_iso_code = nullif(@subdivision_2_iso_code, ''),subdivision_2_name = nullif(@subdivision_2_name, ''),city_name = nullif(@city_name, ''),metro_code = nullif(@metro_code, ''),time_zone = nullif(@time_zone, '');

导入国家

load data infile '/var/maxMind/GeoLite2-Country-CSV_20240614/GeoLite2-Country-Locations-en.csv'
into table geoip2_locationfields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (geoname_id, locale_code, continent_code, continent_name,@country_iso_code, @country_name,is_in_european_union
)
set country_iso_code = nullif(@country_iso_code, ''),country_name = nullif(@country_name, '');

请注意,有许多不同的Locations文件可用。其他具有不同语言后缀的文件包含-en 某些geoname_ids 的不同语言的本地化版本数据。根据应用程序的需求,您可以决定将其他Locations文件导入本地化表。例如,您可以加载GeoIP2-City-Locations-en.csv 到名为 的表中geoip2_location-en,并加载 GeoIP2-City-Locations-zh-CN.csv到名为 的表中geoip2_location-zh。然后,您可以分别查询您需要的英语或中文位置表。

查询我们的表格

我们现在可以使用我们的geoip2_location表来解析表geoname_id提供的内容geoip2_network。例如:

select latitude, longitude, accuracy_radius, continent_name, country_name, subdivision_1_name, city_name
from (select *from geoip2_networkwhere inet6_aton('214.0.0.0') >= network_startorder by network_start desclimit 1
) net
left join geoip2_location location on (net.geoname_id = location.geoname_id and location.locale_code = 'en'
)
where inet6_aton('214.0.0.0') <= network_end;
+----------+-----------+-----------------+----------------+---------------+--------------------+-----------+
| latitude | longitude | accuracy_radius | continent_name | country_name  | subdivision_1_name | city_name |
+----------+-----------+-----------------+----------------+---------------+--------------------+-----------+
|   37.751 |   -97.822 |            1000 | North America  | United States | NULL               | NULL      |
+----------+-----------+-----------------+----------------+---------------+--------------------+-----------+
1 row in set (0.00 sec)

这里我们只对英语结果感兴趣,但如果我们对不同或其他语言感兴趣,我们可以调整我们的连接条件。

请注意左外连接是如何使用的。这是因为我们的geoip2_network 表的任何给定行可能都没有可用的附加位置信息。例如,某些 IP 地址无法解析为城市或分区。如果可用,使用左连接我们仍会收到latitudelongitude和 accuracy_radius作为查询结果,而如果没有可用的附加位置信息,则内连接将导致零行。

除了geoname_id提供网络位置信息的列之外,还有 和registered_country_geoname_id, represented_country_geoname_id分别提供有关 ISP 注册网络的国家/地区和 IP 地址用户所代表的国家/地区的位置信息。 两者的位置数据都可以通过其他连接来包含:

select latitude, longitude, accuracy_radius,location.continent_name as location_continent_name,location.country_name as location_country_name,location.subdivision_1_name as location_subdivision_1_name,location.city_name as location_city_name,registered_country.continent_name as registered_country_continent_name,registered_country.country_name as registered_country_country_name,represented_country.continent_name as represented_country_continent_name,represented_country.country_name as represented_country_country_name
from (select *from geoip2_networkwhere inet6_aton('214.0.0.0') >= network_startorder by network_start desclimit 1
) net
left join geoip2_location location on (net.geoname_id = location.geoname_id and location.locale_code = 'en'
)
left join geoip2_location registered_country on (net.registered_country_geoname_id = registered_country.geoname_idand registered_country.locale_code = 'en'
)
left join geoip2_location represented_country on (net.represented_country_geoname_id = represented_country.geoname_idand represented_country.locale_code = 'en'
)
where inet6_aton('214.0.0.0') <= network_end;
+----------+-----------+-----------------+-------------------------+-----------------------+-----------------------------+--------------------+-----------------------------------+---------------------------------+------------------------------------+----------------------------------+
| latitude | longitude | accuracy_radius | location_continent_name | location_country_name | location_subdivision_1_name | location_city_name | registered_country_continent_name | registered_country_country_name | represented_country_continent_name | represented_country_country_name |
+----------+-----------+-----------------+-------------------------+-----------------------+-----------------------------+--------------------+-----------------------------------+---------------------------------+------------------------------------+----------------------------------+
|   37.751 |   -97.822 |            1000 | North America           | United States         | NULL                        | NULL               | North America                     | United States                   | NULL                               | NULL                             |
+----------+-----------+-----------------+-------------------------+-----------------------+-----------------------------+--------------------+-----------------------------------+---------------------------------+------------------------------------+----------------------------------+
1 row in set (0.00 sec)

node 使用

github 文档地址icon-default.png?t=N7T8https://github.com/maxmind/GeoIP2-node#city-example

1. 导入模块

npm install @maxmind/geoip2-node

2. node使用

import { Reader } from "@maxmind/geoip2-node";const path = require("path");
const databasePath = path.resolve(__dirname, "../../../GeoLite2-Country.mmdb");export default class IpCenterV2 {//获取国家code
public async getIP(ip: any) {const result = Reader.open(databasePath).then((reader) => {return reader.country(ip)?.country?.isoCode;}).catch((error) => {console.error("Error opening MMDB:", error);return undefined;});return result || undefined;}}#调用const bIpDb = new IpCenterV2();et res = await bIpDb.getCountryISOCode(ip);

这篇关于官方文档 搬运 MAXMIND IP定位 mysql导入 简单使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1065052

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

MySQL zip安装包配置教程

《MySQLzip安装包配置教程》这篇文章详细介绍了如何使用zip安装包在Windows11上安装MySQL8.0,包括下载、解压、配置环境变量、初始化数据库、安装服务以及更改密码等步骤,感兴趣的朋... 目录mysql zip安装包配置教程1、下载zip安装包:2、安装2.1 解压zip包到安装目录2.2

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

MySQL安装时initializing database失败的问题解决

《MySQL安装时initializingdatabase失败的问题解决》本文主要介绍了MySQL安装时initializingdatabase失败的问题解决,文中通过图文介绍的非常详细,对大家的学... 目录问题页面:解决方法:问题页面:解决方法:1.勾选红框中的选项:2.将下图红框中全部改为英

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Python自动化Office文档处理全攻略

《Python自动化Office文档处理全攻略》在日常办公中,处理Word、Excel和PDF等Office文档是再常见不过的任务,手动操作这些文档不仅耗时耗力,还容易出错,幸运的是,Python提供... 目录一、自动化处理Word文档1. 安装python-docx库2. 读取Word文档内容3. 修改

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安