Vagrant 中的 mysql 外界访问设置方法

问题描述

在开启了端口转发的情况下,Vagrant 中的 mysql 在默认安装情况下从外界也是无法访问。

解决思路

需要两步。此处假定 root 密码为 123456

  1. 注释掉/etc/mysql/my.cnf中的bind-address = 127.0.0.1。因为绑定到 127.0.0.1 时,mysql 只接受来自 localhost 的连接。
  2. 开放访问权限。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;"

重启服务(ubuntu)sudo service mysql restart

再次连接,成功。

参考

猜你喜欢

转载自blog.csdn.net/u013614126/article/details/72810222