在應用層開發中,身份驗證通常僅涉及 username 與 password 兩個字段。但作為企業級數據庫,MySQL 的認證體系遠比這複雜:它融合了基於主機的訪問控制(Host-based Access Control)、權限矩陣、資源限制 等多維度檢查。本文將從系統表結構、認證流程圖、核心源碼(sql_acl.cc)三個維度,深度剖析 MySQL 登錄認證的完整鏈路。
mysql.user 結構解讀MySQL 將用戶信息、全局權限、資源限制、SSL 憑證等全部存儲在 mysql.user 表中(MySQL 5.7 為例,共 39 字段)。認證階段主要涉及前三列:
| 字段 | 類型 | 說明 |
|---|---|---|
Host | char(60) | 允許連接的主機名/IP,支持通配符 % |
User | char(32) | 用戶名 |
Password | char(41) | 加密後的密碼值(SHA1 雙重哈希) |
關鍵設計:三元組 (Host, User, Password) 共同決定一個合法身份。 同一用戶名可對應不同 Host 記錄,實現「同用戶、不同來源、不同權限」的精細控制。
mysql> SELECT Host, User, Password FROM user;
+-----------+------+----------+
| Host | User | Password |
+-----------+------+----------+
| localhost | root | |
| 127.0.0.1 | root | |
| localhost | | |
+-----------+------+----------+
root 用戶僅允許本地連接(localhost / 127.0.0.1),密碼為空。SHOW DATABASES 等元操作)。
流程核心節點:
acl_check_host() 驗證 mysql.user.Host 是否匹配check_user() 驗證用戶名/密碼login_connection()static bool login_connection(THD *thd) {
NET *net = &thd->net;
int error;
// 設置連接階段超時
my_net_set_read_timeout(net, connect_timeout);
my_net_set_write_timeout(net, connect_timeout);
// 核心驗證邏輯
error = check_connection(thd);
net_end_statement(thd);
if (error) {
statistic_increment(aborted_connects, &LOCK_status);
return true; // 認證失敗
}
// 認證成功,恢復默認超時
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
my_net_set_write_timeout(net, thd->variables.net_write_timeout);
return false;
}
check_connection() 簡化邏輯static int check_connection(THD *thd) {
// 1. 獲取客戶端 IP/主機名
char ip[30];
vio_peer_addr(net->vio, ip, &thd->peer_port);
thd->main_security_ctx.ip = my_strdup(ip);
thd->main_security_ctx.host_or_ip = thd->main_security_ctx.ip;
// 可選:反向 DNS 解析獲取主機名
if (!(specialflag & SPECIAL_NO_RESOLVE)) {
thd->main_security_ctx.host = ip_to_hostname(&thd->remote.sin_addr, &connect_errors);
// 截斷過長主機名,防止溢出
if (thd->main_security_ctx.host) {
thd->main_security_ctx.host[min(strlen(...), HOSTNAME_LENGTH)] = 0;
thd->main_security_ctx.host_or_ip = thd->main_security_ctx.host;
}
// 連接錯誤過多導致主機被封鎖
if (connect_errors > max_connect_errors) {
my_error(ER_HOST_IS_BLOCKED, MYF(0), thd->main_security_ctx.host_or_ip);
return 1;
}
}
// 2. Host 白名單檢查
if (acl_check_host(thd->main_security_ctx.host, thd->main_security_ctx.ip)) {
my_error(ER_HOST_NOT_PRIVILEGED, MYF(0), thd->main_security_ctx.host_or_ip);
return 1;
}
// 3. 解析客戶端發送的認證包(用戶名、密碼、默認數據庫)
char *user = end;
char *passwd = strend(user) + 1;
uint user_len = passwd - user - 1;
// ... 省略協議解析細節 ...
// 4. 用戶名/密碼驗證
return check_user(thd, COM_CONNECT, passwd, passwd_len, db, TRUE);
}
| 函數 | 職責 | 失敗返回 |
|---|---|---|
acl_check_host() | 遍歷 mysql.user 表,檢查是否存在匹配的 Host 記錄(支持通配符) | ER_HOST_NOT_PRIVILEGED (1130) |
check_user() | 在通過 Host 檢查的記錄中,進一步匹配 User 與 Password(採用挑戰-響應機制,防重放) | ER_ACCESS_DENIED_ERROR (1045) |
acl_getroot() / get_user_options() | 構建用戶權限上下文,檢查 max_connections、max_user_connections 等資源限制 | 對應資源超限錯誤碼 |
MySQL 不在網絡上傳輸明文密碼。流程:
scramble)發送給 ClientSHA1(password) XOR SHA1(scramble + SHA1(SHA1(password)))mysql.user.Password 讀取 SHA1(SHA1(password)),用相同算法驗證通過 plugin 表註冊認證插件(如 mysql_native_password、caching_sha2_password、auth_socket、LDAP、Kerberos 等),實現可插拔認證方式。
FLUSH HOSTS為避免頻繁 DNS 解析,MySQL 維護內存主機緩存。當 max_connect_errors 觸發封鎖時,需執行 FLUSH HOSTS 或重啟服務解封。
DROP USER ''@'localhost';root@'127.0.0.1' 和 root@'localhost'validate_password 插件max_connect_errors:防止暴力破解導致誤封合法主機MySQL 認證並非簡單的「查表比對」,而是一個分層、可擴展、狀態感知的安全子系統:
理解這一機制,不僅有助於排查「Access denied」類故障,更是設計數據庫安全架構、實現自定義認證插件的基石。