解決Docker MySQL無法被宿主機訪問的問題

1 問題描述

Docker啓動MySQL容器後,創建一個localhost訪問的用戶:

create user test@localhost identified by 'test';

但是在宿主機中無法通過該用戶登錄:

mycli -u test

在這裏插入圖片描述

2 原因

Docker中的MySQL創建localhost的用戶只能在Docker內部訪問,而不能通過外部訪問。

至於爲什麼能在宿主機訪問root,是因爲默認存在兩個root,分別是:

  • root@localhost
  • root@%

在這裏插入圖片描述

test只有一個localhost

在這裏插入圖片描述

3 解決方案

創建test@%或者創建[email protected]即可:

create user test@% identified by 'test';
create user [email protected] identified by 'test';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章