Access MySQL from Host being on Vagrant

For this all you would need is tunneling/port forwarding from vagrant to mysql.

Being on vagrant machine, figure out what the host ip is, usually netstat would tell you that.
The default being 10.0.2.2 and assuming you have 3306 port on host machine do the following

ssh username@10.0.2.2 -L 3306:127.0.0.1:3306 -N

The above would forward the 3306 [ the one on the left ] to the hosts port. Mind you, you have to use 127.0.0.1 to access your vagrant machine in place of localhost which might not work.

Port not forwarded from vagrant for rails application on host

Having a rails application on the vagrant machine and the 3000 port not being forwarded when you access it form host machine like

http://localhost:3000

if so keep reading

Assuming you have already setup your vagrant machine

Go ahead and open your Vagrant file, find the config.vm.network and update it with

config.vm.network "forwarded_port", guest: 3000, host: 3000

Then make sure you are binding the server with 0.0.0.0 as it is the main problem is being the default 127.0.0.1 for that, when yoiu are instantiation the server do the binding with 0.0.0.0

bin/server --binding=0.0.0.0

Basically the above would tell to listen to almost any ip. This has its own risk if you are doing it on public VM so caution shall be applied. Where as when it is 127.0.0.1, it will be listening to the local address only.

database

Connecting to Vagrant [PuPHPet] MySql from host

Probably here with – I can’t connect to mysql on vagrant machine Yup I have been there

If you are php developer and are using vagrant you would want to use mysql tool to access the vagrant mysql server.

Here is the step by step procedure.

I will assume the PuPHPet configuration
First, get the user which can connect from any host

Log into your vagrant

vagrant ssh

Then open the mysql configuration file

vi /etc/mysql/my.cnf

And update the bind-address part to be 0.0.0.0. This will allow address to be used from any host.
If you want to specifically access from some host only, then put IP over there.

Then being in your vagrant machine run

mysql -uroot -p123

This will be depending on if you have provided a password for root the default on puPHPet would ask password so you would have a password

After this allow root user to access everything from everywhere.. or you can create another user and grant that

mysql > GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123';
mysql > FLUSH PRIVILEGES;

Then to make the sql pickup the changes made so far

sudo service mysql restart

Then being on your host machine open your sequel pro – if you are on mac.. or other clients you would use for mysql.

Mysql Host: 127.0.0.1
Mysql user: root
Mysql Password: 123

SSH Host: 127.0.0.1
SSH Username: vagrant – this is the default one
SSH key -> point the key in puphpet/files/dot/ssh/id_rsa
SSH Port: 2222 – this is also the default ssh port forwarding, if you change this change it here too..

See how to Setting up vegrant machine from scratch here