Configuring the database reader requires the database file to be accessible on
the filesystem. After configuring the database reader, you can then query the
database by calling the method corresponding to the database type (e.g. city
or country) and passing it the IP address you want to look up.
If the lookup succeeds, the method call will return a model class/object for the
database method you called. This model in turn contains multiple record
classes/objects, each of which represents part of the data for the record.
If the request fails, the reader class will throw an exception or return an
error depending on the library.
1Filedatabase=newFile("/path/to/maxmind-database.mmdb") 2 3// This reader object should be reused across lookups as creation of it is 4// expensive. 5DatabaseReaderreader=newDatabaseReader.Builder(database).build(); 6 7// If you want to use caching at the cost of a small (~2MB) memory overhead: 8// new DatabaseReader.Builder(file).withCache(new CHMCache()).build(); 910InetAddressipAddress=InetAddress.getByName("128.101.101.101");1112CityResponseresponse=reader.city(ipAddress);1314Countrycountry=response.getCountry();15System.out.println(country.getIsoCode());
1// Asynchronous database opening
2constReader=require('@maxmind/geoip2-node').Reader; 3 4Reader.open('/path/to/maxmind-database.mmdb').then(reader=>{ 5constresponse=reader.city('128.101.101.101'); 6 7console.log(response.country.isoCode); 8}); 91011// Synchronous database opening
12constfs=require('fs');13constReader=require('@maxmind/geoip2-node').Reader;1415constdbBuffer=fs.readFileSync('/path/to/maxmind-database.mmdb');1617// This reader object should be reused across lookups as creation of it is
18// expensive.
19constreader=Reader.openBuffer(dbBuffer);2021response=reader.city('128.101.101.101');2223console.log(response.country.isoCode);
1<?php 2require_once'vendor/autoload.php'; 3useGeoIp2\Database\Reader; 4 5// This reader object should be reused across lookups as creation of it is
6// expensive.
7$reader=newReader('/path/to/maxmind-database.mmdb'); 8 9$record=$reader->city('128.101.101.101');1011print($record->country->isoCode);
1importgeoip2.database23# This reader object should be reused across lookups as creation of it is4# expensive.5withgeoip2.database.Reader('/path/to/maxmind-database.mmdb')asreader:6response=reader.city('128.101.101.101');7print(response.country.iso_code)
1require'maxmind/geoip2'23# This reader object should be reused across lookups as creation of it is4# expensive.5reader=MaxMind::GeoIP2::Reader.new('/path/to/maxmind-database.mmdb')67record=reader.city('128.101.101.101')89putsrecord.country.iso_code