1.先安裝MySQL,直接yum安裝
* yum -y install mysql mysql-server2.再安裝一些依賴的包mysql
3.下載bind, bind官網( https://www.isc.org/)
4.下載mysql-bind 補丁源碼
5.解壓bind和mysql-bind源碼壓縮文件
tar zxvf bind-9.10.3-P2.tar.gz tar zxvf mysql-bind.tar.gz
6.將mysql-bind源碼目錄下的mysqldb.c 和 mysqldb.h拷貝到bind源碼目錄下的bin/named和bin/named/include/ 目錄下
cd mysql-bind cp -f mysqldb.c mysqldb.h ../bind-9.10.3-P2/bin/named/ cp -f mysqldb.c mysqldb.h ../bind-9.10.3-P2/bin/named/include/
7.修改bind源碼目錄下bin/named/Makefile.in文件
cd ../bind-9.10.3-P2 vim bin/named/Makefile.in 將如下幾行: DBDRIVER_OBJS = DBDRIVER_SRCS = DBDRIVER_INCLUDES = DBDRIVER_LIBS = 修改成: DBDRIVER_OBJS = mysqldb.@O@ DBDRIVER_SRCS = mysqldb.c DBDRIVER_INCLUDES = -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX DBDRIVER_LIBS = -rdynamic -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -lssl -lcrypto DBDRIVER_INCLUDES 的值是經過 mysql_config --cflags 命令獲取的 DBDRIVER_LIBS 的值是經過 mysql_config --libs 命令獲取的 8.修改bind源碼目錄下bin/named目錄下的main.c文件 vim bin/named/main.c 加入 #include "mysqldb.h" 以下: #include <config.h> #include "mysqldb.h" #include <ctype.h> #include <stdlib.h> #include <string.h> 而後在註釋代碼段 /* xxdb_init(); */ 後加入 mysqldb_init(); 在註釋代碼段 /* xxdb_clear(); */後加入 mysqldb_clear();
9.修改mysqldb.c
10.安裝bind
./configure --prefix=/usr/local/bind --enable-threads # 指定安裝目錄和開啓多線程的處理能力 make make install
11.配置bind
cd /usr/local/bind-9.10/etc /usr/local/bind/sbin/rndc-confgen -r /dev/urandom>rndc.conf cat rndc.conf|tail |head -9 |sed "s/^#//g">named.conf # 生成配置文件
12.建立一個數據庫
############這版不太好用 Create database mydomain; 建立一張數據表 CREATE TABLE dnsrecord ( name varchar(255) default NULL, ttl int(11) default NULL, rdtype varchar(255) default NULL, rdata varchar(255) default NULL )TYPE=MyISAM; 插入一些測試數據 INSERT INTO dnsrecord VALUES ('test.net', 259200, 'SOA', 'test.net. www.test.net 200505101 28800 7200 86400 28800'); INSERT INTO dnsrecord VALUES ('test.net', 259200, 'NS', 'ns1.test.net.'); INSERT INTO dnsrecord VALUES ('ns1.test.net', 259200, 'A', '192.168.2.2'); INSERT INTO dnsrecord VALUES ('www.test.net', 259200, 'A', '192.168.2.1');
##########################好使############################### DROP TABLE IF EXISTS 10_outside; CREATE TABLE 10_outside ( name varchar(255) default NULL, ttl int(11) default NULL, rdtype varchar(255) default NULL, rdata varchar(255) default NULL ); -- -- Dumping data for table `10_outside` -- LOCK TABLES 10_outside WRITE; INSERT INTO 10_outside VALUES ('25.71.210.10.in-addr.arpa',3600,'PTR','cas1.test.mydomain.com.cn.'); INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'SOA','test.mydomain.com.cn. zhengyu.staff.mydomain.com.cn. 20070319 1800 600 604800 600'); INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas1.test.mydomain.com.cn.'); INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas2.test.mydomain.com.cn.'); INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas3.test.mydomain.com.cn.'); INSERT INTO 10_outside VALUES ('27.71.210.10.in-addr.arpa',3600,'PTR','cas2.test.mydomain.com.cn.'); UNLOCK TABLES; -- -- Table structure for table `test_mydomain_com_cn_outside` -- DROP TABLE IF EXISTS test_mydomain_com_cn_outside; CREATE TABLE test_mydomain_com_cn_outside ( name varchar(255) default NULL, ttl int(11) default NULL, rdtype varchar(255) default NULL, rdata varchar(255) default NULL ); -- -- Dumping data for table `test_mydomain_com_cn_outside` -- LOCK TABLES test_mydomain_com_cn_outside WRITE; INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'SOA','test.mydomain.com.cn. zhengyu.staff.mydomain.com.cn. 20070319 1800 600 604800 600'); INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas1.test.mydomain.com.cn.'); INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas2.test.mydomain.com.cn.'); INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas3.test.mydomain.com.cn.'); INSERT INTO test_mydomain_com_cn_outside VALUES ('cas1.test.mydomain.com.cn',3600,'A','10.210.71.25'); INSERT INTO test_mydomain_com_cn_outside VALUES ('cas2.test.mydomain.com.cn',3600,'A','10.210.71.27'); INSERT INTO test_mydomain_com_cn_outside VALUES ('cas3.test.mydomain.com.cn',3600,'A','10.210.132.80'); INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.191'); INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.192'); INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.193'); INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.194'); INSERT INTO test_mydomain_com_cn_outside VALUES ('*',3600,'A','10.210.71.1'); INSERT INTO test_mydomain_com_cn_outside VALUES ('conf.test.mydomain.com.cn',3600,'CNAME','cas2.test.mydomain.com.cn.'); UNLOCK TABLES; ############################################
13.繼續配置bind
vim /usr/local/bind/etc/named.conf 在後面按照如下格式加入 zone "mydomain.com" { type master; notify no; database "mysqldb dbname tablename hostname user password"; }; mydomain.com爲要解析的域名 dbname 爲數據庫名 hostname爲數據庫服務器地址 user 爲可操做前面數據庫表的數據庫用戶名 password 爲對應數據庫用戶名的密碼 配置完成 在命令行下運行 /usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g 查看沒問題後 /usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf
[root@silence etc]# ls bind.keys named.conf named.root rndc.conf root.zone [root@silence etc]# cat root.zone $TTL 86400 @ IN SOA ns1.mydomain.com. w1.mydomain.com ( 2018070110 1H 5M 7D 1D) IN NS ns1 IN NS ns2 IN MX 10 mx1 IN MX 20 mx2 ns1 IN A 192.168.108.160 ns2 IN A 192.168.108.138 ns3 IN A 192.168.108.166 mx1 IN A 192.168.108.138 w1 IN A 192.168.1.2 w0 IN A 192.168.1.1 www IN A 192.168.108.160 * IN A 192.168.108.166 [root@silence etc]# cat named.conf key "rndc-key" { algorithm hmac-md5; secret "ZYobWCcSDr2HDCMuojc6gg=="; }; controls { inet 127.0.0.1 port 953 allow { 127.0.0.1; } keys { "rndc-key"; }; }; options { listen-on port 53 { 127.0.0.1;172.16.188.123; }; directory "/data/work/bind9.9.3"; allow-query-cache { any; }; allow-query { any; }; dnssec-enable yes; dnssec-validation yes; dnssec-lookaside auto; }; zone "." { type hint; file "/data/work/bind9.9.3/etc/root.zone"; }; zone "mydomain" { type forward; forwarders { 114.114.114.114;8.8.8.8; }; forward first; }; logging { channel bind_log { file "/data/work/bind9.9.3/logs/bind.log" versions 3 size 20m; severity info; print-time yes; print-severity yes; print-category yes; }; category default { bind_log; }; }; zone "test.mydomain.com.cn" IN{ type master; notify no; database "mysqldb mydomain test_mydomain_com_cn_outside 172.16.188.123 root 111111"; }; zone "16.172.in-addr.arpa" IN{ type master; notify no; database "mysqldb mydomain 10_outside 172.16.188.123 root 111111"; };
運行bindios
寫入系統服務c++
vim /etc/init.d/named #################################################################### #!/bin/bash # # description: named daemon 描述信息 # chkconfig: - 25 88 啓動和關閉級別 # # 啓動named的pid文件、其實文件路徑咱們均可以本身指定的 pidFile=/usr/local/bind/var/run/named.pid # 啓動服務時的鎖文件、判斷一個服務是否是啓動是靠這個鎖文件的 lockFile=/var/lock/subsys/named # named的配置文件路徑 confFile=/usr/local/bind/etc/named.conf # 判斷/etc/rc.d/init.d/functions路徑下的functios是否存在這個文件、存在就sources進來 [ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions # 定義啓動方法 start() { # 判斷鎖文件是否存在、若是存在就說服務已經啓動並退出 if [ -e $lockFile ]; then echo "named is already running..." exit 0 fi # 打印服務已經啓動的信息 echo -n "Starting named:" # 調用functions中的daemon方法、並用--pidfile指定pid文件在什麼地方、還指定用戶和配置文件、咱們原本是直接啓用named的、如今利用daemon這個函數來啓用這個服務、他會得到不少額外的屬性、若是成功了會幫咱們打印什麼[ OK ]的、還能夠作判斷 daemon --pidfile "$pidFile" /usr/local/bind/sbin/named -c "$confFile" RETVAL=$? echo # 判斷上面的命令是否執行成功、說明啓動成功 if [ $RETVAL -eq 0 ]; then # 若是判斷成功、就建立鎖文件 touch $lockFile return $RETVAL else # 不然、就是失敗了、那就刪除鎖文件和pid文件 rm -f $lockFile $pidFile return 1 fi } # 定義中止方法 stop() { # 若是鎖文件不存在 if [ ! -e $lockFile ]; then # 說明服務尚未啓動 echo "named is stopped." fi echo -n "Stopping named:" killproc named RETVAL=$? echo # 判斷以上的命令是否執行中止成功、若是成功 if [ $RETVAL -eq 0 ];then # 刪除鎖文件和pid文件 rm -f $lockFile $pidFile return 0 else # 不然打印說沒能正常中止 echo "Cannot stop named." # 調用functions中的函數、會打印失敗 failure return 1 fi } # 定義重啓服務 restart() { # 調用中止方法 stop # 睡眠2秒 sleep 2 # 調用啓動方法 start } # 定義從新加載服務方法 reload() { echo -n "Reloading named: " # killprco HUP信號、named進程的信號 killproc named -HUP RETVAL=$? echo return $RETVAL } # 定義服務狀態 status() { if pidof named &> /dev/null; then echo -n "named is running..." success echo else echo -n "named is stopped..." success echo fi } # 定義錯誤提示信息 usage() { echo "Usage: named {start|stop|restart|status|reload}" } case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; reload) reload ;; *) usage exit 4 ;; esac
[root@silence ~]# nslookup cas1.test.mydomain.com.cn Server: 172.16.188.123 Address: 172.16.188.123#53 Name: cas1.test.mydomain.com.cn Address: 10.210.71.25 [root@silence ~]# /data/work/bind9.9.3/bin/dig -t A cas1.test.mydomain.com.cn @172.16.188.123 ; <<>> DiG 9.9.3-P1 <<>> -t A cas1.test.mydomain.com.cn @172.16.188.123 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10127 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 3 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;cas1.test.mydomain.com.cn. IN A ;; ANSWER SECTION: cas1.test.mydomain.com.cn. 3600 IN A 10.210.71.25 ;; AUTHORITY SECTION: test.mydomain.com.cn. 3600 IN NS cas1.test.mydomain.com.cn. test.mydomain.com.cn. 3600 IN NS cas2.test.mydomain.com.cn. test.mydomain.com.cn. 3600 IN NS cas3.test.mydomain.com.cn. ;; ADDITIONAL SECTION: cas2.test.mydomain.com.cn. 3600 IN A 10.210.71.27 cas3.test.mydomain.com.cn. 3600 IN A 10.210.132.80 ;; Query time: 3 msec ;; SERVER: 172.16.188.123#53(172.16.188.123) ;; WHEN: Wed Nov 25 16:37:23 CST 2020 ;; MSG SIZE rcvd: 154 [root@silence ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 172.16.188.123