如何从 vagrant virtualbox 机器连接主机 PostgreSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19933550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect with host PostgreSQL from vagrant virtualbox machine
提问by Jorge Arévalo
I have a VirtualBox machine running Ubuntu 12.04 in a Mac OS X host machine. In my host machine (Mac OS X), I have PostgreSQL installed. I would like to connect to this PostgreSQL server frommy Ubuntu virtual machine (I know normally it's the opposite way).
我有一台在 Mac OS X 主机上运行 Ubuntu 12.04 的 VirtualBox 机器。在我的主机 (Mac OS X) 中,我安装了 PostgreSQL。我想从我的 Ubuntu 虚拟机连接到这个 PostgreSQL 服务器(我知道通常是相反的方式)。
I guess I should configure some networking parameters in VirtualBox itself. All I get from Vagrant docsis I need to assign my vm a static IP to use a private network. But once created, how do I reach my host from my guest?
我想我应该在 VirtualBox 本身中配置一些网络参数。我从Vagrant 文档中得到的只是我需要为我的虚拟机分配一个静态 IP 以使用专用网络。但是一旦创建,我如何从我的客人到达我的主人?
I didn't find anything like that in vagrant docs. So, there's probably a good reason for that. Does it make sense? I don't want to duplicate PostgreSQL installation. Just use my current existent one.
我在流浪文档中没有找到类似的东西。所以,这可能是一个很好的理由。是否有意义?我不想重复 PostgreSQL 安装。只需使用我目前存在的一个。
回答by Matt Cooper
You can reach your host from your guest by using the default gateway on your VM.
您可以使用 VM 上的默认网关从来宾访问主机。
See this answerfor an explanation.
请参阅此答案以获取解释。
By running netstat -rn
you can get the default gateway and then use that ip address in your config file of your application.
通过运行,netstat -rn
您可以获得默认网关,然后在应用程序的配置文件中使用该 IP 地址。
Running netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
should give you the correct ip address. (only tested this on my machine)
运行netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
应该会给你正确的IP地址。(仅在我的机器上测试过)
回答by Daniel Garmoshka
Easy way - simply use this "magic" IP from inside of vagrant without any additional configurations:
简单的方法 - 只需从 vagrant 内部使用这个“魔法”IP,无需任何额外配置:
10.0.2.2
10.0.2.2
Don't know if it's always static, though for me works and it's very convenient - I can use laptop at home, from office - having assigned different IPs to me by routers, but my VMs know the "trusty name" of their master
不知道它是否总是静态的,但对我来说是有效的,而且非常方便 - 我可以在家中或办公室使用笔记本电脑 - 路由器为我分配了不同的 IP,但我的 VM 知道它们的主人的“可信名称”