Linuxest 2014-03-22
有时有这样的需求,一个公司会有许多域名需要管理。而这些域名和ip的对应经常变化,后台的操作人员不是linux人。下面的文档将实现这个需求。
1.安装lamp环境 
yum install mysql-server php* httpd 
/etc/init.d/mysqld start 
/etc/init.d/httpd start
2.安装bind服务 
tar zxvf bind-9.9.5.tar.gz -C /usr/src/ 
./configure –prefix=/usr/local/named –with-dlz-mysql –without-ssl –enable-threads 
make &&make install
3.配置bind服务及mysql数据库 
配置bind: 
/usr/local/named/sbin/rndc-confgen >/usr/local/named/etc/rndc.conf 
cd /usr/local/named/etc/ 
tail -10 rndc.conf | head -9 | sed s/#\ //g >named.conf 
vi  named.conf  ##在末尾添加如下内容 
dlz "DLZ use MySQL" { 
database "mysql 
{host=127.0.0.1 dbname=named ssl=false user=named pass=123123} 
{select zone from dns_records where zone = '$zone$'} 
{select ttl,type,mx_priority, case when lower(type)='txt' then concat('\"',data,'\"') when lower(type)='soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) 
else data end from dns_records where zone = '$zone$' and host = '$record$'}"; 
}; 
注意:host=127.0.0.1不要写主机名不然会报错,dbname是数据库的名字,user指定数据用户,pass指定密码。select是用查询的sql语句,提供给客户查询解析。 
创建数据库: 
这是一数据库创建完成后导出的文件可以复制,用vi创建named.sql文件 
[root@ns ~]# cat  named.sql 
-- MySQL dump 10.13  Distrib 5.1.55, for unknown-linux-gnu (x86_64) 
-- 
-- Host: localhost    Database: named 
-- ------------------------------------------------------ 
-- Server version    5.1.55-log 
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 
/*!40103 SET TIME_ZONE='+00:00' */; 
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 
-- 
-- Current Database: `named` 
-- 
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `named` /*!40100 DEFAULT CHARACTER SET latin1 */; 
USE `named`; 
-- 
-- Table structure for table `dns_records` 
-- 
DROP TABLE IF EXISTS `dns_records`; 
/*!40101 SET @saved_cs_client    = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `dns_records` ( 
  `zone` text, 
  `host` text, 
  `type` text, 
  `data` text NOT NULL, 
  `ttl` int(11) DEFAULT NULL, 
  `mx_priority` text, 
  `refresh` int(11) DEFAULT NULL, 
  `retry` int(11) DEFAULT NULL, 
  `expire` int(11) DEFAULT NULL, 
  `minimum` int(11) DEFAULT NULL, 
  `serial` bigint(20) DEFAULT NULL, 
  `resp_person` text, 
  `primary_ns` text 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 
-- 
-- Dumping data for table `dns_records` 
-- 
LOCK TABLES `dns_records` WRITE; 
/*!40000 ALTER TABLE `dns_records` DISABLE KEYS */; 
INSERT INTO `dns_records` VALUES ('linuxfan.cn','ns','A','192.168.8.79',800,NULL,NULL,10,NULL,NULL,NULL,NULL,NULL); 
/*!40000 ALTER TABLE `dns_records` ENABLE KEYS */; 
UNLOCK TABLES; 
-- 
-- Table structure for table `xfr_table` 
-- 
DROP TABLE IF EXISTS `xfr_table`; 
/*!40101 SET @saved_cs_client    = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `xfr_table` ( 
  `zone` text, 
  `client` text 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 
-- 
-- Dumping data for table `xfr_table` 
-- 
LOCK TABLES `xfr_table` WRITE; 
/*!40000 ALTER TABLE `xfr_table` DISABLE KEYS */; 
/*!40000 ALTER TABLE `xfr_table` ENABLE KEYS */; 
UNLOCK TABLES; 
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 
-- Dump completed on 2014-03-22 17:52:53 
[root@ns ~]# 
创建数据named并给权限 
mysqladm -uroot password 123123  ##给mysql  root修改密码 
mysql -uroot -p123123 
create database named; 
grant all on named.*  to 'named'@'127.0.0.1' identified by '123123';    ##密码必须与named.conf中一致 
flush privileges; 
quit; 
[root@ns ~]# mysql -uroot -p123123 - -database named <named.sql  ##导入数据库 
4.启动named服务 
/usr/local/named/sbin/named -gc /usr/local/named/etc/named.conf & 
netstat -uptln |grep named  
[root@ns ~]# cat /etc/resolv.conf    ##指定dns服务 
search linuxfan.cn 
nameserver 192.168.8.79 
[root@ns ~]# /usr/local/named/bin/nslookup ns.linuxfan.cn 
Server:        192.168.8.79 
Address:    192.168.8.79#53 
Name:    ns.linuxfan.cn 
Address: 192.168.8.79 
测试成功。 
5.额外的设置,安装phpmyadmin作为dns的后台管理,对于对数据库不是很懂的朋友,可以看看。 
phpMyAdmin-3.3.10-all-languages.tar.gz软件包,注意要安装php5.3。如果是如果是php版本不是5.3及以上可使用phpmyadmin-2.11.11.3-all-languages.tar.gz安装。 
命令如下: 
tar zxvf phpmyadmin-3.3.10-all-languages.tar.gz -C /usr/src/ 
cd /usr/src/ 
mv phpMyAdmin-3.3.10-all-languages/ /var/www/html/phpmyadm 
cd /var/www/html/phpmyadm 
cp config.sample.inc.php config.inc.php  ##建立配置文件
使用浏览器访问登录即可:

phpMyAdmin 的详细介绍:请点这里
phpMyAdmin 的下载地址:请点这里
推荐阅读:
zone "localdomain" IN { type master; file "localdomain.zone"; allow-update { none; }; };zone "1.1.1.