mongodb分片集群对接ldap必须通过saslauthd代理,所有mongos、shard节点及config server均需配置--setparameter saslauthdpath=/var/run/saslauthd/mux;saslauthd须显式加载ldap.conf(rhel系用-o参数,ubuntu系确认mechanisms与start),ldap_filter需匹配目录服务属性(如ad用(samaccountname=%u)),且unix socket权限与selinux策略必须正确设置。

MongoDB 分片集群不支持直接对接 LDAP,必须通过 saslauthd 代理实现统一身份认证,且仅限 SCRAM-SHA-1 或 SCRAM-SHA-256 机制;所有 mongos 和 mongod 实例都必须启用 --auth 并显式指定 --setParameter saslauthdPath=/var/run/saslauthd/mux,否则连接会静默降级为本地认证。
分片集群中哪些节点要配 saslauthd 路径
不是只配 config server 或 mongos 就够了——所有参与认证路径的节点都必须配置:
-
mongos实例:必须加--setParameter saslauthdPath=/var/run/saslauthd/mux,它是客户端请求的第一入口,负责把用户名转发给saslauthd -
mongod(shard 节点):同样需要该参数,因为某些操作(如直接连 shard 执行db.runCommand({connectionStatus: 1}))会绕过 mongos,触发本地 SASL 认证流程 -
config server:虽不处理业务查询,但若启用了security.authorization: enabled,它也会尝试调用 SASL,漏配会导致 config server 启动失败或认证异常
遗漏任意一个,都会出现“Authentication failed”却无明确日志指向的问题。建议在 systemd 的 ExecStart 中统一追加该参数,避免配置漂移。
saslauthd 配置必须显式加载 ldap.conf,且路径不能省略
不同发行版默认不读 /etc/saslauthd.conf,必须用 -O 参数强制指定:
- RHEL/CentOS/Fedora:
/etc/sysconfig/saslauthd中设MECH=ldap,并确保FLAGS="-O /etc/saslauthd.conf"—— 缺少-O,saslauthd就只走 pam,完全忽略 ldap 配置 - Ubuntu/Debian:
/etc/default/saslauthd中设MECHANISMS="ldap",同时确认START=yes,再手动把saslauthd.conf放到/etc/下,并在启动命令中补上-O /etc/saslauthd.conf
验证方式:运行 saslauthd -v -n 1,输出里必须含 ldap 字样,且无 Failed to load mechanism 报错;否则说明机制没加载成功,后续所有 MongoDB 认证都会 fallback 到本地用户。
LDAP 过滤规则写错会导致“用户存在但无法登录”,且错误不报具体原因
saslauthd.conf 中的 ldap_filter 是最常出问题的地方,OpenLDAP 和 Active Directory 的属性名完全不同:
- OpenLDAP 常用
(uid=%u)或(cn=%u),但若公司实际用mail属性做登录名,就得写成(mail=%u) - Active Directory 必须用
(sAMAccountName=%u),写成(uid=%u)会查不到,返回authentication failed,但日志里只显示“no such user”,不提示过滤器执行失败 -
ldap_search_base必须是完整 DN,比如ou=employees,dc=corp,dc=example,dc=com,少一个ou=或多一个逗号,就查不到任何条目
调试建议:先用 ldapsearch -x -H ldaps://ldap.corp.example.com -D "cn=admin,dc=corp,dc=example,dc=com" -W -b "ou=employees,dc=corp,dc=example,dc=com" "(sAMAccountName=testuser)" 手动验证过滤逻辑是否通,再同步改到 saslauthd.conf。
Unix socket 权限和 SELinux 是分片集群中最难排查的卡点
分片集群节点多、部署分散,/var/run/saslauthd/mux 的权限很容易不一致:
- 确认
saslauthd进程启动时用了-m /var/run/saslauthd(查看ps aux | grep saslauthd),否则 socket 路径不对 -
mongos和mongod进程用户(如mongod用户)必须属于saslauthd组:usermod -a -G saslauthd mongod - SELinux 启用时,
mongod_t默认无权访问saslauthd_var_run_t,需加策略:setsebool -P saslauthd_read_sockets on或临时放行:audit2allow -a -M mongodb_sasl && semodule -i mongodb_sasl.pp
现象典型:所有配置都对,saslauthd -v -n 1 测试通过,但 MongoDB 连接时只报 Cannot connect to saslauthd server: Permission denied,不提 socket 路径或 SELinux —— 这时候基本就是权限或策略没到位。











