解決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@%或者創建test@172.17.0.1即可:

create user test@% identified by 'test';
create user test@172.17.0.1 identified by 'test';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM