首先要获取宿主机(Windows)的IP:
export HOST_IP=$(grep -oP '(?<=nameserver\ ).*' /etc/resolv.conf)
然后通过-h指定MySQL服务器所在的IP访问:
mysql -h $HOST_IP -uroot -proot
可能会出现以下错误:
Host is not allowed to connect to this MySQL server.
这是因为MySQL默认禁用远程访问,解决方法:
# 用Windows登录MySQL,然后执行以下语句
use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES; # 必须执行这一步,否则无效
参考
Host is not allowed to connect to this MySQL server解决方法_写代码的林克-CSDN博客