目錄服務針對「讀多寫少、層次結構、屬性豐富」的資料存取場景設計,是替代關聯式資料庫處理通訊錄、組織架構等查詢密集型業務的優選方案。本文以 OpenLDAP 為核心,詳細記錄在 Linux 上部署符合 LDAPv3 標準的目錄服務,並實現 Windows Outlook 通訊錄無縫查詢的完整流程。
| 維度 | 關聯式資料庫 (RDBMS) | 目錄服務 (LDAP) |
|---|---|---|
| 資料模型 | 表格、行列、強型別 | 樹狀條目、屬性-多值、Schema 靈活 |
| 優化目標 | 事務完整性、複雜關聯查詢 | 高速讀取、大量並發查詢、複製同步 |
| 權限粒度 | 通常至資料表層級 | 支援條目/屬性級存取控制 (ACI) |
| 協議標準 | SQL (廠商差異大) | LDAP (RFC 4510+,標準化程度高) |
| 典型場景 | 交易系統、ERP、財務 | 通訊錄、單一登入、裝置管理、憑證發布 |
ldapsearch、ldapadd、ldapmodify、ldapdelete/etc/openldap/slapd.conf (服務端)、/etc/openldap/ldap.conf (客戶端預設)/var/lib/ldap# 全域存取控制
access to attrs=userPassword
by self write
by anonymous auth
by * none
access to *
by self write
by users read
by * none
# Schema 引入
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/inetorgperson.schema
# 資料庫定義
database mdb
suffix "dc=example,dc=com"
rootdn "cn=admin,dc=example,dc=com"
rootpw {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
directory /var/lib/ldap
# 索引優化查詢速度
index objectClass eq
index cn,uid,mail,sn,givenName eq,sub,approx
index entryCSN,entryUUID eq
# 複製配置 (可選)
# syncrepl rid=001
# provider=ldap://replica.example.com
# type=refreshAndPersist
# retry="5 5 300 5"
# searchbase="dc=example,dc=com"
# bindmethod=simple
# binddn="cn=replicator,dc=example,dc=com"
# credentials=secret
LDIF (LDAP Data Interchange Format) 為純文字交換格式。針對 Outlook 通訊錄,需包含 inetOrgPerson 物件類別及對應屬性:
dn: uid=jdoe,ou=People,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
uid: jdoe
cn: John Doe
sn: Doe
givenName: John
mail: jdoe@example.com
telephoneNumber: +886-2-12345678
mobile: +886-912-345678
title: Senior Engineer
ou: Engineering
departmentNumber: 100
jpegPhoto:: /9j/4AAQSkZJRgABAQ... (Base64 編碼照片)
使用 ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f users.ldif 匯入。
ou=People,dc=example,dc=comTLSCertificateFile、TLSCertificateKeyFile、TLSCACertificateFileppolicy (密碼策略)、memberof (自動維護群組反向屬性)、auditlog (操作審計)OpenLDAP 提供標準化、高效能、可擴展的目錄服務基礎設施。透過合理的 Schema 設計、索引優化、存取控制與複製架構,可完美支援 Outlook 通訊錄查詢等企業級身份與資源管理場景,是構建統一身份認證平台的核心基石。