In this tutorial we demonstrate how to fetch a geolocation CSV or MMDB database and setup automatic updates.
dbip-update is the preferred way for loading and updating databases. It is bundled in the package below and is meant to be run on the command line.
dbip-phpsrc-4.0.tgzWhen run without arguments or configuration file, dbip-update outputs this help page :
~# ./dbip-update.php
usage: ./dbip-update.php -k <accountKey> [-l] [-d <dbType>] [-f <format>] [-o <outputDir|outputFileName>] [-b <dataSourceName> [-u <dbUser>] [-p <dbPassword] [-t <dbTableName>]] [-c <configFile>] [-n] [-z|-Z] [-w] [-q]
-l list available items and exit
-n request new items only
-z fetch uncompressed file (default for mmdb format)
-Z fetch compressed file (default for csv format)
-w overwrite destination file if it already exists
-b PDO DSN for database update (ie. "mysql:host=localhost;dbname=dbip")
-u database username (default 'root')
-p database password (default '')
-t name of database table (default 'dbip_lookup')
-q be quiet
~#
If you plan to use it for updates on a regular basis, you may want to create a configuration file to hold your basic account and database configuration.
The configuration file is optional. When dbip-update finds a file named dbip-update.ini in its directory, it will attempt to load it and apply its settings.
Below is a sample dbip-update.ini :
[account]
; Your account key is available in your customer section at https://db-ip.com/account/
accountKey = INSERT_YOUR_ACCOUNT_KEY_HERE
[file]
; This can be either csv or mmdb, default is csv
format = mmdb
; The place where your web server or other app expects to find the database
outputFileName = /home/myapp/dbip-country.mmdb
Now that you have created a configuration file, it is time to fetch the latest available database file.
This is simply done by running dbip-update :
# ./dbip-update.php
Starting update for ip-to-country (November 14th 2024)
Download completed: 6,847.9 KB
Verify signature: [MD5] [SHA1] passed
Wrote /home/myapp/dbip-country.mmdb
~#
The process takes a few seconds to several minutes depending on your internet connection performance
dbip-update has the ability to start an update only if there are new database releases that you have not downloaded yet.
This is done by adding a -n
parameter to the commande line :
~# ./dbip-update.php -n
there are no new downloads available
~#
If your operating systems supports it, you should then add a crontab entry to periodically check for new releases and update your local database.
When running it from a crontab, you should prevent dbip-update from sending unnecessary details to the console, the -q
argument will silent all but critical errors.
In the sample crontab entry below, dbip-update is run daily at midnight and will update the local database if it finds a new release :
# m h dom mon dow user command
0 0 * * * root /path/to/myapp/dbip/dbip-update.php -w -n -q
API | DB | |||||||
---|---|---|---|---|---|---|---|---|
Free | Basic | Core | Extended | IP to Country | IP to City | IP to Location | IP to ISP | IP to Location+ISP |
Was this article helpful ? Share it with others by clicking the social media buttons !