Chapter 4. Configure database connectivity

The default PDNS distribution comes with a simple MySQL backend built in, which we will now use for demonstrating database connectivity. This backend is called 'mysql', and needs to be configured in pdns.conf. Add the following lines, adjusted for your local setup:

	launch=mysql
	mysql-host=127.0.0.1
	mysql-user=root
	mysql-dbname=pdnstest
      
Remove any earlier launch statements. Also remove the bind-example-zones statement as the bind module is no longer launched.

WARNING! Make sure that you can actually resolve the hostname of your database without accessing the database! It is advised to supply an IP address here to prevent chicken/egg problems!

Now start PDNS using the monitor command:

	# /etc/init.d/pdns monitor
	(...)
	15:31:30 PowerDNS 1.99.0 (Mar 12 2002, 15:00:28) starting up
	15:31:30 About to create 3 backend threads
	15:31:30 [MySQLbackend] Failed to connect to database: Error: Unknown database 'pdnstest'
	15:31:30 [MySQLbackend] Failed to connect to database: Error: Unknown database 'pdnstest'
	15:31:30 [MySQLbackend] Failed to connect to database: Error: Unknown database 'pdnstest'
      
This is as to be expected - we did not yet add anything to MySQL for PDNS to read from. At this point you may also see other errors which indicate that PDNS either could not find your MySQL server or was unable to connect to it. Fix these before proceding.

General MySQL knowledge is assumed in this chapter, please do not interpret these commands as DBA advice!

4.1. Configuring MySQL

Connect to MySQL as a user with sufficient privileges and issue the following commands:

	  # mysql 
	  mysql> CREATE DATABASE pdnstest;
	  mysql> use pdnstest;

	  mysql> CREATE TABLE records (
	  id int(11) NOT NULL auto_increment,
	  domain_id int(11) default NULL,
	  name varchar(255) default NULL,
	  type varchar(6) default NULL,
	  content varchar(255) default NULL,
	  ttl int(11) default NULL,
	  prio int(11) default NULL,
	  change_date int(11) default NULL,
	  PRIMARY KEY (id),
	  KEY name_index(name),
	  KEY nametype_index(name,type),
	  KEY domainid_index(domain_id)
	  );
	
Now we have a database and an empty table. PDNS should now be able to launch in monitor mode and display no errors:
	  # /etc/init.d/pdns monitor
	  (...)
	  15:31:30 PowerDNS 1.99.0 (Mar 12 2002, 15:00:28) starting up
	  15:31:30 About to create 3 backend threads
	  15:39:55 [MySQLbackend] MySQL connection succeeded
	  15:39:55 [MySQLbackend] MySQL connection succeeded
	  15:39:55 [MySQLbackend] MySQL connection succeeded
	
A sample query sent to the database should now return quickly without data:
	  $ host www.test.com 127.0.0.1
	  www.test.com A record currently not present at snapcount
	
And indeed, the control console now shows:
	  Mar 12 15:41:12 We're not authoritative for 'www.test.com', sending unauth normal response
	
Now we need to add some records to our database:
	  # mysql pdnstest
	  mysql>
	  INSERT INTO records (domain_id, name, content, type,ttl,prio) 
	  VALUES (1,'test.com','localhost ahu@ds9a.nl 1','SOA',86400,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'test.com','dns-us1.powerdns.net','NS',86400,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'test.com','dns-eu1.powerdns.net','NS',86400,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'www.test.com','199.198.197.196','A',120,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'mail.test.com','195.194.193.192','A',120,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'localhost.test.com','127.0.0.1','A',120,NULL);
	  INSERT INTO records (domain_id, name, content, type,ttl,prio)
	  VALUES (1,'test.com','mail.test.com','MX',120,25);
	
If we now requery our database, www.test.com should be present:
	  $ host www.test.com 127.0.0.1
	  www.test.com        	A	199.198.197.196
	  
	  $ host -v -t mx test.com 127.0.0.1
	  Address: 127.0.0.1
	  Aliases: localhost

	  Query about test.com for record types MX
	  Trying test.com ...
	  Query done, 1 answer, authoritative status: no error
	  test.com            	120	IN	MX	25 mail.test.com
	  Additional information:
	  mail.test.com       	120	IN	A	195.194.193.192
	
To confirm what happened, issue the command SHOW * to the control console:
	  % show *
	  corrupt-packets=0,latency=0,packetcache-hit=2,packetcache-miss=5,packetcache-size=0,
	  qsize-a=0,qsize-q=0,servfail-packets=0,tcp-answers=0,tcp-queries=0,
	  timedout-packets=0,udp-answers=7,udp-queries=7,
	  % 
	
The actual numbers will vary somewhat. Now enter QUIT and start PDNS as a regular daemon, and check launch status:
	  # /etc/init.d/pdns start 
	  pdns: started
	  # /etc/init.d/pdns status
	  pdns: 8239: Child running
	  # /etc/init.d/pdns dump  
	  pdns: corrupt-packets=0,latency=0,packetcache-hit=0,packetcache-miss=0,
	  packetcache-size=0,qsize-a=0,qsize-q=0,servfail-packets=0,tcp-answers=0,
	  tcp-queries=0,timedout-packets=0,udp-answers=0,udp-queries=0,
	
You now have a working database driven nameserver! To convert other zones already present, use the zone2sql described in Appendix A.