在開啟了端口轉發的情況下,Vagrant 中的 mysql 在默認安裝情況下從外界也是無法訪問。
解決思路
需要兩步。此處假定 root 密碼為 123456
- 注釋掉
/etc/mysql/my.cnf
中的bind-address = 127.0.0.1
。因為綁定到 127.0.0.1 時,mysql 只接受來自 localhost 的連接。 - 開放訪問權限。
mysql -uroot -p123456 -e "create user 'root'@'10.0.2.2' identified by '123456'; grant all privileges on *.* to 'root'@'10.0.2.2' with grant option; flush privileges;"
實例
當 mysql 安裝好,並且 vagrant 的3306端口映射到宿主機的3306端口時,用 Navicat 連接報錯:Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
sudo vim /etc/mysql/my.cnf
注釋掉 bind-address = 127.0.0.1
。
重啟服務(ubuntu)sudo service mysql restart
重新連接,報錯:Host '10.0.2.2' is not allowed to connect to this MySQL server
修改權限:
mysql -uroot -p123456 -e "create user 'root'@'10.0.2.2' identified by '123456'; grant all privileges on *.* to 'root'@'10.0.2.2' with grant option; flush privileges;"
- 1
重啟服務(ubuntu)sudo service mysql restart
再次連接,成功。