this is obsolete doc -- see http://doc.nethence.com/ instead

Binding Bind to MySQL... On RHEL5 

 

Introduction 

Note. The [MySQL Bind SDB Driver] (http://mysql-bind.sourceforge.net/) works only with Bind 9, not Bind 8. 

 

Requirements 

Make sure those packages are installed, 

rpm -q \
mysql \
mysql-server \
mysql-devel \
openssl \
openssl-devel \
gcc \
make \
| grep ^package

 

Make sure MySQL is running and secured, 

service mysqld restart
chkconfig mysqld on
mysql_secure_installation

 

Restrict the MySQL daemon to localhost, 

cd /etc/
cp my.cnf my.cnf.dist
vi my.cnf

add this to the [mysql] section, 

bind-address=127.0.0.1...

apply, 

service mysqld restart

Ref. http://www.greensql.com/articles/mysql-security-best-practices 

 

Create a database and grant write access to e.g. DBUSER with the DBPASS passwod, 

mysql -uroot -p
create database DBNAME;
create user DBUSER identified by 'DBPASS';
grant all privileges on DBNAME.* to DBUSER;

 

Make sure BIND server is NOT installed, 

rpm -qa | grep bind

Note. bind-libs and bind-utils packages are just fine 

Note. bind, bind-chroot and bind-sdb pkgs should NOT be installed 

 

Installation 

Get latest BIND tarball (ftp://ftp.isc.org/isc/bind9/cur/), 

wget ftp://ftp.isc.org/isc/bind9/cur/9.7/bind-9.7.7.tar.gz

 

Get the BIND/MySQL SDB driver (http://sourceforge.net/projects/mysql-bind/), 

wget http://sourceforge.net/projects/mysql-bind/files/latest/download

 

Extract the archives, 

tar xzf bind-9.7.7.tar.gz
tar xzf mysql-bind.tar.gz

 

Deploy the driver, 

cp mysql-bind/mysqldb.c bind-9.7.7/bin/named/
cp mysql-bind/mysqldb.h bind-9.7.7/bin/named/include/

 

Edit the makefile, 

cd bind-9.7.7/bin/named/
cp Makefile.in Makefile.in.dist
vi Makefile.in

change, 

DBDRIVER_OBJS = mysqldb.@O@
DBDRIVER_SRCS = mysqldb.c

also see MySQL's compile flags, 

mysql_config --cflags

and copy/paste to, 

DBDRIVER_INCLUDES = ...(paste)...

also see MySQL's libraries, 

mysql_config --libs

and copy/paste to, 

DBRIVER_LIBS = ...(paste)...

 

Add driver's include into Bind's code, 

(same directory bin/named)
cp main.c main.c.dist
vi main.c

add, 

/*
 * Include header files for database drivers here.
 */
/* #include "xxdb.h" */
#include "mysqldb.h"

also search for 'ns_server_create' and add mysqldb_init just before, 

mysqldb_init();
ns_server_create(ns_g_mctx, &ns_g_server);

also search for 'ns_server_destroy' and add mysqldb_clear just after, 

ns_server_destroy(&ns_g_server);
mysqldb_clear();

 

Fix driver's own include path (named/), 

(same directory bin/named)
cp mysqldb.c mysqldb.c.dist
vi mysqldb.c

change, 

#include <named/mysqldb.h>,

by, 

#include "include/mysqldb.h"

Note. otherwise you'll get this error at compile time, 

mysqldb.c:41:27: error: named/mysqldb.h: No such file or directory

 

Now compile Bind, 

cd ../../
./configure --help
./configure
#./configure --disable-openssl-version-check
make clean
make
make install

 

Configuration 

Let's proceed with the Bind configuration (new file), 

cd /etc/
vi named.conf

like, 

zone "example.local" {
        type master;
        notify no;
        database "mysqldb DBNAME example_local 127.0.0.1 DBUSER DBPASS";
};

note. Change DBNAME, DBUSER and DBPASS accordingly. 

note. Use underscores instead of a dot for table name as domain name, change example_local accordingly. 

check, 

named-checkconf

Note. also just in case (optional, it reads /etc/named.conf anyway), 

cd /usr/local/etc/
ln -s ../../../etc/named.conf

 

Connect to MySQL as DBUSER, 

mysql -uDBUSER -p
use DBNAME;

create a table per domain in that database, 

create table example_local (
  name varchar(255) default NULL,
  ttl int(11) default NULL,
  rdtype varchar(255) default NULL,
  rdata varchar(255) default NULL
) type=MyISAM;

inject some data e.g., 

insert into example_local values ('example.local', 259200, 'SOA', 'example.local. postmaster.example.local. 1358628703 28800 7200 86400 28800');
insert into example_local values ('example.local', 259200, 'NS', 'ns.example.local.');
insert into example_local values ('example.local', 259200, 'MX', '5 mx.example.local.');
insert into example_local values ('ns.example.local', 259200, 'A', '10.0.0.253');
insert into example_local values ('example.local', 259200, 'A', '10.1.1.1');
insert into example_local values ('mail.example.local', 259200, 'A', '10.1.1.1');
insert into example_local values ('www.example.local', 259200, 'Cname', 'example.local.');

Note. change 1358628703 by the serial you want. 

 

Ready go go 

Start the daemon and enable it at boot time, 

/usr/local/sbin/named
cat >> /etc/rc.local <<EOF9
echo -n Starting named-mysql...
/usr/local/sbin/named && echo \ done
EOF9

 

Check that everything's fine, 

netstat -an --inet | grep :53
host example.local localhost
host www.example.local localhost

 

In case you need to reload named.conf, 

ps aux | grep named
kill -HUP XXXX

and you should seem something like this in the logs, 

Jan 19 22:03:57 named-mysql named[1400]: reloading zones succeeded

 

Installing some GUI 

Well phpMyAdmin will do just fine, even the old version (2.x), 

cd ~/
yum -y install httpd php php-mysql php-mcrypt
service httpd start
chkconfig httpd on
wget http://www.oldapps.com/phpmyadmin.php?app=ee2c1b8fffbed9f3c3a1121c30cb5ba4
#wget http://www.oldapps.com/phpmyadmin.php?app=b8ef0a2e18e08c1e533116a2b1fa4ebf
unzip phpMyAdmin-2.11.9.3-english.zip
#unzip phpMyAdmin-2.11.11.3-all-languages.zip
mv phpMyAdmin-2.11.9.3-english/ /var/www/htdocs/SOMEFOLDER/
cd /var/www/htdocs/SOMEFOLDER/
ls -l config.inc.php
sed '/^\//d; /^ \*/d; /^$/d' config.sample.inc.php > config.inc.php
vi config.inc.php

note. Just php-mcrypt is additionally needed by this version of phpmyadmin.: 

change, 

$cfg['blowfish_secret'] = 'ANYTHING HERE';
$cfg['Servers'][$i]['extension'] = 'mysqli';

and point your browser to, 

http://NAMED_SERVER/SOMEFOLDER/

login as DBUSER/DBPASS. 

 

Convertion script 

Be careful this is only usable for a small home network, not production systems, as it causes a little service interruption (the appropriate word should rather be "corruption" as no host name resolution will be provided in that small amount of time) while recreating the table. Convert a hosts file (having fqdn at the end of lines) to sql queries, 

copy/paste your hosts file to hosts.input

then, 

vi schosts

like, 

#!/bin/ksh
set -e

 

dbname=DBNAME
dbuser=DBUSER

 

serial=`date +%s`

 

cat <<EOF9
mysql -u$dbuser -p

 

use $dbname;
truncate table example_local;
insert into example_local values ('example.local', 259200, 'SOA', 'example.local. postmaster.example.local. $serial 28800 7200 86400 28800');
insert into example_local values ('example.local', 259200, 'NS', 'ns.example.local.');
insert into example_local values ('example.local', 259200, 'MX', '5 mx.example.local.');

 

EOF9

 

sed '/^$/d; /^#/d;' hosts.input | while read line; do
        ip=`echo "$line" | awk '{print $1}'`
        long=`echo "$line" | awk '{print $NF}'`
        cat <<EOF9
insert into example_local values ('$long', 259200, 'A', '$ip');
EOF9
        unset ip long
done

note. Change DBNAME and DBUSER accordingly. 

note. if you need to add CNAME you'll have to tune that script for, 

  insert into example_local values ('some.host.example.local', 259200, 'CNAME', 'some.other.host.example.local.');

note. assuming the ns.example.local host is defined within your hosts file. 

enable the script, 

chmod +x schosts

 

The changes are immediate, no need to reload named with kill -HUP. 

 

References 

mysql-bind/README 

bind-9.7.1-P2/README 

bind-9.7.1-P2/doc/misc/SDB 

http://mysql-bind.sourceforge.net/ 

http://goes-gsoc.zerothree.net/2009/05/09/mysql-bind-sdb-driver/