94

I mounted a new VirtualBox Machine with Vagrant, and inside that VM I installed Mysql Server. How can I connect to that server outside the vm? I already forward the port 3306 of the Vagrantfile , but when I try to connect to the mysql server, it`s resposts with the error: 'reading initial communication packet'

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
1
  • Can you share more of your configuration, specifically the networking options (how many, type [bridged, NAT, ...]) as well as a screenshot of the port forwards you did? It may also be a firewall related issue on your VM.
    – Goyuix
    May 25, 2012 at 17:00

7 Answers 7

130

Make sure MySQL binds to 0.0.0.0 and not 127.0.0.1 or it will not be accessible from outside the machine

You can ensure this by editing the /etc/mysql/my.conf file and looking for the bind-address item--you want it to look like bind-address = 0.0.0.0. Then save this and restart MySQL:

sudo service mysql restart

If you are doing this on a production server, you want to be aware of the security implications, discussed here: https://serverfault.com/questions/257513/how-bad-is-setting-mysqls-bind-address-to-0-0-0-0

6
  • This was definitely the case for my mysql 5.5 install. From the comments in mysql.conf, I think it varys by version--they use to have the commented out configuration mentioned in other answers.
    – mooreds
    Aug 30, 2013 at 17:33
  • 7
    You can automate this step in your bootstrap file with the following command (tested on CentOS): sed -i 's/symbolic-links=0/symbolic-links=0\nbind-address=0.0.0.0/g' /etc/my.cnf
    – ronan_mac
    Jun 10, 2014 at 13:54
  • or just comment out the bind-address line
    – xiaoyifang
    Nov 5, 2014 at 7:22
  • 9
    I get this Host '10.0.2.2' is not allowed to connect to this MySQL server. But solved with serverfault.com/a/486716/147813 Oct 19, 2015 at 6:51
  • 2
    In addition to the @CMCDragonkai serverfault link (I don't have reputation there to comment), this command can be used in something like an Ansible script to automate granting root user permissions: mysql -e "create user 'root'@'10.0.2.2' identified by 'vagrant'; grant all privileges on *.* to 'root'@'10.0.2.2' with grant option; flush privileges;" Aug 22, 2016 at 19:08
50

Log in to your box with ssh vagrant@127.0.0.1 -p 2222 (password vagrant)

Then: sudo nano /etc/mysql/my.cnf and comment out the following lines with #

#skip-external-locking 
#bind-address

save it & exit

then: sudo service mysql restart

Then you can connect through SSH to your MySQL server.

3
  • 10
    I commented out with "#" instead of ";"
    – Jason
    Apr 5, 2013 at 16:54
  • 4
    I have to bind the address and run this: GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION;
    – An Nguyen
    Jul 15, 2014 at 3:04
  • Thank you, this was troubling me for ages. # skip-external-locking solved it!
    – Jack
    Dec 6, 2016 at 16:43
18

I came across this issue recently. I used PuPHPet to generate a config.

To connect to MySQL through SSH, the "vagrant" password was not working for me, instead I had to authenticate through the SSH key file.

To connect with MySQL Workbench

Connection method

Standard TCP/IP over SSH

SSH

Hostname: 127.0.0.1:2222 (forwarded SSH port)
Username: vagrant
Password: (do not use)
SSH Key File: C:\vagrantpath\puphpet\files\dot\ssh\insecure_private_key
              (Locate your insercure_private_key)

MySQL

Server Port: 3306
username: (root, or username)
password: (password)

Test the connection.

1
  • I did basically the same thing to connect to the database with Phpstorm, but instead of PuPHPet I used Protobox.
    – null
    Nov 7, 2014 at 15:39
17

For anyone trying to do this using mysql workbench or sequel pro these are the inputs:

Mysql Host: 192.168.56.101 (or ip that you choose for it)
username: root (or mysql username u created)
password: **** (your mysql password)
database: optional
port: optional (unless you chose another port, defaults to 3306)

ssh host: 192.168.56.101 (or ip that you choose for this vm, like above)
ssh user: vagrant (vagrants default username)
ssh password: vagrant (vagrants default password)
ssh port: optional (unless you chose another)

source: https://coderwall.com/p/yzwqvg

3
  • 1
    This was very helpful! I didn't have to adjust bind-address at all using virtual box and ssh agent forwarding
    – kasakka
    Dec 27, 2013 at 11:25
  • 1
    Make sure you remove any public keys from your host machine's known_hosts file that are for the same host (IP Address). This will have happened if you have been using another Vagrant VM with the same IP address
    – Jon Hudson
    Apr 8, 2015 at 22:57
  • You can also get the SSH config values from the vagrant ssh-config command.
    – salsbury
    Nov 28, 2017 at 16:06
13

Well, since neither of the given replies helped me, I had to look more, and found solution in this article.

And the answer in a nutshell is the following:

Connecting to MySQL using MySQL Workbench

Connection Method: Standard TCP/IP over SSH
SSH Hostname: <Local VM IP Address (set in PuPHPet)>
SSH Username: vagrant (the default username)
SSH Password: vagrant (the default password)
MySQL Hostname: 127.0.0.1
MySQL Server Port: 3306
Username: root
Password: <MySQL Root Password (set in PuPHPet)>

Using given approach I was able to connect to mysql database in vagrant from host Ubuntu machine using MySQL Workbench and also using Valentina Studio.

3
  • I'm using Valentina, which is basically MySQL Workbench in terms of how one would connect to a Vagrant guest server. Of all the answers, this one, right here, made it work. All that other stuff was handled for me by PuPHPet, so all that Vagrant configuration stuff is probably more for those people who like to manually configure their Vagrantfiles. For PuPHPet people this is the answer. Aug 9, 2015 at 16:13
  • I had to specify the SSH Key File path to the private key file for the vagrant box: $HOME/project/.vagrant/machines/default/virtualbox/private_key in Ubuntu 14.04 Apr 5, 2017 at 3:00
  • You can also get the SSH config values from the vagrant ssh-config command.
    – salsbury
    Nov 28, 2017 at 16:06
4

Here are the steps that worked for me after logging into the box:

Locate MySQL configuration file:

$ mysql --help | grep -A 1 "Default options"

Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

On Ubuntu 16, the path is typically /etc/mysql/mysql.conf.d/mysqld.cnf

Change configuration file for bind-address:

If it exists, change the value as follows. If it doesn't exist, add it anywhere in the [mysqld] section.

bind-address = 0.0.0.0

Save your changes to the configuration file and restart the MySQL service.

service mysql restart

Create / Grant access to database user:

Connect to the MySQL database as the root user and run the following SQL commands:

mysql> CREATE USER 'username'@'%' IDENTIFIED BY 'password';

mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'username'@'%';
1

This worked for me: Connect to MySQL in Vagrant

username: vagrant password: vagrant

sudo apt-get update sudo apt-get install build-essential zlib1g-dev
git-core sqlite3 libsqlite3-dev sudo aptitude install mysql-server
mysql-client


sudo nano /etc/mysql/my.cnf change: bind-address            = 0.0.0.0


mysql -u root -p

use mysql GRANT ALL ON *.* to root@'33.33.33.1' IDENTIFIED BY
'jarvis'; FLUSH PRIVILEGES; exit


sudo /etc/init.d/mysql restart




# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|

  config.vm.box = "lucid32"

  config.vm.box_url = "http://files.vagrantup.com/lucid32.box"

  #config.vm.boot_mode = :gui

  # Assign this VM to a host-only network IP, allowing you to access
it   # via the IP. Host-only networks can talk to the host machine as
well as   # any other machines on the same network, but cannot be
accessed (through this   # network interface) by any external
networks.   # config.vm.network :hostonly, "192.168.33.10"

  # Assign this VM to a bridged network, allowing you to connect
directly to a   # network using the host's network device. This makes
the VM appear as another   # physical device on your network.   #
config.vm.network :bridged

  # Forward a port from the guest to the host, which allows for
outside   # computers to access the VM, whereas host only networking
does not.   # config.vm.forward_port 80, 8080

  config.vm.forward_port 3306, 3306

  config.vm.network :hostonly, "33.33.33.10"


end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.