How To Install Bind9+MySQL


I used to have this tutorial some time ago online but due to some unfortunate circumstances i lost the database backup holding it.
Therefore i’m coming back with a revised edition of it for those in need of a bind9 with mysql installation.
At the time of this article writing the bind9 sources from Debian Etch are patched for the latest cache-poisoning vulnerability found by Mr. Dan Kaminsky
Once again this tutorial is strictly Debian Etch compatible. You may however reproduce this install on other Linux distros but i’m not gonna support any question related to other installations.
Enough said let’s proceed.

I assume you’ve already logged in on your server. Allright. Let’s go to /usr/src.

gw1-livent:~# cd /usr/src/
gw1-livent:/usr/src#

We get the bind9 source.

gw1-livent:/usr/src# apt-get source bind9
Reading package lists… Done
Building dependency tree… Done
Need to get 4347kB of source archives.
Get:1 http://ftp.debian.org etch/main bind9 1:9.3.4-2etch3 (dsc) [897B]
Get:2 http://ftp.debian.org etch/main bind9 1:9.3.4-2etch3 (tar) [4044kB]
Get:3 http://ftp.debian.org etch/main bind9 1:9.3.4-2etch3 (diff) [302kB]
Fetched 4347kB in 1s (2971kB/s)
dpkg-source: extracting bind9 in bind9-9.3.4
dpkg-source: unpacking bind9_9.3.4.orig.tar.gz
dpkg-source: applying ./bind9_9.3.4-2etch3.diff.gz
gw1-livent:/usr/src#

We download the mysql-bind.tar.gz package from sourceforge.net (you may as well pick other mirror as i used Switch)

gw1-livent:/usr/src# wget http://switch.dl.sourceforge.net/sourceforge/mysql-bind/mysql-bind.tar.gz
–18:56:17– http://switch.dl.sourceforge.net/sourceforge/mysql-bind/mysql-bind.tar.gz
=> `mysql-bind.tar.gz’
Resolving switch.dl.sourceforge.net… 130.59.138.20, 2001:620:0:1b::20
Connecting to switch.dl.sourceforge.net|130.59.138.20|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 14,019 (14K) [application/x-tar]

100%[==============================================>] 14,019 –.–K/s

18:56:18 (93.41 KB/s) – `mysql-bind.tar.gz’ saved [14019/14019]

gw1-livent:/usr/src#

We unpack it and go into the newly created directory.

gw1-livent:/usr/src# tar zxvf mysql-bind.tar.gz
mysql-bind/
mysql-bind/COPYING
mysql-bind/mysqldb.h
mysql-bind/ChangeLog
mysql-bind/.cvsignore
mysql-bind/mysqldb.c~
mysql-bind/ChangeLog~
mysql-bind/CVS/
mysql-bind/CVS/Entries
mysql-bind/CVS/Root
mysql-bind/CVS/Repository
mysql-bind/mysqldb.c
mysql-bind/README
mysql-bind/README~
mysql-bind/zonetodb.c
gw1-livent:/usr/src# cd mysql-bind
gw1-livent:/usr/src/mysql-bind#

Now we will need to copy the necessary files into the bind9 source directory.

gw1-livent:/usr/src/mysql-bind# cp mysqldb.c ../bind9-9.3.4/bin/named/
gw1-livent:/usr/src/mysql-bind# cp mysqldb.c ../bind9-9.3.4/bin/named/include/
gw1-livent:/usr/src/mysql-bind# cp mysqldb.h ../bind9-9.3.4/bin/named/
gw1-livent:/usr/src/mysql-bind# cp mysqldb.h ../bind9-9.3.4/bin/named/include/

Now we will need to edit some files on the bind9 source before actually compiling it. But before we will need to install some other pre-requisites for this installation. As Debian default install doesnt have some packages that we need we will install them now.

gw1-livent:/usr/src/mysql-bind# apt-get install fakeroot debhelper libmysqlclient15-dev

We will edit the Makefile.in file in /usr/src/bind9-9.3.4/bin/named/ and add some proper values to DBDRIVER_OBJS, DBDRIVER_SRCS, DBDRIVER_INCLUDES and DBDRIVER_LIBS parameters. Now open /usr/src/bind9-9.3.4/bin/named/Makefile.in with your favourite editor and search for:

#
# Add database drivers here.
#
DBDRIVER_OBJS =
DBDRIVER_SRCS =
DBDRIVER_INCLUDES =
DBDRIVER_LIBS =

.. which should at this step look like

#
# Add database drivers here.
#
DBDRIVER_OBJS = mysqldb.@O@
DBDRIVER_SRCS = mysqldb.c
DBDRIVER_INCLUDES =
DBDRIVER_LIBS =

For the next two values we will need to find out mysql configuration values that we will insert.
First let’s find for DBDRIVER_INCLUDES:

gw1-livent:/usr/src/bind9-9.3.4/bin/named# mysql_config –cflags
-I/usr/include/mysql -DBIG_JOINS=1
gw1-livent:/usr/src/bind9-9.3.4/bin/named#

Second DBDRIVER_LIBS …

gw1-livent:/usr/src/bind9-9.3.4/bin/named# mysql_config –libs
-L/usr/lib/mysql -lmysqlclient
gw1-livent:/usr/src/bind9-9.3.4/bin/named#

Now we add these values to the same part in Makefile.in so it looks like:

#
# Add database drivers here.
#
DBDRIVER_OBJS = mysqldb.@O@
DBDRIVER_SRCS = mysqldb.c
DBDRIVER_INCLUDES = -I/usr/include/mysql -DBIG_JOINS=1
DBDRIVER_LIBS = -L/usr/lib/mysql -lmysqlclient

Ok, we are done with this file. We are gonna edit now /usr/src/bind9-9.3.4/bin/named/main.c. Open it in your favorite editor and look for:

#include <dst /result.h>

/*
* Defining NS_MAIN provides storage declarations (rather than extern)
* for variables in named/globals.h.
*/

After #include on a new line add #include “include/mysqldb.h” so it looks like:

#include <dst /result.h>
#include "include/mysqldb.h"

/*
* Defining NS_MAIN provides storage declarations (rather than extern)
* for variables in named/globals.h.
*/

In the same file main.c search for:

/*
* Add calls to register sdb drivers here.
*/
/* xxdb_init(); */

ns_server_create(ns_g_mctx, &ns_g_server);
}

and change it so it looks like

/*
* Add calls to register sdb drivers here.
*/
/* xxdb_init(); */
mysqldb_init();
ns_server_create(ns_g_mctx, &ns_g_server);
}

then search for:

/*
* Add calls to unregister sdb drivers here.
*/
/* xxdb_clear(); */

isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,

and make sure it looks like:

/*
* Add calls to unregister sdb drivers here.
*/
/* xxdb_clear(); */
mysqldb_clear();
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,

Save the file main.c and exit now. We are gonna make one more change in the file mysqldb.c that came in the mysql-bind package since it’s broken.
Open the file /usr/src/bind9-9.3.4/bin/named/mysqldb.c in your favorite text editor and look for the last include line that should look like:

#include <named /mysqldb.h>

Change the line so it looks:

#include “include/mysqldb.h”

As we are done editing the files we can now proceed with the build of bind9-mysql debian packages. Change the directory to /usr/src/bind9-9.3.4

gw1-livent:/usr/src/bind9-9.3.4/bin/named# cd ../../
gw1-livent:/usr/src/bind9-9.3.4#

Build it.

gw1-livent:/usr/src/bind9-9.3.4# dpkg-buildpackage -rfakeroot -b

If you get a message like:

dpkg-buildpackage: source package is bind9
dpkg-buildpackage: source version is 1:9.3.4-2etch3
dpkg-buildpackage: source changed by LaMont Jones
dpkg-buildpackage: host architecture i386
dpkg-buildpackage: source version without epoch 9.3.4-2etch3
dpkg-checkbuilddeps: Unmet build dependencies: bison texlive-latex-base xsltproc
dpkg-buildpackage: Build dependencies/conflicts unsatisfied; aborting.
dpkg-buildpackage: (Use -d flag to override.)

then you will need to install the extra required packages by issuing:

gw1-livent:/usr/src/bind9-9.3.4# apt-get install bison texlive-latex-base xsltproc

The apt-get install process will ask your confirmation regarding the installation of these packages and as soon as you agree it will go downloading and installing them. Assuming you didnt need to do this step or that you didnt even encountered we continue.
After the dpkg-buildpackage is done and successfull you should see at the end among the last few lines:

dpkg-deb: building package `bind9′ in `../bind9_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `bind9-host’ in `../bind9-host_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libbind-dev’ in `../libbind-dev_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libbind9-0′ in `../libbind9-0_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libdns22′ in `../libdns22_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libisc11′ in `../libisc11_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `liblwres9′ in `../liblwres9_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libisccc0′ in `../libisccc0_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `libisccfg1′ in `../libisccfg1_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `dnsutils’ in `../dnsutils_9.3.4-2etch3_i386.deb’.
dpkg-deb: building package `lwresd’ in `../lwresd_9.3.4-2etch3_i386.deb’.

That means that all required bind9 debian packages were built and they are available one directory before the bind9 source. Therefore we go up one directory:

gw1-livent:/usr/src/bind9-9.3.4# cd ../
gw1-livent:/usr/src#

and install our fresh built bind9 with mysql backend with all the other bind9 related libraries and utilities:

gw1-livent:/usr/src# dpkg -i bind9_9.3.4-2etch3_i386.deb bind9-doc_9.3.4-2etch3_all.deb bind9-host_9.3.4-2etch3_i386.deb dnsutils_9.3.4-2etch3_i386.deb libbind9-0_9.3.4-2etch3_i386.deb libbind-dev_9.3.4-2etch3_i386.deb libdns22_9.3.4-2etch3_i386.deb libisc11_9.3.4-2etch3_i386.deb libisccc0_9.3.4-2etch3_i386.deb libisccfg1_9.3.4-2etch3_i386.deb liblwres9_9.3.4-2etch3_i386.deb lwresd_9.3.4-2etch3_i386.deb

This is about all about building bind9 with mysql on Debian Etch.

Now you will need a basic structure for your mysql domain database and an example of how to add a domain to your named.conf.

The entry in named.conf will look something like this:

zone “mydomain.com” {
type master;
notify no;
database “mysqldb dbname tablename hostname user password”;
};

You should create a database for the driver and add one table for each domain. The following SQL will create a table for one domain. Change mydomain to something that will represent the domain more clearly.

CREATE TABLE mydomain (
name varchar(255) default NULL,
ttl int(11) default NULL,
rdtype varchar(255) default NULL,
rdata varchar(255) default NULL
) TYPE=MyISAM;

The following is an SQL dump of an example table for a domain. The entries should be familiar to anyone who is familiar with BIND zone files. Change it to match your domain.

INSERT INTO mydomain VALUES (‘mydomain.com’, 259200, ‘SOA’, ‘mydomain.com. www.mydomain.com. 200309181 28800 7200 86400 28800′);
INSERT INTO mydomain VALUES (‘mydomain.com’, 259200, ‘NS’, ‘ns0.mydomain.com.’);
INSERT INTO mydomain VALUES (‘mydomain.com’, 259200, ‘NS’, ‘ns1.mydomain.com.’);
INSERT INTO mydomain VALUES (‘mydomain.com’, 259200, ‘MX’, ‘10 mail.mydomain.com.’);
INSERT INTO mydomain VALUES (‘w0.mydomain.com’, 259200, ‘A’, ‘192.168.1.1′);
INSERT INTO mydomain VALUES (‘w1.mydomain.com’, 259200, ‘A’, ‘192.168.1.2′);
INSERT INTO mydomain VALUES (‘mydomain.com’, 259200, ‘Cname’, ‘w0.mydomain.com.’);
INSERT INTO mydomain VALUES (‘mail.mydomain.com’, 259200, ‘Cname’, ‘w0.mydomain.com.’);
INSERT INTO mydomain VALUES (‘ns0.mydomain.com’, 259200, ‘Cname’, ‘w0.mydomain.com.’);
INSERT INTO mydomain VALUES (‘ns1.mydomain.com’, 259200, ‘Cname’, ‘w1.mydomain.com.’);
INSERT INTO mydomain VALUES (‘www.mydomain.com’, 259200, ‘Cname’, ‘w0.mydomain.com.’);
INSERT INTO mydomain VALUES (‘ftp.mydomain.com’, 259200, ‘Cname’, ‘w0.mydomain.com.’);

With all these being said i really hope this article helps many of you. It took some time to elaborate and make it step by step but it worths. I will also attach the packages built for this tutorial for those that will simply want to get them, install them and have it running.

Note: the downloads below are available only for registered members of Digitalnerds.

Thank you very much for your attention.

The Digitalnerd

  bind9_9.3.4-2etch3_i386.deb (290.5 KiB, 23 hits)
You need to be a registered user to download this file.

  bind9-doc_9.3.4-2etch3_all.deb (183.4 KiB, 14 hits)
You need to be a registered user to download this file.

  bind9-host_9.3.4-2etch3_i386.deb (110.6 KiB, 14 hits)
You need to be a registered user to download this file.

  dnsutils_9.3.4-2etch3_i386.deb (176.3 KiB, 13 hits)
You need to be a registered user to download this file.

  libbind9-0_9.3.4-2etch3_i386.deb (92.7 KiB, 14 hits)
You need to be a registered user to download this file.

  libbind-dev_9.3.4-2etch3_i386.deb (973.2 KiB, 15 hits)
You need to be a registered user to download this file.

  libdns22_9.3.4-2etch3_i386.deb (461.4 KiB, 14 hits)
You need to be a registered user to download this file.

  libisc11_9.3.4-2etch3_i386.deb (166.0 KiB, 16 hits)
You need to be a registered user to download this file.

  libisccc0_9.3.4-2etch3_i386.deb (92.7 KiB, 15 hits)
You need to be a registered user to download this file.

  libisccfg1_9.3.4-2etch3_i386.deb (103.5 KiB, 14 hits)
You need to be a registered user to download this file.

  liblwres9_9.3.4-2etch3_i386.deb (107.7 KiB, 15 hits)
You need to be a registered user to download this file.

  lwresd_9.3.4-2etch3_i386.deb (203.3 KiB, 15 hits)
You need to be a registered user to download this file.




This entry was posted on Wednesday, August 6th, 2008 and is filed under Featured, Linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

16 Responses to “How To Install Bind9+MySQL”

  1. Sal

    can’t get this to work on debian 64-bit, any help?

  2. Andy

    There should be no difference as far as i know. Of course that the packages listed here for download will not work on Debian 64bit. You will need to compile it from scratch following the tutorial.. If you still have question dont hesitate to hit me up.

    Regards
    Andy

  3. lucas

    I found that I had to change the in Makefile.in to this:

    DBDRIVER_LIBS = -L/usr/lib/mysql -lmysqlclient -lm -lz

    The -lm and -lz for some reason need to be in a certain order and was causing a fail when building.

    Once changed, built right up just like the tutorial, worked perfectly.

  4. Andy

    I’m glad you sorted the problem out. I didnt ran into this problem since i dont have the extra -lm -lz flags in my mysql_config –libs output.
    However all is good when it ends good :)
    Happy my tutorial was helpful to you.

    Regards
    Andy

  5. lucas

    When it comes to have multiple dns servers what scenario do you think would work best?

    I’ve been going over the options and benefits of having just the primary run the mysql addon, but can’t get how the zone file will be updated on secondaries without restarting bind on the primary. (which was a major benefit with the mysql usage)

    So is mysql replication the only way? I guess if it is a smaller setup 2 dns servers just pull from the same mysql server over a lan, but when it comes to geographically dispersed dns servers, I don’t see how this would work, replicating over the internet seems to break a lot. Maybe you have a suggestion?

    BTW, I’ve followed the vsftpd tutorial also, I’m converting everything of mine to using mysql backend. DNS/ftp users/and soon to be email users with postfix. Keep them coming!

  6. Pedro

    I get this error:

    named[6061]: zone mydomain.com/IN: loading zone: creating database: failure and if i use a file with the same data everything works fine.

  7. Andy

    Pedro:
    You will first need to create your database. Already did that?.
    As in “create database domain;” (no qutes)

    lucas:
    Well mayeb just having the secondaries restart the bind9 daemon and it will just update? I woud really try that if i were you. ;)
    As about geographically dispersed dns servers mysql replication wouldnt break at all. Or at least it shouldnt. TCP checksums should really protect against data corruption in the replication process, however i would adivse doing it over an encrypted connection like a stunnel or similar.

    Regards

  8. Pedro

    Andy: yes de database is created and the zone i think link to the database like:

    zone “myadomain”{
    type master;
    database “mysqldb dbname tablename localhost usser pass”;
    //file “/etc/bind/mydomain.db”
    }

    whet i use the file source works ok but when i use the mysql database source i have the error i posted before.

  9. Andy

    Pedro

    Can you please check the logs of mysql and see in detail what is the problem. I personally think the user you are using to connect to mysql doesnt have permission to access the database you created to hold your domains tables. Looking forward to hear from you.

    Regards

  10. Tom

    What’s the difference between using mysql-bind (which seems to be rather old) and BIND DLZ which seems to be “supported” by BIND (hence the parameter –with-dlz-path for ./configure).

  11. Andy

    Hi Tom

    I never used DLZ. However after looking at their homepage i can tell you that the configuration in DLZ is far more complicated than that of bind-mysql. bind-mysql uses a very simple syntax.
    Compare:

    zone “mydomain.com” {
    type master;
    notify no;
    database “mysqldb dbname tablename hostname user password”;
    };

    with

    dlz “Mysql zone” {
    database “mysql
    {host=localhost dbname=dns_data ssl=tRue}
    {select zone from dns_records where zone = ‘%zone%’}
    {select ttl, type, mx_priority, case when lower(type)=’txt’ then concat(‘\”‘, data, ‘\”‘)
    else data end from dns_records where zone = ‘%zone%’ and host = ‘%record%’
    and not (type = ‘SOA’ or type = ‘NS’)}
    {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum
    from dns_records where zone = ‘%zone%’ and (type = ‘SOA’ or type=’NS’)}
    {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire,
    minimum from dns_records where zone = ‘%zone%’ and not (type = ‘SOA’ or type = ‘NS’)}
    {select zone from xfr_table where zone = ‘%zone%’ and client = ‘%client%’}
    {update data_count set count = count + 1 where zone =’%zone%’}”;
    };

    The thing is that bind-mysql preserves the standard BIND structure and naming convention while DMZ is changing it overall.

    Kind Regards
    Andy

  12. Pietie

    @Pedro

    Have you ever managed to get it to work? I am having the same problem, created the DB using the same user and pass I am able to do the lookups.

    Regards

  13. Andy

    I’m actually using this on production servers running well over 300 domains.

    Andy

  14. will

    Just found out the current version doesn’t support update. If you try an nsupdate you’ll get “servfail”.
    Any idea on when it’ll be available?

  15. jameslee

    can u tell me what this problem is, Andy?

    unsupported database type ‘mysqldb’
    zone mydomain.com/IN: loading zone: creating database: not found

  16. Andy

    @ will it supports update. There is something you did wrong there,

    @jameslee: you also did something wrong.

    As a notice i just installed 2 days ago a 64 bits debian and compiled the bind as outlined in this tutorial. It works flawlesly.

    Andy

Leave a Reply