<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Generic MySQL and PgSQL backends</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PowerDNS manual" HREF="index.html"><LINK REL="UP" TITLE="Backends in detail" HREF="backends-detail.html"><LINK REL="PREVIOUS" TITLE="MySQL PDNS backend" HREF="pdnsbackend.html"><LINK REL="NEXT" TITLE="Oracle backend" HREF="oracle.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PowerDNS manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="pdnsbackend.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Appendix A. Backends in detail</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="oracle.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="GENERIC-MYPGSQL-BACKENDS" >A.5. Generic MySQL and PgSQL backends</A ></H1 ><P > <DIV CLASS="TABLE" ><A NAME="AEN4773" ></A ><P ><B >Table A-5. Generic PgSQL and MySQL backend capabilities</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><TBODY ><TR ><TD >Native</TD ><TD >Yes - but PostgreSQL does not replicate</TD ></TR ><TR ><TD >Master</TD ><TD >Yes</TD ></TR ><TR ><TD >Slave</TD ><TD >Yes</TD ></TR ><TR ><TD >Superslave</TD ><TD >Yes</TD ></TR ><TR ><TD >Autoserial</TD ><TD >NO</TD ></TR ><TR ><TD >Case</TD ><TD >All lower</TD ></TR ><TR ><TD >Module name < 2.9.3</TD ><TD >pgmysql</TD ></TR ><TR ><TD >Module name > 2.9.2</TD ><TD >gmysql and gpgsql</TD ></TR ><TR ><TD >Lauch name</TD ><TD >gmysql and gpgsql2 and gpgsql</TD ></TR ></TBODY ></TABLE ></DIV > </P ><P > PostgreSQL and MySQL backend with easily configurable SQL statements, allowing you to graft PDNS on any PostgreSQL or MySQL database of your choosing. Because all database schemas will be different, a generic backend is needed to cover all needs. </P ><P > The template queries are expanded using the C function 'snprintf' which implies that substitutions are performed on the basis of %-place holders. To place a a % in a query which will not be substituted, use %%. Make sure to fill out the search key, often called 'name' in lower case! </P ><P > There are in fact two backends, one for PostgreSQL and one for MySQL but they accept the same settings and use almost exactly the same database schema. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN4807" >A.5.1. MySQL specifics</A ></H2 ><P > <DIV CLASS="WARNING" ><P ></P ><TABLE CLASS="WARNING" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="../images/warning.gif" HSPACE="5" ALT="Warning"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P > If using MySQL with 'slave' support enabled in PowerDNS you <B CLASS="COMMAND" >must</B > run MySQL with a table engine that supports transactions. </P ></TD ></TR ></TABLE ></DIV > </P ><P > In practice, great results are achieved with the 'InnoDB' tables. PowerDNS will silently function with non-transaction aware MySQLs but at one point this is going to harm your database, for example when an incoming zone transfer fails. </P ><P > The default setup conforms to the following schema: <PRE CLASS="PROGRAMLISTING" >create table domains ( id INT auto_increment, name VARCHAR(255) NOT NULL, master VARCHAR(128) DEFAULT NULL, last_check INT DEFAULT NULL, type VARCHAR(6) NOT NULL, notified_serial INT DEFAULT NULL, account VARCHAR(40) DEFAULT NULL, primary key (id) )type=InnoDB; CREATE UNIQUE INDEX name_index ON domains(name); CREATE TABLE records ( id INT auto_increment, domain_id INT DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, type VARCHAR(6) DEFAULT NULL, content VARCHAR(255) DEFAULT NULL, ttl INT DEFAULT NULL, prio INT DEFAULT NULL, change_date INT DEFAULT NULL, primary key(id) )type=InnoDB; CREATE INDEX rec_name_index ON records(name); CREATE INDEX nametype_index ON records(name,type); CREATE INDEX domain_id ON records(domain_id); create table supermasters ( ip VARCHAR(25) NOT NULL, nameserver VARCHAR(255) NOT NULL, account VARCHAR(40) DEFAULT NULL ); GRANT SELECT ON supermasters TO pdns; GRANT ALL ON domains TO pdns; GRANT ALL ON records TO pdns; </PRE > </P ><P > Zone2sql with the --gmysql flag also assumes this layout is in place. </P ><P > This schema contains all elements needed for master, slave and superslave operation. Depending on which features will be used, the 'GRANT' statements can be trimmed to make sure PDNS cannot subvert the contents of your database. </P ><P > When using the InnoDB storage engine, we suggest adding the following lines to the 'create table records' command above: <PRE CLASS="PROGRAMLISTING" >CONSTRAINT `records_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`) ON DELETE CASCADE</PRE > </P ><P > This automates deletion of records on deletion of a domain from the domains table. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN4821" >A.5.2. PostgresSQL specifics</A ></H2 ><P > The default setup conforms to the following schema, which you should add to a PostgreSQL database. <PRE CLASS="PROGRAMLISTING" >create table domains ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, master VARCHAR(128) DEFAULT NULL, last_check INT DEFAULT NULL, type VARCHAR(6) NOT NULL, notified_serial INT DEFAULT NULL, account VARCHAR(40) DEFAULT NULL ); CREATE UNIQUE INDEX name_index ON domains(name); CREATE TABLE records ( id SERIAL PRIMARY KEY, domain_id INT DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, type VARCHAR(6) DEFAULT NULL, content VARCHAR(255) DEFAULT NULL, ttl INT DEFAULT NULL, prio INT DEFAULT NULL, change_date INT DEFAULT NULL, CONSTRAINT domain_exists FOREIGN KEY(domain_id) REFERENCES domains(id) ON DELETE CASCADE ); CREATE INDEX rec_name_index ON records(name); CREATE INDEX nametype_index ON records(name,type); CREATE INDEX domain_id ON records(domain_id); create table supermasters ( ip VARCHAR(25) NOT NULL, nameserver VARCHAR(255) NOT NULL, account VARCHAR(40) DEFAULT NULL ); GRANT SELECT ON supermasters TO pdns; GRANT ALL ON domains TO pdns; GRANT ALL ON domains_id_seq TO pdns; GRANT ALL ON records TO pdns; GRANT ALL ON records_id_seq TO pdns; </PRE > </P ><P > This schema contains all elements needed for master, slave and superslave operation. Depending on which features will be used, the 'GRANT' statements can be trimmed to make sure PDNS cannot subvert the contents of your database. </P ><P > Zone2sql with the --gpgsql flag also assumes this layout is in place. </P ><P > With PostgreSQL, you may have to run 'createdb powerdns' first and then connect to that database with 'psql powerdns', and feed it the schema above. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="GORACLE" >A.5.3. Oracle specifics</A ></H2 ><P > Generic Oracle support is only available since version 2.9.18. The default setup conforms to the following schema, which you should add to an Oracle database. You may need or want to add 'namespace' statements. <PRE CLASS="PROGRAMLISTING" > create table domains ( id NUMBER, name VARCHAR(255) NOT NULL, master VARCHAR(128) DEFAULT NULL, last_check INT DEFAULT NULL, type VARCHAR(6) NOT NULL, notified_serial INT DEFAULT NULL, account VARCHAR(40) DEFAULT NULL, primary key (id) ); create sequence DOMAINS_ID_SEQUENCE; create index DOMAINS$NAME on Domains (NAME); CREATE TABLE records ( id number(11) not NULL, domain_id INT DEFAULT NULL REFERENCES Domains(ID) ON DELETE CASCADE, name VARCHAR(255) DEFAULT NULL, type VARCHAR(6) DEFAULT NULL, content VARCHAR(255) DEFAULT NULL, ttl INT DEFAULT NULL, prio INT DEFAULT NULL, change_date INT DEFAULT NULL, primary key (id) ); create index RECORDS$NAME on RECORDS (NAME); create sequence RECORDS_ID_SEQUENCE; create table supermasters ( ip VARCHAR(25) NOT NULL, nameserver VARCHAR(255) NOT NULL, account VARCHAR(40) DEFAULT NULL ); </PRE > </P ><P > This schema contains all elements needed for master, slave and superslave operation. Depending on which features will be used, 'GRANT' statements can be trimmed to make sure PDNS cannot subvert the contents of your database. </P ><P > Zone2sql with the --gpgsql flag also assumes this layout is in place. </P ><P > Inserting records is a bit different compared to MySQL and PostgreSQL, you should use: <PRE CLASS="SCREEN" >insert into domains (id,name,type) values (domains_id_sequence.nextval,'netherlabs.nl','NATIVE'); </PRE > </P ><P > Furthermore, use the <B CLASS="COMMAND" >goracle-tnsname</B > setting to specify which TNSNAME the Generic Oracle Backend should be connectiong to. There are no <B CLASS="COMMAND" >goracle-dbname</B >, <B CLASS="COMMAND" >goracle-host</B > or <B CLASS="COMMAND" >goracle-port</B > settings, their equivalent is in <TT CLASS="FILENAME" >/etc/tnsnames.ora</TT >. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN4842" >A.5.4. Basic functionality</A ></H2 ><P > 4 queries are needed for regular lookups, 4 for 'fancy records' which are disabled by default and 1 is needed for zone transfers. </P ><P >The 4+4 regular queries must return the following 6 fields, in this exact order: <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >content</DT ><DD ><P > This is the 'right hand side' of a DNS record. For an A record, this is the IP address for example. </P ></DD ><DT >ttl</DT ><DD ><P > TTL of this record, in seconds. Must be a real value, no checking is performed. </P ></DD ><DT >prio</DT ><DD ><P > For MX records, this should be the priority of the mail exchanger specified. </P ></DD ><DT >qtype</DT ><DD ><P > The ASCII representation of the qtype of this record. Examples are 'A', 'MX', 'SOA', 'AAAA'. Make sure that this field returns an exact answer - PDNS won't recognise 'A ' as 'A'. This can be achieved by using a VARCHAR instead of a CHAR. </P ></DD ><DT >domain_id</DT ><DD ><P > Each domain must have a unique domain_id. No two domains may share a domain_id, all records in a domain should have the same. A number. </P ></DD ><DT >name</DT ><DD ><P > Actual name of a record. Must not end in a '.' and be fully qualified - it is not relative to the name of the domain! </P ></DD ></DL ></DIV > Please note that the names of the fields are not relevant, but the order is! </P ><P > As said earlier, there are 8 SQL queries for regular lookups. To configure them, set 'gmysql-basic-query' or 'gpgsql-basic-query', depending on your choice of backend. If so called 'MBOXFW' fancy records are not used, four queries remain: <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >basic-query</DT ><DD ><P > Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where type='%s' and name='%s'</B > This is the most used query, needed for doing 1:1 lookups of qtype/name values. First %s is replaced by the ASCII representation of the qtype of the question, the second by the name. </P ></DD ><DT >id-query</DT ><DD ><P > Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where type='%s' and name='%s' and domain_id=%d</B > Used for doing lookups within a domain. First %s is replaced by the qtype, the %d which should appear after the %s by the numeric domain_id. </P ></DD ><DT >any-query</DT ><DD ><P > For doing ANY queries. Also used internally. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where name='%s'</B > The %s is replaced by the qname of the question. </P ></DD ><DT >any-id-query</DT ><DD ><P > For doing ANY queries within a domain. Also used internally. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where name='%s' and domain_id=%d</B > The %s is replaced by the name of the domain, the %d by the numerical domain id. </P ></DD ></DL ></DIV > </P ><P > The last query is for listing the entire contents of a zone. This is needed when performing a zone transfer, but sometimes also internally: <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >list-query</DT ><DD ><P > To list an entire zone. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where domain_id=%d</B > </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="MASTER-SLAVE-QUERIES" >A.5.5. Master/slave queries</A ></H2 ><P > Most installations will have zero need to change the following settings, but should the need arise, here they are: <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >master-zone-query</DT ><DD ><P > Called to determine the master of a zone. Default: <B CLASS="COMMAND" >select master from domains where name='%s' and type='SLAVE'</B > </P ></DD ><DT >info-zone-query</DT ><DD ><P > Called to retrieve (nearly) all information for a domain: Default: <B CLASS="COMMAND" >select id,name,master,last_check,notified_serial,type from domains where name='%s'</B > </P ></DD ><DT >info-all-slaves-query</DT ><DD ><P > Called to retrieve all slave domains Default: <B CLASS="COMMAND" >select id,name,master,last_check,type from domains where type='SLAVE'</B > </P ></DD ><DT >supermaster-query</DT ><DD ><P > Called to determine if a certain host is a supermaster for a certain domain name. Default: <B CLASS="COMMAND" > select account from supermasters where ip='%s' and nameserver='%s'"); </B > </P ></DD ><DT >insert-slave-query</DT ><DD ><P > Called to add a domain as slave after a supermaster notification. Default: <B CLASS="COMMAND" > insert into domains (type,name,master,account) values('SLAVE','%s','%s','%s') </B > </P ></DD ><DT >insert-record-query</DT ><DD ><P > Called during incoming AXFR. Default: <B CLASS="COMMAND" > insert into records (content,ttl,prio,type,domain_id,name) values ('%s',%d,%d,'%s',%d,'%s') </B > </P ></DD ><DT >update-serial-query</DT ><DD ><P > Called to update the last notified serial of a master domain. Default: <B CLASS="COMMAND" > update domains set notified_serial=%d where id=%d </B > </P ></DD ><DT >update-lastcheck-query</DT ><DD ><P > Called to update the last time a slave domain was checked for freshness. Default: <B CLASS="COMMAND" > update domains set notified_serial=%d where id=%d </B > </P ></DD ><DT >info-all-master-query</DT ><DD ><P > Called to get data on all domains for which the server is master. Default: <B CLASS="COMMAND" > select id,name,master,last_check,notified_serial,type from domains where type='MASTER' </B > </P ></DD ><DT >delete-zone-query</DT ><DD ><P > Called to delete all records of a zone. Used before an incoming AXFR. Default: <B CLASS="COMMAND" > delete from records where domain_id=%d </B > </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN4954" >A.5.6. Fancy records</A ></H2 ><P > If PDNS is used with so called 'Fancy Records', the 'MBOXFW' record exists which specifies an email address forwarding instruction, wildcard queries are sometimes needed. This is not enabled by default. A wildcard query is an internal concept - it has no relation to *.domain-type lookups. You can safely leave these queries blank. <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >wildcard-query</DT ><DD ><P > Can be left blank. See above for an explanation. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where type='%s' and name like '%s'</B > </P ></DD ><DT >wildcard-id-query</DT ><DD ><P > Can be left blank. See above for an explanation. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where type='%s' and name like '%s' and domain_id=%d</B > Used for doing lookups within a domain. </P ></DD ><DT >wildcard-any-query</DT ><DD ><P > For doing wildcard ANY queries. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where name like '%s'</B > </P ></DD ><DT >wildcard-any-id-query</DT ><DD ><P > For doing wildcard ANY queries within a domain. Default: <B CLASS="COMMAND" >select content,ttl,prio,type,domain_id,name from records where name like '%s' and domain_id=%d</B > </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN4978" >A.5.7. Settings and specifying queries</A ></H2 ><P > The queries above are specified in pdns.conf. For example, the basic-query would appear as: <PRE CLASS="SCREEN" > gpgsql-basic-query=select content,ttl,prio,type,domain_id,name from records where type='%s' and name='%s' </PRE > When using the Generic PostgreSQL backend, they appear as above. When using the generic MySQL backend, change the "gpgsql-" prefix to "gmysql-". </P ><P > Queries can span multiple lines, like this: <PRE CLASS="SCREEN" > gpgsql-basic-query=select content,ttl,prio,type,domain_id,name from records \ where type='%s' and name='%s' </PRE > Do not wrap statements in quotes as this will not work. Besides the query related settings, the following configuration options are available: </P ><P > <P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >gpgsql-dbname</DT ><DD ><P > Database name to connect to </P ></DD ><DT >gpgsql-host</DT ><DD ><P > Database host to connect to. WARNING: When specified as a hostname a chicken/egg situation might arise where the database is needed to resolve the IP address of the database. It is best to supply an IP address of the database here. </P ></DD ><DT >gmysql-socket (only for MySQL!)</DT ><DD ><P > Filename where the MySQL connection socket resides. Often <TT CLASS="FILENAME" >/tmp/mysql.sock</TT > or <TT CLASS="FILENAME" >/var/run/mysqld/mysqld.sock</TT >. </P ></DD ><DT >gpgsql-password</DT ><DD ><P > Password to connect with </P ></DD ><DT >gpgsql-user</DT ><DD ><P > PgSQL user to connect as </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN5008" >A.5.8. Native operation</A ></H2 ><P > For native operation, either drop the FOREIGN KEY on the domain_id field, or (recommended), make sure the <B CLASS="COMMAND" >domains</B > table is filled properly. To add a domain, issue the following: <PRE CLASS="PROGRAMLISTING" > insert into domains (name,type) values ('powerdns.com','NATIVE'); </PRE > The records table can now be filled by with the domain_id set to the id of the domains table row just inserted. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN5013" >A.5.9. Slave operation</A ></H2 ><P > The PostgreSQL backend is fully slave capable. To become a slave of the 'powerdns.com' domain, execute this: <PRE CLASS="PROGRAMLISTING" > insert into domains (name,master,type) values ('powerdns.com','213.244.168.217','SLAVE'); </PRE > And wait a while for PDNS to pick up the addition - which happens within one minute. There is no need to inform PDNS that a new domain was added. Typical output is: <PRE CLASS="PROGRAMLISTING" > Apr 09 13:34:29 All slave domains are fresh Apr 09 13:35:29 1 slave domain needs checking Apr 09 13:35:29 Domain powerdns.com is stale, master serial 1, our serial 0 Apr 09 13:35:30 [gPgSQLBackend] Connected to database Apr 09 13:35:30 AXFR started for 'powerdns.com' Apr 09 13:35:30 AXFR done for 'powerdns.com' Apr 09 13:35:30 [gPgSQLBackend] Closing connection </PRE > </P ><P > From now on, PDNS is authoritative for the 'powerdns.com' zone and will respond accordingly for queries within that zone. </P ><P > Periodically, PDNS schedules checks to see if domains are still fresh. The default <B CLASS="COMMAND" >slave-cycle-interval</B > is 60 seconds, large installations may need to raise this value. Once a domain has been checked, it will not be checked before its SOA refresh timer has expired. Domains whose status is unknown get checked every 60 seconds by default. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN5021" >A.5.10. Superslave operation</A ></H2 ><P > To configure a supermaster with IP address 10.0.0.11 which lists this installation as 'autoslave.powerdns.com', issue the following: <PRE CLASS="PROGRAMLISTING" > insert into supermasters ('10.0.0.11','autoslave.powerdns.com','internal'); </PRE > From now on, valid notifies from 10.0.0.11 that list a NS record containing 'autoslave.powerdns.com' will lead to the provisioning of a slave domain under the account 'internal'. See <A HREF="slave.html#SUPERMASTER" >Section 13.2.1</A > for details. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN5026" >A.5.11. Master operation</A ></H2 ><P > The PostgreSQL backend is fully master capable with automatic discovery of serial changes. Raising the serial number of a domain suffices to trigger PDNS to send out notifications. To configure a domain for master operation instead of the default native replication, issue: <PRE CLASS="PROGRAMLISTING" > insert into domains (name,type) values ('powerdns.com','MASTER'); </PRE > Make sure that the assigned id in the domains table matches the domain_id field in the records table! </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pdnsbackend.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="oracle.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >MySQL PDNS backend</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="backends-detail.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Oracle backend</TD ></TR ></TABLE ></DIV ></BODY ></HTML >