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.
The first answer is right but not enough.when I connect MySQL, I get a error:
Solution:
aha, all problems are solved,
was finally able to make it work -
edit the
/etc/mysql/my.cnf
file and make sure, eitherbind-address = 0.0.0.0
#bind-address ...
You may need to add it to the mysqld section of the my.cnf file:
make sure to restart your mysql server after the change
Then you can connect from your host - so I first had an error like
so I came back to the guest and did
Then I had no issue to connect from the host machine
Personally I don't bother with modifying MySQL for development Vagrant boxes - it's time consuming and difficult to script in a provisioner meaning you have to do it by hand every time you
vagrany destroy
or a new developer starts contributing. Instead, I connect via SSH Tunnel which is made super easy using Vagrant's generatedprivate_key
file. No additional post-install tweaking necessary.Follow these steps to SSH Tunnel with SequelPro, MySql Workbench, or any other client that supports SSH connectivity:
127.0.0.1
(more predictable cross-os)root
and the password is defined/created in the provisioner (see snippet below)Vagrant
private_key
for the Vagrant machine instead, located in.vagrant/machines/default/virtualbox
in the root of your VM project; note that on most OS's this directory, and all starting with a.
are hiddenTo automate installation and root password creation, add this to your Vagrant provisioner script file (
config.vm.provision
in Vagrantfile), commonly namedprovisioner.sh
:Hope this helps save someone else some time!