How to connect remotely to sql database in docker container? - mysql

newbie here.
I have a docker container with a mysql server on it. I want to be able to work on it on different pc.
Following a guide I did these steps:
1)create a db and an account with all privileges
2)got the machine ip throudh powershell with:
"docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' YOUR_CONTAINER_NAME"
3)tried to connect to my server in workbench
It didn't work but I don't really know what I'm doing.
Anybody can give me a step by step guide to follow? Thank you

Add port mapping using docker run -p and modify inbound firewall rules in the PC where container is running to allow traffic on the port from another machine in same network

Related

Connecting to Remote MySQL DB in docker container using PDO

I am a bit of a newbie and new to the php coding world and have a small task I am trying to find an answer to, but I cannot seem to find the relevant question asked anywhere before.
Basically I have a server where I create reports from mySQL DBs located on other remote machines.
So far these other servers have had a basic mysql server running on them and I can easily connect to them with PDO like this:
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
But I now have a server running mySQL inside of a docker container on this remote server.
So my question is, is it possible to connect to this mySQL db as well using PDO?
I am able to connect to the DB remotely with mySQL workbench using Standard TCP/IP over SSH (as I also do with the other servers), but the one thing I have to specify differently here compared to the other servers not using docker is the 'MySQL server host relative to the SSH server'. Usually just having 127.0.0.1 here is fine, but with mySQL inside a docker container I first have to find this relative address with
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' database-name
you then get something like 172.21.0.5 and add this in that field
workbench connection
How are you able to represent this small change in the PDO statement? Is it still possible to connect in this way?
actually, no. You can't connect inside code as mysql workbench over ssh. But you can use ssh jumb for this connection as below link.
https://www.tecmint.com/access-linux-server-using-a-jump-host/
You should configure jump command to a mysql servers with port. this jump command provides your connection via bastion you mentioned instance for ssh to the mysql server.
for example: ssh -N -i pemfile.pem(if you have to connect to jump server) -L fakemysqlserverip:3306:mysqlserverrealip:3306 ec2-user#jumpserverip
fakemysqlserverip may be localhost,127.0.0.1 or different
jumpserverip is your server which is for using over ssh
Regards

MySql Docker container refuses to connect to Microsoft Azure (Mac)

First of all - hello. Secondly, I just downloaded Docker and got it to run MySql on my Mac. It has never connected before and I am trying to learn how to make databases.
Everything was running smoothly - I opened Docker and ran my container (it is running), but when I tried to connect with Microsoft Azure, it persistently refused to do so.
Since my password was strong enough to fill the criteria, I checked the port and it is indeed 1433.
So I started to use every variation I could think of in username, just to see what happens.
Nothing. No connection. I read it could be the firewall, but all the instructions I found were Windows and other applications.
Can anyone here point me in the right direction? It seems such a pathetic hill to die on.
docker run --name <your-mysql-name> -e MYSQL_ROOT_PASSWORD=<your-sql-password> -p <host-port>:<container-port> -d mysql
You'll need to expose / publish the port (-p command), if not MySQL cannot be accessed by external sources.
For example:
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=somepassword -p 3306:3306 -d mysql
The user name is "sa". I found it on a blog and tried it out. It worked.

How to access mysql databases inside docker from a SQL GUI?

I am using vessel to run my Laravel project.
As I understand it basically simply creates a put-together docker image of my laravel project with mysql and everything.
Now since its running in docker, is it possible for me to access the mySQL databases with datagrip or tableplus on my host machine?
You just need to expose a port via docker. If you dont,you will have port 3306 which can be accessed via the local containers but if you bind port 3307:3306 for instance you can connect to mysql locally on 3307
you will need to connect to MySQL server using the docker containers IP that can be found using docker inspect.
This can also help you:
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55

Node Red MySQL can not connect to docker MySQL server working locally

I have started a container, and he seems that working:
I can login to the mysql db via
docker exec -it mysqlsys bash
The credentials work fine there.
So, the next step, I am going to the node-red, and use the "node-red-contrib-mysql". But when, I setup the data to connect to the DB, I can see that "Connection lost", and I can not work at all.
Can someone help with it?
Thanks.
P.S.
Host IP address 127.0.0.1 is not correct. You need to use your actual machine IP address.
Or if Docker is running on the same machine then use localhost.
It should work !!! let me know.
You can either use container's IP or create a docker-compose.yml file to create services. For the first option, to find the IP write on the terminal
docker inspect mysqlsys
and find the IPAddress key.
If you select the second option you can use as host the service name.

Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?

I have a locally running MySQL server.. it's NOT within a container. My app is going to be hitting RDS so no sense in going that route. My app was able to hit RDS no problem, as a test. But obviously I want to hit something local for local development.
From my terminal I can do mysql --user=root --password=password mydb successfully.
And as I'm not getting a timeout error, from my container I can ping 127.0.0.1:3306 with no issue.
I also used console to see I am definitely passing the right info, after having updated the values from RDS to locally running MySQL.
Docker container has its own network IPs, including its own localhost. So you basically need to be sure of two things:
That your host MySQL is listening in all of its interfaces (bind-addres = 0.0.0.0 in my.cnf). Check with netstat -na|grep 3306.
Figure out the host ip that your container can reach. So check the IP of the container: docker inspect container-id, find the IP, and replace the last part with .1, that should be the IP of your host in the containers own network. I.e. 172.17.0.1 (it can be considered as fixed IP, for dev environment it's ok)
So most likely that this is what you need: 172.17.0.1:3306