[HowTo] Bind9+MySQL on Debian
Tired of editing Bind zone files? Or you want to create UI for your clients, so they can edit their zones? It can’t be easier, than when you have your zones in MySQL database.
Here I will explain how to install Bind with MySQL database support. After patching, Bind will be able to use both, MySQL tables and zone files.
Installing packages and getting sources
Get MySQL BIND SDB driver from http://mysql-bind.sourceforge.net/
If you don’t have a Bind server yet, it would be nice to install it. This will create startup scripts, sample configs, etc.
apt-get install bind
Now set up all other packages need:
apt-get install mysql-server-5.0 mysql-client libmysql++-dev dpkg-dev
Now, get source of your existing Bind package (the current version is bind9-9.3.4):
apt-get source bind
Untar both packages, bind9_9.3.4.orig.tar.gz and mysql-bind.tar.gz
There is already a README with mysql-bind package, but I’ll explain here as well.
Copy file mysqldb.c from mysql-bind package to “bind9-9.3.4/bin/named/mysqldb.c” and
mysqldb.h to “bind9-9.3.4/bin/named/include/named/mysqldb.h”
Patching Bind source files
Edit: bind9-9.3.4/bin/named/Makefile.in
Under comment “# Add database drivers here.” set these:
DBDRIVER_OBJS = mysqldb.@O@
DBDRIVER_SRCS = mysqldb.c
DBDRIVER_INCLUDES = -I’/usr/include/mysql’
DBDRIVER_LIBS = -L’/usr/lib/mysql’ -lmysqlclient
Where:
DBDRIVER_INCLUES is output of “mysql_config –cflags”
DBDDRIVER_LIBS is output of “mysql_config –libs”
Edit: bind9-9.3.4/bin/named/main.c
Add “#include <named/mysqldb.h>” in the beginning of the file, right after this comment “* Include header files for database drivers here.“.
Add “mysqldb_init();” right before “ns_server_create(some params);” and “mysqldb_clear();” after “ns_server_destroy(some params);“.
You’ll see comments like “* Add calls to register/unregister sdb drivers here.”
Compiling Bind
Now go to bind9-9.3.4 and configure.
“sysconfdir”, “prefix” and “localstatedir” should much your current configuration. If you installed bind via APT, then these should be the right ones:
./configure –sysconfdir=/etc/bind –prefix=/usr –localstatedir=/var/run/bind
make
make install
Now if you restart Bind (/etc/init.d/bind9 restart), it should run without problems.
Creating database and inserting sample data
mysql> create database bind;
mysql> use bind;
I will create a subzone of hackersdiary.com, test.hackersdiary.com
mysql> CREATE TABLE test_hackersdiary_com (
name varchar(255) default NULL,
ttl int(11) default NULL,
rdtype varchar(255) default NULL,
rdata varchar(255) default NULL
) TYPE=MyISAM;
mysql> INSERT INTO test_hackersdiary_com VALUES (’test.hackersdiary.com’, 3600, ‘SOA’, ‘ns.hackersdiary.com. tom.hackersdiary.com. 200902051 28800 7200 86400 28800′);
mysql> INSERT INTO test_hackersdiary_com VALUES (’test.hackersdiary.com’, 3600, ‘NS’, ‘ns.hackersdiary.com.’);
mysql> INSERT INTO test_hackersdiary_com VALUES (’test.hackersdiary.com’, 3600, ‘NS’, ‘ns2.hackersdiary.com.’);
mysql> INSERT INTO test_hackersdiary_com VALUES (’test.hackersdiary.com’, 3600, ‘MX’, ‘10 mail.test.hackersdiary.com.’);
mysql> INSERT INTO test_hackersdiary_com VALUES (’mail.test.hackersdiary.com’, 3600, ‘A’, ‘127.0.0.1′);
Now setup zone in “/etc/bind/named.conf.local”
zone “test.hackersdiary.com” {
type master;
database “mysqldb bind test_hackersdiary_com localhost binduser bindpassword”;
};
Restart Bind (/etc/init.d/bind9 restart)
Testing zone
Now ask your nameserver to resolve mail.test.hackersdiary.com “dig mail.test.hackersdiary.com @ns.hackersdiary.com”
You should get:
;; ANSWER SECTION:
mail.test.hackersdiary.com. 3600 IN A 127.0.0.1
Now get PhpMyAdmin or create your own UI for zone editing. There is one ready to use dnsSQLpanel, but I haven’t tested it.
Comments? Problems? You’re welcome.