Running A MySQL-Based DNS Server: MyDNS
Author: Falko Timme
Last edited: 01/02/2006
In this tutorial I will describe how to install and configure MyDNS, a DNS server that uses a MySQL database as backend instead of configuration files like, for example, Bind or djbdns. This has the advantage that you can easily use web-based frontends to administrate your DNS records. You could even write your own frontend, e.g. using PHP, to interact with the MyDNS database. MyDNS simply reads the records from the database, and it does not have to be restarted/reloaded when DNS records change or zones are created/edited/deleted! This is a major advantage.
I will also show how to set up a secondary DNS server, but I will not use normal zone transfers to get the records from the primary to the secondary DNS server; instead, I will use MySQL database replication. I will simply replicate the data from the primary to the secondary server.
There are multiple web frontends to MyDNS. Of course, you can use phpMyAdmin for the MyDNS administration, or you can use the web frontend that comes with MyDNS (which is not for beginners because it lacks the field descriptions), or you can use MyDNSConfig, a tool written by myself. I will show how to install each of them.
All in all, MyDNS makes a very good impression on me, and I think that the future belongs to services that use a database as backend instead of configuration files.
I have used a Debian Sarge system for this setup. However, most of the steps described here should also apply to other distributions like Fedora, SuSE, Mandriva, etc.
I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!
1 Install MySQL And MyDNS
First, make sure you have a working basic Debian installation, as described on http://www.howtoforge.com/perfect_setup_debian_sarge and http://www.howtoforge.com/perfect_setup_debian_sarge_p2 (please refer to the respective "Perfect Setup" document on HowtoForge if you use another distribution).
Then install MySQL:
apt-get install mysql-server mysql-client libmysqlclient12-dev phpmyadmin
You will be asled a few questions:
Enable suExec? <-- Yes
Which web server would you like to reconfigure automatically? <-- apache, apache2
Do you want me to restart apache now? <-- Yes
Set a root MySQL password like this:
mysqladmin -u root password yourrootsqlpassword
The MyDNS installation is easy. Just follow these steps:
tar xvfz mydns-1.0.0.tar.gz
Now we have to create the MyDNS database (called mydns) and a MyDNS database user (also called mydns, with mydns_password as password):
mysql -u root -p
Now, on the MySQL shell, we create the mydns database and the user mydns:
CREATE DATABASE mydns;
GRANT SELECT, INSERT, UPDATE, DELETE ON mydns.* TO 'mydns'@'localhost' IDENTIFIED BY 'mydns_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydns.* TO 'mydns'@'localhost.localdomain' IDENTIFIED BY 'mydns_password';
Back on the normal shell, we create the tables in the mydns database:
mydns --create-tables | mysql -u root -p mydns
If you see errors like these:
mydns: error loading gid for group `nobody'
mydns: using gid 65534 from user `nobody'
do not worry, you can forget about them.
Next we add two more columns to the soa table of the mydns database:
mysql -u root -p
ALTER TABLE mydns.soa ADD COLUMN active ENUM('Y','N') NOT NULL;
ALTER TABLE mydns.soa ADD INDEX (active);
ALTER TABLE mydns.soa ADD COLUMN xfer CHAR(255) NOT NULL;
With the active column, you can simply enable/disable zones later on, and the xfer column can be used for zone transfers (which we do not need because we will use MySQL database replication to get the data to the secondary DNS server, but I mention it for completeness) (if you want to use zone transfers instead of database replication, then the xfer column should contain one or more IP addresses separated by commas. These IP addresses will be allowed to transfer the zone.).
Now we create the MyDNS configuration file /etc/mydns.conf by running this command:
Then we edit /etc/mydns.conf so that it looks like this:
Make sure to fill in the correct database details, and also take care about the group. On Debian, nobody's group is nogroup, which is different from the default value in /etc/mydns.conf (also nobody), so make sure you change this! Also set allow-tcp to yes.
Now we create the MyDNS init script /etc/init.d/mydns:
Then we make it executable and start MyDNS:
chmod 755 /etc/init.d/mydns
If you want that MyDNS is started automatically during the system boot, you have to create the necessary system bootup links. On Debian, you do it like this:
update-rc.d mydns defaults 21 22
On Fedora or RedHat, you would run this:
chkconfig --levels 235 mydns on