I am trying to import GeoLite2 ip to country csv file to my MySQL database. I was succesfully able to do that, however, to use it to search for IPs in my queries I need to create two more fields in both the ipv4 and ipv6 tables to store the start_ip and end_ip values for each CIDR range given (GeoLite csv files specify only the range in CIDR format).
I was able to convert this correctly for ipv4 address ranges using the solution given here Importing MaxMind's GeoLite2 to MySQL
So I used
INET_ATON(SUBSTRING(network, 1, LOCATE('/', network) - 1))
for start_ip and
INET_ATON(SUBSTRING(network, 1, LOCATE('/', network) - 1)) + (POW(2, (32-CONVERT(SUBSTRING(network, LOCATE('/', network) + 1), UNSIGNED INT)))-1)
for end_ip. network is the name of the field where the CIDR range is stored.
However this does not work for IPv6 address ranges in CIDR format. I tried using INET6_ATON instead of INET_ATON but it gives me a numeric value out of range error. I am not much knowledgeable about IPv6 addresses so finding a formula myself is not easy.
Any help appreciated.
You can use my tool for converting MaxMind GeoLite2 country/city CSV to MySQL/PostgreSQL/Microsoft SQL Server 2019 from GitHub https://github.com/mbto/maxmind-geoip2-csv2sql-converter
You can read examples, or follow this steps:
bin/GeoLite2-Country-CSV.mysql.default.ini
with your profile name, examplebin/GeoLite2-Country-CSV.mysql.Your Project Name.ini
or use default.[windows_loader]
or[unix_loader]
section (set MySQL host:port, user and password).chmod +x maxmind-geoip2-csv2sql-converter
maxmind-geoip2-csv2sql-converter.bat -c "GeoLite2-Country-CSV.mysql.Your Project Name.ini" -k Your_License_Key -i 4,6
bin/converted/loader.bat
andbin/converted/loader.sh
will be available.chmod +x loader.sh
loader.bat
orloader.sh
for importing schemas.Done