MySQL原生不支持直接对接AD认证,必须使用企业版插件:5.7.19+用authentication_ldap_simple,8.0.16+用authentication_ldap_sasl;社区版不可用。

MySQL 本身不支持直接对接 Active Directory(AD)做外部认证,必须依赖企业版提供的 authentication_ldap_simple 或 authentication_ldap_sasl 插件 —— 这是硬性前提,社区版无法启用。
确认 MySQL 版本与许可证类型
LDAP 插件仅存在于 MySQL Enterprise Edition,且最低要求为 5.7.19(authentication_ldap_simple)或 8.0.16(authentication_ldap_sasl)。运行以下命令验证:
SELECT VERSION();<br>SHOW VARIABLES LIKE 'license';
若返回 GPL 或版本低于 5.7.19,后续所有配置都会失败。别跳过这步 —— 很多人卡在这里却反复调参数。
-
authentication_ldap_simple适用于明文传输密码的 AD 环境(需 TLS 加密链路,否则密码裸奔) -
authentication_ldap_sasl支持 GSSAPI(即 Kerberos),更适合域内集成,但 Windows 上服务端插件不可用 - 插件文件名后缀因系统而异:
authentication_ldap_simple.so(Linux)、.dll(Windows)
加载 LDAP 插件并验证路径
插件必须位于 plugin_dir 指向的目录下。先查路径:
SHOW VARIABLES LIKE 'plugin_dir';
将对应插件文件(如 authentication_ldap_simple.so)复制进去后,执行:
INSTALL PLUGIN authentication_ldap_simple SONAME 'authentication_ldap_simple.so';
若报错 Plugin 'authentication_ldap_simple' is disabled,说明文件不存在、权限不对,或 SELinux/Windows Defender 阻断了动态加载。
- Linux 下检查文件权限:
ls -l /path/to/plugin_dir/authentication_ldap_simple.so,确保 mysqld 进程用户可读 - Windows 上注意:
authentication_ldap_sasl服务端插件不支持,只能用客户端插件配合 SASL 客户端工具 - 加载后运行
SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME LIKE 'authentication_ldap%';确认状态为ACTIVE
创建 LDAP 映射用户并指定认证方式
不能直接用 AD 用户名登录 MySQL;必须先在 MySQL 中声明一个“映射账户”,绑定 LDAP 认证逻辑:
CREATE USER 'joe'@'%' IDENTIFIED WITH authentication_ldap_simple AS 'cn=joe,cn=Users,dc=example,dc=com';
其中 AS 后的 DN 必须与 AD 中实际条目完全一致(区分大小写、OU 层级、空格)。常见错误包括:
- 误写成
uid=joe(AD 默认用cn,OpenLDAP 才常用uid) - DN 中漏掉
cn=Users或写成ou=Users(AD 默认容器是cn=Users,不是ou) - 没加引号导致 SQL 解析失败,尤其 DN 含逗号或空格时必须用单引号包裹
- 未提前在 AD 中启用该用户、重置密码、或设置“密码永不过期”(避免 LDAP 绑定时因策略拒绝)
客户端连接时必须启用 mysql_clear_password
使用 authentication_ldap_simple 时,MySQL 客户端必须显式启用 mysql_clear_password 插件,否则会报错 Plugin mysql_clear_password could not be loaded 或 Access denied for user:
mysql -u joe -p --default-auth=mysql_clear_password
或者在连接字符串中指定(如 JDBC):
?defaultAuthenticationPlugin=mysql_clear_password
注意:mysql_clear_password 不传输明文密码到 MySQL 服务端,而是由客户端插件将密码透传给 LDAP 插件 —— 所以服务端日志里看不到密码,但网络链路必须 TLS 加密(require_secure_transport=ON),否则中间人可截获。
- 若用 MySQL Shell,需加
--mysqlc参数启动兼容模式 - PHP PDO 需在 DSN 中加
&auth_plugin=mysql_clear_password - Python mysql-connector-python 8.0+ 默认支持,但老版本需手动指定
auth_plugin='mysql_clear_password'
真正容易被忽略的是 AD 域控制器的防火墙策略:MySQL 服务器必须能从 389(LDAP)或 636(LDAPS)端口连通域控,且 DNS 正向/反向解析必须双向正常 —— 很多环境只开了 389 却忘了开 636,或 hosts 文件写死了旧 IP 导致 TLS 证书校验失败。











