How to access remotely a dockerised database container in Ubuntu OS? - mysql

Current Situation :
In my company we are using a windows server, in which we have installed Docker Quickstart Terminal. We have made a mysql-container and made a DB inside. We access the DB with python apps in remote pcs by using the server's IP in the code (host argument):
connx = mysql.connector.connect(user='root', password='somepass', host='192.XXX.XX.XX', port=3306, database='db_name', auth_plugin='mysql_native_password')
but we used port-forward in the server-side to access the dockerised DB, thus the packets are forwarded to 192.168.99.100 (default docker IP)
Future Situation:
The company has decided to change the server and use Ubuntu instead (v18.04 i think). Unfortunately i have very little experience with linux and could not find a simple answer as the following online :
'Which IP should we use on the host argument above ?'
Docker installation on linux does not seem to install a VM, so will the new server's IP be enough to access the dockerised IP remotely ?
PS : we will probably do a 'docker run -p 3306:3306 mysql:latest' command on the server to expose the ports

You should be able to access the database the say way you were able to access in Windows Server, i.e., using the IP of the Ubuntu host machine and port forwarding the containerized database port with the host port.

Related

Can't reach MySQL hosted DB from Docker .NET Core

I’ve got a DigitalOcean Droplet that run Ubuntu and I’ve installed MySQL to it. I can reach it from my computer with SSH connection from MySQL workbench, so the remote access is Ok.
The hostname is 127.0.0.1
The bind address in my mysqld.cnf: 127.0.0.1
I’ve got a .NET Core API and I would like to use Docker to run it. I made the container push to docker hub and pulled it to the droplet. When I try to run I get this error:
An error occurred using the connection to database “ on server
‘127.0.0.1’.
My connection string:
"Server=127.0.0.1;Port=3306;Database=db;Uid=user;Pwd=pass”
I tried to server: localhost
IP address for docker0: 172.17.0.1 so I tried this to connectionString as well.
I don’t understand why can I connect to DB from MySQL workbench and cannot from .NET Core Web API.
The .NET Core version is 3.1.
Your .NET Core is inside container. Thus if you try to connect localhost or 127.0.0.1, it will try to connect the container itself, not the host. Since your MySQL is on your host, not inside the container, you can access it with other IP.
For example you have eth0 or enps0 or something like that, then that interface have IP 192.168.1.2, then you can use this IP as connection string.
Or, the alternate way, is using --network host when creating container. In this way, if you use 127.0.0.1, it will try to connect the host.

Can't connect to mysql server (LAMP) in google cloud

I installed LAMP on a VM instance on Google cloud but when i try to access mysql database remotely from my linux terminal using
$ mysql -u username -h ipaddress -p
after entering password nothing happens (no error). I tried accessing it locally via ssh it works.
Is there any other setting on cloud platform. I need to change before accessing database from a remote pc. Followed this tutorial but it just stops after entering password.
Tried commenting out bind address but didn't work. Need help.
Just to be clear, are you using the mysql command on your remote machine or through SSH?
Normally, you should never allow external access to remote MySQL for security reasons, especially if the only app accessing the data is local. (however, it's unclear if that is the reason why you can't connect)
This is how you should connect:
Connect to your remote VM using SSH (ex: ssh user#ipaddress)
THEN, connect to your MySQL instance.
When connecting using a DB editor, some editors allow MySQL connections through SSH. Sequel Pro for Mac does this very well.

Connecting to localhost mysql server from inside docker container on macOS

How can I connect to localhost mysql server from a docker container on macOS ?
One way to do it using --add-host but that requires me to pass some name like "myhost".
Is there any way in macOS so that references to localhost from inside docker container actually refer to docker host ?
On MacOS docker provide special DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.
use host.docker.internal
to connect to the host running the Docker.
this works From docker version 18.03 onwards only and This is for development purposes and will not work in a production environment outside of Docker Desktop for Mac.
( refer the page https://docs.docker.com/docker-for-mac/networking/ for more info )
sample connection string for oracle, jdbc:oracle:thin:#host.docker.internal:1521/orcl
From inside of a Docker container, how do I connect to the localhost of the machine?
You should be able to connect to MySql running on host machine using host machine actual IP address. In MacOS, try to find your ip by command ifconfig. Mostly using IP assigned to en0 i.e. your ethernet interface should work. Just call that IP from within your container.
*localhost or 127.0.0.1 or 0.0.0.0 doesnt call host machine as they are local to container itself.

How do I connect to my local MacBook MySQL form Vagrant VM Box?

I've set up Vagrant VM, it's working well for me.
Local MySQL is working good too when connection via localhost.
But I can't find a way how to connect via vagrant to macbook local MySQL.
The only visible IP to my vagrant is my local IP address of macbook: 10.0.0.4
It pings it, but this IP is not answering any requests to port 3306, though my.cnf config is set to:
bind-address = 0.0.0.0:3306
and Mac firewall switched off for a test.
I'm not sure what info to provide, let me know in comments and I will expand my question.
Thanks a lot
In terms of being able to access the instance of mysql running on your VM's host machine, you can use the ip that vagrant uses for its natted interface.
mysql -uroot -h 10.0.2.2
If you've configured a bridged interface for your vagrant machine then you can use the ip that your host has on the interface that you have bridged to with vagrant.
I've tested this with the brew installed version of mysql and it works when the mysql server on the host is bound on 0.0.0.0 or 127.0.0.1, i.e. mysql.server start --bind-address=127.0.0.1
You can connect from your vagrant box to local mysql using
mysql --host=10.0.2.2 -uroot -p

Can I SSH to MySQL with the GUI tools?

I have a ubuntu server which I've locked down to only HTTP, HTTPS and SSH (port 30000). On this box I have a MySQL server which 99.9% of the time is only used locally. Every now and then I want to connect to the MySQL instance with the GUI tools, but without touching the firewall.
Therefore, it is possible to set things up so that I can connect to the MySQL box with the SSH connection? Is there any sort of port forwarding magic I can do?
ssh -L localport:127.0.0.1:remoteport user#remotehost
where localport is an unbound port on your local system, remoteport is the port on which MySQL listens, 127.0.0.1 is the loopback address on your remote system, user is your user name and remotehost is the address of the remote system.
Then point your MySQL client to 127.0.0.1:localport. The connection will be forwarded over the SSH tunnel to the remote port.
If you are using Windows or Unix on your local machine, PuTTY is an excellent tool for this. Here's a tutorial on port forwarding:
http://www.cs.uu.nl/technical/services/ssh/putty/puttyfw.html
Set up a tunnel using the SSH client to map a local port, through the tunnel, to a port on the remote machine, then connect to the local port using the client.
man ssh will have the gory details, but you're probably better off searching the web for something a wee bit easier to read.
I'd suggest using HeidiSQL if you are on Windows. It handles the SSH tunnel by itself (you just need to download the plink.exe utility).
In my humble opinion, it is the best MySQL tool out there (and it's free and open source).
There two ways to use the GUI mysql tools on a remote MYSQL machine.
I'm assuming the remote machine is running linux.
Option 1.
SSH port forwarding.
ssh -L 3306:proxy:3306 username#remotehost
Basically you forward port 3306 on your local machine to 3306 on your remote machine, then you tell your graphical MYSQl tool to connect to localhost:3306 and you assume that your remote machine is listening on the standard port of 3306
http://magazine.redhat.com/2007/11/06/ssh-port-forwarding/
Also ssh -D 3306 username#remotehost should be similar
Option 2.
If you are running xserver on your remote machine and have the GUI tools installed on your remote machine you can tunnel your GUI over the SSH connection.
ssh -X username#remotehost and then start your GUI tools over on remote machine from the command line.
SSH/HTTP tunneling in SQLyog is good. There is public/private key authentication which adds an additional layer of security.
I suggest looking to phpMyAdmin a web based tool to administrate MySQL, and is very good (requires PHP). It can be installed through the normal Ubuntu methods (e.g. Synaptic)
Or do X tunnelling with SSH as mentioned above.