How to configure, Use and Auto-Update Maxmind GeoIP Database

Mithun Kadyada
3 min readNov 27, 2019

--

Fig-1: Basic Work-flow of DataReader API

Overview:

MaxMind is a leading provider of IP intelligence and online fraud prevention tools. It provides a line of database services and products that provide geographic information and other data associated with specific Internet protocol addresses (each a “ GeoIP Database” and collectively the “ GeoIP Databases”). This blog is about reading IP information from MaxMind database PHP framework.

There are 2 types of databases provided by MaxMind:

  • GeoLite ( free database).
  • GeoIP (licensed and paid database).

In this blog, I’m going to explain the following steps:

  1. Downloading database file.
  2. Installing the required dependencies.
  3. Using dependencies.
  4. Reading data from the database with example.
  5. Setting up the auto-update program.

1. Downloading database file:

  • For downloading GeoLite databases click here (note: download .mmdb < comes under MaxMind DB binary, gzipped > format instead of .csv format).
  • For downloading GeoIP databases click here (note: it’s not a free version, Login to your account and purchase the database).
Fig-2: Reference for downloading proper .mmdb database

Note: If you’re using any framework and your project is hosted then it’s better to save the downloaded database file in your project root directory. If you’re using this database for test purposes then, file location doesn’t matter(ex: /path-to/GeoIP/GeoIP2-Country.mmdb).

2. Installing the required dependencies:

— Installing composer:

  • To download Composer, run in the root directory of your project: curl -sS https://getcomposer.org/installer | php
  • You should now have the file composer.phar in your project directory.

— Installing geoip2 data-reader dependency:

  • Run in your project root: php composer.phar require geoip2/geoip2:~2.0
  • composer.json and composer.lock as well as the directory vendor in your project directory is automatically generated. If you use a version control system, composer.json should be added to it.

3. Using dependencies:

  • If your code is in the root directory, then add require_once 'vendor/autoload.php'; on top of your page.
  • If your page is in sub-folder, then take the below reference require_once( dirname(__FILE__).'/../vendor/autoload.php');

4. Reading data from the database with example:

  • If you’re not using any PHP framework, then use the below code.
  • If you’re working on any PHP Framework(eg: Yii framework) then refer below code.

5. Setting up the auto-update program:

MaxMind provides the GeoIP Update program, which performs automatic updates for both GeoIP2 and GeoIP Legacy binary databases. Please follow the instructions below:

Step 1: Install GeoIP Update:

  • Install GeoIP Update. The latest release may be downloaded from GitHub Releases. See here for installation instructions.

Step 2: Obtain ‘GeoIP.conf’ with your Account Information:

  • For Paid GeoIP2 and GeoIP Legacy Databases — Get a partially pre-filled configuration file (may require authentication) and save it in your configuration directory (e.g., /usr/local/etc/) as GeoIP.conf. You will need to replace the YOUR_LICENSE_KEY_HERE placeholder with an active license key associated with your MaxMind account. You can see your license key information on your account License Keys page.
  • For Free GeoLite2 Databases — The GeoIP Update program will also work without an account to retrieve the GeoLite2 databases. Please use the following GeoIP.conf file:

Step 3: Run GeoIP Update:

Go to crontab -e, open the crontab file and add the below code.

# top of crontab 
MAILTO=<your_mail_id>

5 22 * * * /usr/local/bin/geoipupdate
# end of crontab

This crontab file would run every day at 10:05 PM, and it would email you the results. if you don’t want to receive mail, then remove MAILTO=<your_email_id>

Conclusion:

I hope you liked and followed this blog on reading IP information from the MaxMind database with the help of PHP framework. We learned how to download databases, reading the location from the database and auto-updating the databases. The method explained above will work on .mmdb format of both GeoIP-databases(paid) and GeoLite-databases(free-version). For GeoIP-database update you have to pay for a subscription(monthly and yearly subscription available).

References:

Originally published at https://blogs.tensult.com on November 27, 2019.

--

--