Mysql的流浪端口转发 [英] Vagrant port forwarding for Mysql

查看:253
本文介绍了Mysql的流浪端口转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Vagrantfile中设置端口转发以从主机系统连接到来宾mysqld,但出现reading initial communication packet错误. 主持人:优胜美地,嘉宾:可信赖,无业游民1.7.4

I am trying to setup port forwarding in Vagrantfile to connect to guest mysqld from host system, but get reading initial communication packet error. Host: Yosemite, Guest: Trusty, vagrant 1.7.4

Vagrantfile(主机):

Vagrantfile(host):

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

my.ini(访客):

my.ini(guest):

bind-address            = 127.0.0.1

8080转发的工作原理很简单.

8080 forwarding works like a charm.

mysql -h127.0.0.1 -uroot -p也可以.

mysql -h127.0.0.1 -P 3309 -uroot -p出现reading initial communication packet错误.

当我从主机telnet时,连接立即关闭:

When I telnet from host, the connection instantly closes:

$ telnet localhost 3309
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

当我从主机SSH到无业游民的盒子时,端口转发有效:

Port forwarding works when I ssh to vagrant box from host:

$ssh vagrant@127.0.0.1 -p 2222 -L3308:localhost:3306 

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

Then I can connect from host mysql -h127.0.0.1 -P3308 -uroot -p without problems, which I use as a temporary workaround.

推荐答案

终于能够使它工作了-

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

  • 您有bind-address = 0.0.0.0
  • 或者您在#bind-address ...
  • 行中添加注释
  • you have bind-address = 0.0.0.0
  • or you comment the line #bind-address ...

您可能需要将其添加到my.cnf文件的mysqld部分:

You may need to add it to the mysqld section of the my.cnf file:

[mysqld]
bind-address = 0.0.0.0

确保更改后重新启动mysql服务器

make sure to restart your mysql server after the change

$ sudo service mysql restart

然后您可以从您的主机进行连接-所以我首先遇到了类似的错误

Then you can connect from your host - so I first had an error like

$ 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

所以我回到客人那里做了

so I came back to the guest and did

vagrant@precise64:~$ 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)

那我从主机连接就没问题了

Then I had no issue to connect from the host machine

$ 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)

这篇关于Mysql的流浪端口转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆