如何在VirtualBox Vagrant中连接到Mysql服务器?

95

我使用Vagrant挂载了一台新的VirtualBox虚拟机,在该虚拟机中安装了Mysql Server。如何在虚拟机外连接到该服务器?我已经在Vagrantfile中转发了3306端口,但是当我尝试连接mysql服务器时,它会返回错误:“reading initial communication packet”。

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

你能分享更多的配置信息吗?具体来说,网络选项(数量、类型[桥接、NAT等])以及端口转发的截图。这可能也与你的虚拟机防火墙有关。 - Goyuix
7个回答

131

确保MySQL绑定到0.0.0.0而不是127.0.0.1,否则将无法从机器外部访问它。

您可以通过编辑/etc/mysql/my.conf文件并查找bind-address项来确保这一点--您希望它看起来像bind-address = 0.0.0.0。然后保存并重新启动MySQL:

sudo service mysql restart

如果您正在生产服务器上进行此操作,您需要注意安全性问题,在此处讨论:https://serverfault.com/questions/257513/how-bad-is-setting-mysqls-bind-address-to-0-0-0-0


这绝对是我安装mysql 5.5时的情况。从mysql.conf的注释中可以看出,我认为它因版本而异——他们曾经使用其他答案中提到的被注释掉的配置。 - mooreds
7
你可以在引导文件中使用以下命令自动化此步骤(已在CentOS上测试):sed -i 's/symbolic-links=0/symbolic-links=0\nbind-address=0.0.0.0/g' /etc/my.cnf - ronan_mac
仅注释掉 bind-address 行。 - xiaoyifang
9
我收到了这个错误信息:“Host '10.0.2.2' is not allowed to connect to this MySQL server”。但是,通过查看http://serverfault.com/a/486716/147813中的解决方法,问题得以解决。 - CMCDragonkai
2
除了@CMCDragonkai的serverfault链接(我没有声望在那里评论),这个命令可以用在像Ansible脚本之类的东西中,以自动授予root用户权限: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;” - KayakinKoder
我必须添加:CREATE USER 'root'@'%' IDENTIFIED BY 'qwerty'; GRANT ALL PRIVILEGES ON * . * TO 'root'@'%'; FLUSH PRIVILEGES; - Sergio Negri

50
使用ssh vagrant@127.0.0.1 -p 2222(密码为vagrant)登录到您的盒子。
然后,使用sudo nano /etc/mysql/my.cnf命令,并使用#注释掉以下行。
#skip-external-locking 
#bind-address

保存并退出
然后执行:sudo service mysql restart
然后您可以通过SSH连接到您的MySQL服务器。

10
我使用 "#" 注释代替了 ";"。 - Jason
4
我需要绑定地址并运行以下命令: GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION; 该命令意为授予'myuser'用户在任何主机上对所有数据库和表的全部权限,并且该用户可以将这些权限授予其他用户。 - An Nguyen
谢谢,这个问题困扰了我很长时间。#skip-external-locking解决了它! - Jack

18

最近我遇到了这个问题。我使用PuPHPet生成了一个配置。

要通过SSH连接到MySQL,"vagrant"密码对我不起作用,我必须通过SSH密钥文件进行身份验证。

使用MySQL Workbench连接

连接方法

标准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)

测试连接。


我基本上用Phpstorm做了与PuPHPet相同的事情来连接数据库,但是我使用的是Protobox。 - null

17

如果有人想用MySQL Workbench或Sequel Pro完成此操作,以下是输入内容:

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)

来源:https://coderwall.com/p/yzwqvg


1
这非常有帮助!使用虚拟机和ssh代理转发,我根本不需要调整绑定地址。 - kasakka
1
请确保从主机的known_hosts文件中删除任何与同一主机(IP地址)相关的公钥。如果您使用另一个具有相同IP地址的Vagrant VM,则会发生这种情况。 - Jon Hudson
您还可以通过vagrant ssh-config命令获取SSH配置值。 - salsbury

13

好的,由于给定的回复都没有帮到我,我不得不再仔细查找,并在这篇文章中找到了解决方案。

简而言之,答案如下:

使用MySQL Workbench连接MySQL

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

使用给定的方法,我能够通过MySQL Workbench和Valentina Studio从Ubuntu主机上的Vagrant连接到MySQL数据库。


我正在使用Valentina,它基本上就像MySQL Workbench一样连接到Vagrant客户服务器。在所有答案中,这个答案让它起作用了。PuPHPet已经处理了所有其他的东西,所以所有的Vagrant配置都可能更适合那些喜欢手动配置他们的Vagrantfiles的人。对于PuPHPet用户来说,这就是答案。 - unrivaledcreations
我必须在Ubuntu 14.04中为vagrant box指定SSH密钥文件路径:$HOME/project/.vagrant/machines/default/virtualbox/private_key - Francisco Quintero
您还可以通过vagrant ssh-config命令获取SSH配置值。 - salsbury

4
以下是我在登录系统后所使用的步骤: 1. 找到 MySQL 配置文件:
$ 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

在Ubuntu 16上,路径通常为/etc/mysql/mysql.conf.d/mysqld.cnf
更改绑定地址的配置文件:
如果存在,则将值更改如下。如果不存在,请在[mysqld]部分的任何位置添加它。
bind-address = 0.0.0.0

保存更改到配置文件并重新启动MySQL服务。

service mysql restart

创建/授权数据库用户:

以root用户身份连接到MySQL数据库并运行以下SQL命令:

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

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

1
这对我有用:在Vagrant中连接MySQL
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接