MongoDB 与 Active Directory 集成必须使用 Enterprise 版本,Community Edition 完全不支持 Kerberos;若出现 Unsupported SASL mechanism: GSSAPI 错误,基本可确认是版本问题。
MongoDB 与 Active Directory 集成必须使用 Enterprise 版本,Community Edition 完全不支持 Kerberos;若看到 Unsupported SASL mechanism: GSSAPI 错误,基本可确认是版本问题。
运行 mongod --version,输出中必须包含 modules: enterprise 或 modules: subscription。Community 版即使编译时带 SASL 支持,也无法启用 GSSAPI 认证机制。Kerberos 集成不是“配置开关”,而是二进制级能力,换包比调参数更重要。
mongo:enterprise 镜像,而非 mongo:latest(默认为 Community)yum list installed | grep mongo 检查包名是否含 mongodb-enterprise-server
mongod.exe 属性“详细信息”页中的“产品名称”,应为 “MongoDB Enterprise Server”mongod 启动时若报 GSSAPI Error: Unspecified GSS failure 或连接后认证失败,90% 是 SPN 与实际绑定地址不一致。Kerberos 要求客户端发起的 mongodb/hostname@REALM 必须能被 KDC 正确解析,而解析依据就是 DNS 中的 A 记录或 hosts 映射 —— 且该 hostname 必须是完整域名(FQDN),不能是 localhost、127.0.0.1 或短主机名。
kadmin -p admin/[email protected] addprinc -randkey mongodb/[email protected]
kadmin -p admin/[email protected] ktadd -k /etc/mongodb.keytab mongodb/[email protected]
--bind_ip m1.example.com(或配置文件中 net.bindIp: m1.example.com),不能写成 0.0.0.0 或 192.168.1.10
nslookup m1.example.com 返回正确 IP;同时确认该主机的 /etc/hosts 中无错误映射覆盖$external 用户时 Realm 大小写必须严格一致AD 用户 [email protected] 和 [email protected] 在 Kerberos 协议中是两个完全不同的 principal。MongoDB 创建用户时若 Realm 大小写错误,后续所有认证都会静默失败(日志里只显示 Authentication failed,无具体原因)。
mongosh 连接后执行:db.getSiblingDB("$external").createUser({user: "[email protected]", roles: [{role: "readAnyDatabase", db: "admin"}]})
klist -k /etc/mongodb.keytab 查看 keytab 中存储的 principal 格式/etc/krb5.conf 的 default_realm 与 AD realm 一致,且 realms 段落中 KDC 地址正确Kerberos 只管身份验证,权限控制依赖 LDAP 查询 AD。若用户能登录但无任何数据库访问权限,问题大概率出在 LDAP 绑定或角色映射上,而非 Kerberos 本身。
--ldapBindMethod=simple(或 ssl)和 --ldapTransportSecurity=tls;若 AD LDAPS 端口(636)不可达,会静默回退到明文,但多数企业策略禁止此行为--ldapQueryTemplate 模板中,%s 替换的是 Kerberos principal 全名(如 [email protected]),但 AD 中 userPrincipalName 属性值通常是 [email protected](小写),需在模板中做大小写归一化处理,例如:(userPrincipalName=%[email protected]),其中 %u 是用户名部分(不含 realm)CN=mgdb-readers,CN=Users,DC=example,DC=com,则必须提前在 MongoDB 中创建名为 mgdb-readers 的角色(不是 MGDB-READERS)最常被跳过的一步:Kerberos + AD 集成不是“配完就能用”的线性流程,而是三个独立子系统(KDC、AD/LDAP、MongoDB)的协同。任何一个环节的 DNS、时间同步(Kerberos 要求客户端和服务端时钟偏差 ≤ 5 分钟)、证书信任链或大小写约定出错,都会导致看似随机的认证失败,且错误日志往往不直接暴露根因。