Vagrant port forwarding for Mysql

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

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(host):

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

my.ini(guest):

bind-address            = 127.0.0.1 

8080 forwarding works like a charm.

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

mysql -h127.0.0.1 -P 3309 -uroot -p from host results with reading initial communication packet error.

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. 

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

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

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

回答1:

was finally able to make it work -

edit the /etc/mysql/my.cnf file and make sure, either

  • you have bind-address = 0.0.0.0
  • or you comment the line #bind-address ...

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) 


回答2:

The first answer is right but not enough.when I connect MySQL, I get a error:

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

Solution:

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;

aha, all problems are solved,



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!