使用EPEL源可直接安装:
yum install pdns pdns-backend-mysql
添加MySQL数据库:
create database powerdns;
grant all privileges on powerdns.* to 'powerdns'@'localhost' identified by 'password';
use powerdns;
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)
) Engine=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(10) DEFAULT NULL,
content VARCHAR(64000) DEFAULT NULL,
ttl INT DEFAULT NULL,
prio INT DEFAULT NULL,
change_date INT DEFAULT NULL,
primary key(id)
) Engine=InnoDB;
CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
create table supermasters (
ip VARCHAR(64) NOT NULL,
nameserver VARCHAR(255) NOT NULL,
account VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (ip, nameserver)
) Engine=InnoDB;
将SQL信息添加到PowerDNS配置文件:
launch=gmysql
gmysql-host=localhost
gmysql-user=powerdns
gmysql-password=password
gmysql-dbname=powerdns
添加域名记录:
INSERT INTO domains (name, type) values ('example.com', 'NATIVE');
insert into `records`(`domain_id`,`name`,`type`,`content`,`ttl`,`prio`,`change_date`) values
(1,'example.com','SOA','localhost root@example.com 1',86400,NULL,NULL);
insert into `records`(`domain_id`,`name`,`type`,`content`,`ttl`,`prio`,`change_date`) values
(1,'example.com','NS','ns.example.com',86400,NULL,NULL);
insert into `records`(`domain_id`,`name`,`type`,`content`,`ttl`,`prio`,`change_date`) values
(1,'example.com','A','192.0.2.10',120,NULL,NULL);
insert into `records`(`domain_id`,`name`,`type`,`content`,`ttl`,`prio`,`change_date`) values
(1,'*.example.com','A','192.0.2.10',120,NULL,NULL);
insert into `records`(`domain_id`,`name`,`type`,`content`,`ttl`,`prio`,`change_date`) values
(1,'ns.example.com','A','192.0.2.10',120,NULL,NULL);
看看效果:
/etc/init.d/pdns monitor
PHP:
<?php
try {
$dbo = new PDO('mysql:host=localhost;dbname=powerdns', 'powerdns', 'password');
} catch (PDOException $e) {
$error = $e->getMessage();
die("PDO Execute Error : ".$error."\n");
}
$domain = 'www.haiyun.me';
$sql = "SELECT `id` FROM domains WHERE NAME = \"$domain\"";
$stmt = $dbo->query($sql);
//var_dump($stmt);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
if ($result) {
$id = $result['id'];
} else {
$sql = "INSERT INTO domains (name, TYPE) VALUES (\"$domain\", 'NATIVE')";
$dbo->query($sql);
$id = $dbo->lastInsertId();
}
$sql = "INSERT INTO `records` (`domain_id`, `name`, `TYPE`, `content`, `ttl`) VALUES (:id, :name, :type, :content, :ttl)";
$stmt = $dbo->prepare($sql);
$stmt->execute(array(':id'=>$id, ':name'=>'a.'.$domain, ':type'=>'A', ':content'=>'192.168.1.1', ':ttl'=>'600'));
$stmt->execute(array(':id'=>$id, ':name'=>'b.'.$domain, ':type'=>'A', ':content'=>'192.168.1.1', ':ttl'=>'600'));
?>
标签:dns