How to connect to Mysql VirtualBox Vagrant from another VirtualBox Vagrant? - vagrant

I have two VirtualBox Vagrant machines running on my Mac:
Ubuntu 16.04.1, private network: 192.168.122.13, running as a webserver (PHP, Apache, etc.).
Ubuntu 16.04.1, private network: 192.168.122.14, running MySQL.
How do I connect to MySQL from the webserver?
I have enabled port forwarding on the MySQL box (3306 guest => 5629 host) and commented out the bind-address line in /etc/mysql/mysql.conf.d/mysqld.cnf. If I SSH onto the webserver and try to connect using mysql -host 192.168.122.14 -P 5629 -u xxx -p yyy the connection times out (ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.122.14' (110)).

MySQL runs on the vagrant box with IP 192.168.122.14, and is listening on port 3306. When you forward ports, it means that the port 3306 from the virtual machine, is forwarded to your host OS on the port 5629.
From your web server (192.168.122.13) you could connect to mysql by
mysql -h 192.168.122.14 -P 3306
or
mysql -h 192.168.122.1 -P 5629
Here 192.168.122.1, refers to the IP of your host OS. The IP 192.168.122.1 is created by vagrant and assigned to your host OS

Related

Windows Docker contnainer of Postgres unreachable on 192.168.56.1

I am running a Postgres Docker container by command docker run --name postgres -p 0.0.0.0:5432:5432 -e POSTGRES_PASSWORD=password -d postgres.
In my understanding, 0.0.0.0:5432 means all the IP addresses of the Windows host at port 5432. And, I can access the port by the command telnet 192.168.56.1 5432 with Windows CMD. With VirtualBox installed, 192.168.56.1 means the physical host.
However, I have an RHEL Linux virtual machine running on VirtualBox. On the VM, the command telnet 192.168.56.1 5432 does not work. But if I try another IP address of the Windows host, it works.
I need to use the virtual machine to test and access the Postgres database deployed on the physical host. I am wondering whether I missed anything here.
Any suggestions or hints will be highly appreciated.

Connecting PostgreSQL installed in docker inside Hyper-V Ubuntu from Windows 10 PgAdmin

I need help in connecting PostgreSQL which is installed in Docker inside HyperV ubuntu 18.4 from Windows 10 PgAdmin. So far I tried the following
Step 1: Install Postgres in Docker (Ubuntu running on Hyper-V)
sudo docker run -p 5432:5432 --name pg_test -e POSTGRES_PASSWORD=admin -d postgres
Step 2: Create a database
docker exec -it pg_test bash
psql -U postgres
create database mytestdb
Step 3: Get the ip address
sudo docker inspect pg_test | grep IPAddress
//returned with 172.17.0.2
Step 4: pg_hba.conf
host all all 0.0.0.0/0 md5
Step 5: When I try to connect from Windows PgAdmin 4, I get this below error -
Note: I have also tried using UBUNTU VM IP address, but no luck
Your's is a case where you are trying to connect to postgres from another subnet, i.e windows subnet to hyper visor subnet if you are not using bridged protocol.
So case 1:
If this is on NAT\HOST and not on bridge then you need to make sure you are able to ping the ubuntu server from windows server.
next is make sure that port is open from ubuntu's end. How do you check that, do a telnet on the port number from windows cmd prompt.
telnet 192.168.0.10 5432
if you are bridged and you can ping ping the server as well, checked that port is opened which is telnet works. You need to make sure that in the postgres.conf file
"listen address" is to "*". which is all.
Again from OS level in ubuntu run the command systemctl stop firewalld to stop firewall and then try to connect. IF this works then you need to open the port in the firewall using this command:
firewall-cmd --permanent --add-port 5432/tcp
I can see from you docker image that 5432 is already opened. This is more of port mapping and firewalld stuff.
You may want to check that pg_hba.conf is not restricted to local. It should not be the case for docker image but you never know.
See: https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
Also, there is a typo: POSTGRES_PASSWOR=admin is missing D, it should be POSTGRES_PASSWORD=admin.
You don't need container IP. Since you have mapped container port to host machine (Ubuntu) anyone outsider just needs the Ubuntu machine IP, and on Ubuntu itself you can use localhost.

Having trouble in accessing my virtual machine from local with SSH

I've ubuntu 16.04 on both my local and virtual machine, I want to access my virtual machine from my local machine, I've already changed the network adapter to bridge connection (both ips are in 192.168,10.x). But when i run the ssh virtual_mac_ip from my local terminal i get the error ssh: connect to host 192.168.10.7 port 22: Connection refused.
ps: I want to configure single node hadoop cluster
The issue has been resolved I changed my network adapter to NAT again, and use port forwarding on port 2222. Now when I run "ssh -p 2222 username#127.0.0.1", I am able to connect to my guest OS
Side note: Please check if OpenSSH is installed on your guess machine

Vagrant port fowarding on macOS?

I'm running postgresql on a Vagrant centos 7.2 box on macOS. I have confirmed postgres is up and running on the Vagrant box on port 5432 by connecting to it using psql on the box itself. I am attempting to forward port 5432 on the Vagrant box to port 10001 on my host machine as follows:
config.vm.define "acc_db" do | acc_db |
acc_db.vm.box = "bento/centos-7.2"
acc_db.vm.hostname = "acc.db"
acc_db.vm.network :forwarded_port, guest: 5432, host: 10001
acc_db.vm.provision "shell",
inline: "yum upgrade -y -q --nogpgcheck"
acc_db.vm.provision "shell",
path: "install_postgres.sh"
I have altered my pg_hba.conf to bind to all ip addresses and allow password authentication, as below:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
I have turned off the firewall but I am still unable to connect to postgres on port 10001 on the host machine:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.10001"?
How do I fix this so that port forwarding works?
I have seen https://gielberkers.com/fixing-vagrant-port-forwarding-osx-yosemite/ but haven't tried it as it involves touching files I'm unfamiliar with. Is it the correct approach? It looks like you have to explicitly allow every port you want to forward via Vagrant.
...connections on Unix domain socket "/tmp/.s.PGSQL.10001" means that you are not attempting a TCP/IP connection, so port forwarding cannot happen.
By default, psql use Unix domain sockets on unix-like OSes.
Use the -h option of psql to specify a host, presumably 127.0.0.1 if using IPv4.
Additionally, the PostgreSQL server must listen to the network interface to which the connection will be routed. By default, it listens only to localhost for security reasons.
Setting listen_addresses='*' in postgresql.conf makes it listen on all existing interfaces.

cannot connect to host 127.0.0.1 Virtualbox

So I'm using a Macbook Pro with virtualbox and trying to SSH to CentOS virtual machine.
I've started the SSH service on centOS and I've gone into the network settings of virtualbox and made sure that the adaptor was set to NAT and that port forwarding was set to port 22 for the guest and host.
When I run the following on my mac: ssh 127.0.0.1
It says: ssh: connect to host 127.0.0.1 port 22: Connection refused
Any ideas why?
Fixed the issue by enabling ssh on mac. I didn't realize it was off as default! If you go into the sharing options in system preferences you can tick a box which enables ssh. To connect you must use: $ ssh username#192.168.1.111 (or whatever the ip is of the machine).

Resources