OpenLdap+MySQL筆記


20元現金領取地址:http://jdb.jiudingcapital.com/phone.html
內部邀請碼:C8E245J (不寫邀請碼,沒有現金送)

國內私募機構九鼎控股打造,九鼎投資是在全國股份轉讓系統掛牌的公眾公司,股票代碼為430719,為“中國PE第一股”,市值超1000億元。

 

最近項目原因,在CentOS下做了一套簡單的LDAP服務,在此記錄一二,本文強調后期碰到的問題和解決方法

下面的鏈接是比較詳細的安裝和基本配置過程,這個鏈接適用於debian,但是類似的可以在其他發行版下借鑒:

http://www.wingfoss.com/content/how-to-install-openldap-with-mysql-on-debian6

如果從源代碼安裝的話,slapd不會被注冊成服務,需要手動來做。下面是一個Ldap自啟動的腳本,我在CentOS下試過了,可以用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
#
# ldap This shell script takes care of starting and stopping
# ldap servers (slapd and slurpd).
#
# chkconfig: - 70 40
# description: LDAP stands for Lightweight Directory Access Protocol, used
# for implementing the industry standard directory services.
# processname: slapd
# config: /etc/openldap/slapd.conf
# pidfile: /var/run/slapd.pid
# Source function library.
. /etc/rc .d /init .d /functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no"  ] && exit  0
[ -f /usr/local/libexec/slapd  ] || exit  0
# [ -f /usr/sbin/slurpd ] || exit 0
 
export  CPPFLAGS= "-I/opt/BerkeleyDB.5.3/include"
export  LDFLAGS= "-L/opt/BerkeleyDB.5.3/lib"
export  LD_LIBRARY_PATH= "/opt/BerkeleyDB.5.3/lib"
 
RETVAL=0
# See how we were called.
case  "$1"  in
         start)
         # Start daemons.
                 echo  -n "Starting ldap: "
                 daemon /usr/local/libexec/slapd
                 RETVAL=$?
                 if  [ $RETVAL - eq  0 ]; then
                         if  grep  -q "^replogfile"  /etc/openldap/slapd .conf; then
                                 daemon slurpd
                                 RETVAL=$?
                                 [ $RETVAL - eq  0 ] && pidof slurpd | cut  -f 1 -d " "  > /var/run/slurpd
                         fi
                 fi
echo
[ $RETVAL - eq  0 ] && touch  /var/lock/subsys/ldap
;;
         stop)
         # Stop daemons.
                 echo  -n "Shutting down ldap: "
                 killproc slapd
                 RETVAL=$?
                 if  [ $RETVAL - eq  0 ]; then
                         if  grep  -q "^replogfile"  /etc/openldap/slapd .conf; then
                         killproc slurpd
                         RETVAL=$?
                         fi
                 fi
echo
if  [ $RETVAL - eq  0 ]; then
                 rm  -f /var/lock/subsys/ldap
                 rm  -f /var/run/slapd .args
                 fi
;;
         status)
                 status slapd
                 RETVAL=$?
                 if  [ $RETVAL - eq  0 ]; then
                         if  grep  -q "^replogfile"  /etc/openldap/slapd .conf; then
                                 status slurpd
                                 RETVAL=$?
                         fi
                 fi
;;
         restart)
                 $0 stop
                 $0 start
                 RETVAL=$?
;;
         reload)
                 killproc -HUP slapd
                 RETVAL=$?
                 if  [ $RETVAL - eq  0 ]; then
                         if  grep  -q "^replogfile"  /etc/openldap/slapd .conf; then
                                 killproc -HUP slurpd
                                 RETVAL=$?
                         fi
                 fi
;;
*)
echo  "Usage: $0 start|stop|restart|status}"
exit  1
esac
exit  $RETVAL

對腳本的一些說明

  • # chkconfig: - 70 40 :此行不僅僅是注釋,服務注冊的命令(chkconfig)會根據這行還決定服務的啟動順序
  • export是導出BerkeleyDB的庫文件和頭文件

將腳本保存在/etc/init.d下,為其設置可執行權限

1
chmod  700 /etc/init .d /ldap

用chkconfig命令注冊腳本在啟動時運行

1
2
chkconfig --add ldap
chkconfig --level 345 ldap on

因為是基於mysql的后端數據庫,所以要保證ldap在mysql啟動后啟動,查看相應運行級別的rc.d文件(比如在level3時,進入到/etc/rc.d/rc3.d),查看其中的鏈接的開頭數字是否大於mysql服務的數字。關於CentOS下,服務的基本概念這里不再說了

 

后續問題:長時間inactive,LDAP故障,需要重啟

LDAP上線后,發現每天晚上過來LDAP服務都不可用了。這個問題一度困擾了我很長時間,網上各種查都無果,為此還在論壇上提問了。詳見http://www.linuxcast.net/ask/show/480

最終偶然的機會,同事抱怨說Oracle數據庫鏈接超過上線,要釋放。我突然來了靈感,猜測是Mysql數據庫鏈接自動關閉的問題,回頭一查,果然mysql的如下參數設置成了8小時:

interactive_timeout    | 28800

wait_timeout       | 28800

這樣一來,一晚上(超過8小時)過來mysql主動將LDAP打開並保存的鏈接單方面關閉了,而LDAP全然不知,也沒有重試鏈接,導致無法查詢數據庫。將這個值改大后可以保證一晚上不釋放即可。

 

后續問題:性能低下

由於保存用戶信息,用於登陸驗證。用戶感知登陸很慢,通常要10秒以上,才能登錄。開始分析:先打開LDAP的log,在LDAP的配置文件里面配置

loglevel   256

然后在linux的日志服務的配置文件中添加

local4.*   -/var/log/ldap.log

log將輸出到上面的路徑下,關於linux系統日志服務的基本知識這里略過

觀察log發現search每次都要大約6秒鍾的時間,是主要的性能瓶頸。最初想為LDAP加索引和緩存,索引似乎只能支持bdb,緩存似乎要另外裝插件;再看mysql端是否可以優化的,用腳本創建出來的表都帶有索引,於是考慮mysql查詢緩存是否能幫上忙:http://blog.webwlan.net/wordpress/?p=422

最終,嘗試配置下面的配置:query_cache_size。這個值默認是0,因此,即使query_cache_type默認是啟用的,也不能緩存查詢結果。將query_cache_size配置成大約100M,測試LDAP,明顯登錄加快。再看log,查詢時間縮短到1秒,暫時解決了燃眉之急。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM