How to Implement LDAP Authentication for MySQL: A Complete Guide for Open Source Environments


3 views

Managing authentication across multiple MySQL instances (5.1/5.5) in open-source environments presents significant operational overhead. The native password management system becomes particularly cumbersome when dealing with dozens of servers and numerous user accounts.

While MySQL Enterprise Edition includes native LDAP support, open-source users have several alternative approaches:

  • PAM Authentication Plugin
  • ProxySQL with LDAP backend
  • Custom authentication plugins
  • Middleware solutions

For MySQL 5.5 and later, the PAM authentication plugin provides a path to LDAP integration:


INSTALL PLUGIN authentication_pam SONAME 'authentication_pam.so';

CREATE USER 'ldapuser'@'%' 
IDENTIFIED WITH authentication_pam 
AS 'mariadb,ldapuser=username,ldapgroup=group';

ProxySQL offers robust LDAP integration for MySQL authentication:


INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'mysql-host',3306);

INSERT INTO mysql_ldap_mapping 
(username, attributes) 
VALUES ('%', '{
  "ldap_uri": "ldap://ldap.example.com:389",
  "ldap_bind_dn": "cn=admin,dc=example,dc=com",
  "ldap_bind_password": "secret",
  "ldap_search_base": "ou=users,dc=example,dc=com"
}');

For unique requirements, developing a custom authentication plugin provides maximum flexibility. The basic skeleton:


#include 
#include 

static int ldap_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
{
    // LDAP connection and authentication logic
    LDAP *ld;
    int rc = ldap_initialize(&ld, "ldap://localhost:389");
    // Authentication implementation
    return CR_OK;
}

static struct st_mysql_auth ldap_handler = {
    MYSQL_AUTHENTICATION_INTERFACE_VERSION,
    "mysql_ldap_auth",
    ldap_auth
};

mysql_declare_plugin(ldap_auth)
{
    MYSQL_AUTHENTICATION_PLUGIN,
    &ldap_handler,
    "LDAP_AUTH",
    "Your Name",
    "LDAP Authentication Plugin",
    PLUGIN_LICENSE_GPL,
    NULL,
    NULL,
    0x0100,
    NULL,
    NULL,
    NULL
}
mysql_declare_plugin_end;

When transitioning from native authentication to LDAP:

  • Maintain parallel authentication during transition
  • Implement thorough testing of LDAP connectivity
  • Consider failover mechanisms for LDAP server outages
  • Document mapping between LDAP attributes and MySQL privileges

LDAP authentication adds latency to MySQL connections. Benchmark tests show:

Authentication Method Avg. Connection Time
Native MySQL 15ms
LDAP (local) 45ms
LDAP (remote) 120ms

Essential security measures for LDAP-authenticated MySQL environments:


# Require encrypted LDAP connections
ldap_uri = "ldaps://ldap.example.com:636"

# Implement connection pooling
min_ldap_connections = 5
max_ldap_connections = 20

# Enable LDAP connection timeout
ldap_timeout = 5

Frequent challenges and their solutions:

  1. Connection Refused: Verify LDAP server accessibility
  2. Authentication Failures: Check DN mapping and password policies
  3. Performance Degradation: Implement LDAP connection pooling
  4. Privilege Mismatches: Validate group/role mappings

Managing authentication across multiple MySQL Community Edition servers (versions 5.1 through 5.6) presents significant administrative overhead. Our environment with dozens of instances requires a centralized solution that:

  • Eliminates password synchronization issues
  • Provides single source of truth for credentials
  • Maintains compatibility with existing MySQL privilege systems

MySQL 5.5+ offers experimental LDAP authentication plugins:


INSTALL PLUGIN authentication_ldap_simple 
SONAME 'authentication_ldap_simple.so';

CREATE USER 'developer'@'%' 
IDENTIFIED WITH authentication_ldap_simple 
AS 'uid=dev1,ou=People,dc=example,dc=com';

Key limitations:

  • Only available in MySQL Enterprise Edition
  • Basic bind operations only (no SASL)
  • No group membership mapping
  • A practical open-source approach combines PAM with LDAP:

    
    # Install PAM modules
    sudo apt-get install libpam-ldap libnss-ldap
    
    # Configure MySQL PAM profile
    echo "mysql-auth required pam_ldap.so" >> /etc/pam.d/mysql
    
    # MySQL configuration
    [mysqld]
    plugin-load-add=auth_pam.so
    authentication_pam=mysql
    

    Example user mapping:

    
    CREATE USER 'ldap_user'@'%' 
    IDENTIFIED WITH auth_pam AS 'mysql';
    

    For environments where modifying MySQL isn't feasible, ProxySQL offers:

    
    INSERT INTO mysql_servers(hostgroup_id,hostname,port) 
    VALUES (10,'mysql-host',3306);
    
    INSERT INTO mysql_ldap_mapping 
    (username, active, use_ssl, ldap_uri, ldap_bind_dn, ldap_bind_pw, ldap_search_base, ldap_filter) 
    VALUES 
    ('%.corp', 1, 0, 'ldap://dc1.corp:389', 'cn=proxy,ou=services,dc=corp', 'secret', 'ou=people,dc=corp', '(&(objectClass=person)(uid=%u))');
    

    For maximum flexibility, consider a synchronization script:

    
    #!/bin/bash
    # Sync LDAP users to MySQL
    ldapsearch -x -b "ou=People,dc=example,dc=com" | \
    awk '/uid:/ {print "CREATE USER IF NOT EXISTS '\''"$2"'\''@'\''%'\'';"}' | \
    mysql -u root -p
    

    When transitioning from local to LDAP authentication:

    1. Maintain local fallback accounts
    2. Stage changes in non-production environments
    3. Document existing privileges before migration