2015-10-15 72 views
13

我正尝试在Vagrantfile中设置端口转发以从主机系统连接到来宾mysqld,但得到reading initial communication packet错误。 主持人:优山美地,客人:忠实的,流浪汉1.7.4用于Mysql的流浪端口转发

Vagrantfile(主持人):

config.vm.network "forwarded_port", guest: 80, host: 8080 
config.vm.network "forwarded_port", guest: 3306, host: 3309 

的my.ini(游客):

bind-address   = 127.0.0.1 

8080转发就像一个魅力。

mysql -h127.0.0.1 -uroot -p from guest also works。

mysql -h127.0.0.1 -P 3309 -uroot -p从主机结果中获得reading initial communication packet错误。

当我从主机远程登录,连接会马上闭合:

$ telnet localhost 3309 
Trying 127.0.0.1... 
Connected to localhost. 
Escape character is '^]'. 
Connection closed by foreign host. 
当我ssh到从主机游民箱

端口转发的工作原理:

$ssh [email protected]127.0.0.1 -p 2222 -L3308:localhost:3306 

然后,我可以从主机mysql -h127.0.0.1 -P3308 -uroot -p连接没有问题,我将其用作临时解决方法。

+0

你有可能会屏蔽这些端口的流量防火墙规则? –

+0

@FrédéricHenri不,这是我的第一个嫌疑人。没有单一的iptable规则。无论如何,我试图通过SSH隧道,它没有问题。 –

+0

正确,但与隧道你仍然使用ssh端口。 –

回答

18

终于能够使它发挥作用 -

编辑/etc/mysql/my.cnf文件,并确保,无论是

  • 你有bind-address = 0.0.0.0
  • 或者你对此有何评论线#bind-address ...

化妆确定在更改后重启你的mysql服务器

$ sudo service mysql restart 

然后你可以从你的主机连接 - 所以我第一次有这样的错误

$ mysql -h127.0.0.1 -P 3309 -uroot -p 
Enter password: 
ERROR 1130 (HY000): Host '172.16.42.2' is not allowed to connect to this MySQL server 

等我回来的客人,做

[email protected]:~$ mysql -h127.0.0.1 -uroot -p 
... 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.42.2' WITH GRANT OPTION; 
Query OK, 0 rows affected (0.00 sec) 

mysql> FLUSH PRIVILEGES; 
Query OK, 0 rows affected (0.00 sec) 

然后我没有问题,连接从主机

$ mysql -h127.0.0.1 -P 3309 -uroot -p 
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 36 
Server version: 5.5.44-0ubuntu0.12.04.1 (Ubuntu) 
+0

感谢Frederic。它也适用于my.cnf中的'bind-address = 10.0.2.15'。 –

+0

显然,virtualbox使用此接口进行端口转发。我找不到一种方法将端口转发到除ssh之外的其他接口,这不是很方便,但有点合理。 –

+1

对于VirtualBox,Vagrant需要连接到虚拟机的第一台网络设备才能成为NAT设备。 NAT设备用于端口转发,这是Vagrant如何获得SSH访问虚拟机的方式。 –

3

第一个答案是正确的,但不够。当我连接我的SQL,我得到一个错误:

Host '10.0.2.2' is not allowed to connect to this MySQL server

解决方案:

create user 'root'@'10.0.2.2' identified by 'password';

grant all privileges on . to 'root'@'10.0.2.2' with grant option;

flush privileges;

啊哈,所有的问题都解决了,