1 問題描述
Docker啟動MySQL容器后,創建一個localhost訪問的用戶:
create user test@localhost identified by 'test';
但是在宿主機中無法通過該用戶登錄:
mycli -u test

2 原因
在Docker中的MySQL創建localhost的用戶只能在Docker內部訪問,而不能通過外部訪問。
至於為什么能在宿主機訪問root,是因為默認存在兩個root,分別是:
root@localhostroot@%

而test只有一個localhost:

3 解決方案
創建test@%或者創建test@172.17.0.1即可:
create user test@% identified by 'test';
create user test@172.17.0.1 identified by 'test';
