Connecting Applications from your Desktop to a database on VirtualBox

If you have your Oracle VM VirtualBox setup and running there is a possibility that you may need to setup the network for the Virtual Machine to make it accessible from the host machine (Desktop or Laptop). This may be needed when you want to connect your local applications to the database running on the VM VirtualBox.

When the VM is initially created on the VirtualBox, the network is not set up. But it’s not difficult to make it work though. We will look at two ways you can do that.

Using Bridged Network Adapter

By default, a VM uses the NAT adapter. NAT does the job of a network adapter, one in particular is enabling the VM allowing you to use the internet via host. However it does not allow you to connect to the VM from your host machine. The easiest way to achieve this is to configure another Network Adapter.

To achieve this, select your VM and go to Settings menu –> Network. Click on the Adapter 2 tab, enable it and make sure that Bridged Adapter is selected. You don’t need to change anything else.

Turn on your VM. Open a terminal and get the IP address of second adapter. Normally it will be named as eth1.

In the example above, the IP address we are looking for is the device eth1 which is 192.168.2.69. You can use this to make connections to your Virutal Machine. I tested it using Putty as a sample application because this is one application that serves me the most.

Using Port Forwarding

The method above is simple but it has one limitation which is that every time when you restart your VM, a different IP address may be assigned. That’s particularly a hassle if you have saved connections. You may have to change them every now and then. To avoid this, the other option to achieve the same functionality is to setup Port Forwarding. In Port Forwarding, you enable your VM to take the requests which were originally meant for your local host. This setup is also pretty simple and straight forward.

Select the VM and open the Setting menu –> Network. On your NAT Adapter, expand the Advanced Settings and click on Port Forwarding. Enter the details in the dialogue box as shown below.

Here you are setting your VM to start accepting connections which are originally directed to 127.0.01:22 (localhost:22). Click OK and turn on your VM.

Now you don’t need any IP address to make connections. Just use localhost:22 as host name and port – all connections will be directed towards the VM. As shown below I am doing the same to connect to VM using Putty.