Connect docker MySql to localhost mysql - mysql

I have used the following command for port mapping
sudo docker run -d -p 3306:33060 --name App1 APP/framework:app
where 3306 port belongs to localhost machine MySQL and 33060 is the port into a docker container, so communication is happening between 3306:33060 in docker. I'm getting issue as below:
sudo docker run -d -p 3306:33060 --name App1 APP/framework:app fc1ffe98b2f2e6299a3070be8296d8b530ef4bdb3bd4cfd79d28ffc535a361c1
docker: Error response from daemon: driver failed programming external connectivity on endpoint App1 (30ec933973acf63a48ef9a20b0027af18bd23e1f36cf852e2e3e3758eaa1f843): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.
I don't want mysql in docker container to create image.Just want to open random port and map 3306 and host-ip to communication with port in docker i.e 33060
Can anyone please suggest any way to resolve this that would be appreciated? Thank you.

Some examples from the docker documentation, before the colon is the container port, and after the colon is the container host port.
-p 8080:80
Map TCP port 80 in the container to port 8080 on the Docker host.
-p 192.168.1.100:8080:80
Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.
-p 8080:80/udp
Map UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udp
Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.
More information at, https://docs.docker.com/config/containers/container-networking/

If you want to access port 3306 in the host from your docker container, you can use
sudo docker run -d --net host --name App1 APP/framework:app
and you container will share the host's network meaning that you could do localhost:3306 from inside your container and "localhost" on the container would be exactly the same thing as "localhost" on the host.

Related

wsl2 docker mysql not listening on port 3306

i'm trying to setup a local enviroment with laravel-sail on wsl2 (debian).
I followed the laravel sail guide and everything works well except I can't connect to mysql in the docker container from the host.
docker ps
sudo ss -tulpn
Why mysql doesn't listen to 3306 port?

Connect to a mysql server running in a docker container from another container?

I have two containers; a node.js server, and a mysql server ...
Both are running fine, I'm trying to connect to the database running in the MySQL container from my node app.
I've created a bridge network:
$ docker network create -d bridge mynetwork
and run both of my containers in this network with:
$ docker run -p 32031:3306 --network mynetwork --name mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:5.7
$ docker run -p 3000:3000 --network mynetwork node-server node index.js
I'm trying to connect with the IP of the mysql container that I found with docker inspect mysql, which is 172.18.0.2 and port 32031.
Where did it go wrong, it's not connecting?
If you connect by IP which you get from docker inspect command. That mean your connection now is 172.18.0.2 and port still 3306
Another way to connect from nodejs (nodejs and mysql in same network). Connect information is:
hostname: mysql // container name
port: 3306
Docker containers connected via a network can communicate internally. Your mysql container is running on port 3306 internally and 32031 externally.
Try connecting via port 3306. It should work.
Ohh, my good luck.
It's working now. I rebuilt the docker image and connected to the database with the IP that I found with inspect mysql container, and with port 32031.
Run docker container same as: $ docker run -p 3000:3000 --network mynetwork node-server node index.js
var pool = mysql.createPool({
connectionLimit : 3,
host : '172.18.0.1',
user : 'root',
port : 32031,
password : 'abc123',
database : 'mydatabase',
charset : 'utf8mb4',
timezone : 'PKT',
timeout : 4000
});

How to connect mysql docker container to node.js server or workbench

Iam beginner to docker and iam working on mysql and node.js I run mysql docker container as
docker run --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
and result of docker ps is showing mysql container is running
and docker logs says
MySQL init process done. Ready for start up.
how to connect with this container in workbench or in my application
Try this:
docker run -p 3306:3306 --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
This will bind the port 3306 on your local machine to the docker image. You should be able to connect to the database with localhost & port 3306 with username root and password abc123.
I just tested it and it works like a charm.
If you are struggling with the error:
failed to connect to localhost at 33016" details = Authentication
plugin 'caching_sha2_password' cannot be loaded:
dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image
not found
Update your MySQL-workbench.
If that doesn't work you will need to add a native password to the root user. Here is how:
Connect to your docker image via bash:
docker exec -it docker-mysql bash
Log into mysql as root
mysql --user=root --password
Enter the password for root (Default is 'root', but 'abc123' in this example)
Finally Run:
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'abc123';
You need to expose the port
Use -p, or -P
-p is bound to a custom port, -P will randomly assign a port to you.
:latest does not need to add, docker will help you add.
The final command should look like this:
docker run -dit -P --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 mysql:latest
Then use xx to see the exposed ports:
docker port docker-mysql
Check which port of the machine is mapped to port 3306 of the container, My result is:
33060/tcp -> 0.0.0.0:32818
3306/tcp -> 0.0.0.0:32819
Now you can connect to this port via software or code.
For development best way is to connect the container to "host" network
docker run --name docker-mysql --network host -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
When you will move to staging/prod environment consider to use some orchestration solution.
Run docker inspect container_id its will show container port but will not show host port bind. By Default running without -p flag it will assign port to container but will not expose host port , so to achieve this you shout try -p flag, -p 3306:3306 in docker run command.

How to open the mysql container docker in the mysql workbench

I have a mysql container like this :
I want to open the mysql using mysql workbench, does anyone know how ??
I am still confused how to fill this data ..
I hope there is a solution to my problem
tl;dr
You have to publish MySQL's port 3306 to the "outside" using the -p switch.
Container ports are not accessible by default to the "outside" and are only open to other containers in the same network.
Run your MySQL image with -p 15000:3306 (map local port 15000 to container's port 3306) then connect in the Workbench to localhost at port 15000. You can choose any port you want, it can be 3306 too: -p 3306:3306.
Example docker run command:
docker run -it --rm -v mysql:/var/lib/mysql -p 3306:3306 mysql
In case of docker-compose:
services:
# …
mysql:
# …
ports:
- 3306:3306

How to connect MySQL client with server running in Docker

I am using Docker first time. I am trying to connect docker based MySQL with my Navicat client. How do I get docker IP and then use it for connection? Here are a few details:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b984b6659d20 mysql:5.7 "docker-entrypoint.sh" 8 minutes ago Up 7 minutes 3306/tcp myre_mysql_run_1
d34d8974912c myre_php-apache-engine "/usr/local/bin/entry" 20 hours ago Up 7 minutes 80/tcp myre_php_apache_engine_dev
981c2a7fa83b mysql:5.7 "docker-entrypoint.sh" 20 hours ago Up 47 seconds 3306/tcp myre_mysql_dev
472531e09d08 jwilder/nginx-proxy "/app/docker-entrypoi" 20 hours ago Up 7 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx_proxy
If you need to access the mysql container from the outside. Than you need to map the container port on the port of your server (using -p).
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_USER=test -e MYSQL_PASSWORD=test -d mysql:latest
Now you can connect to your container using the IP of your server + the port. In my case I'm in a VLAN. The IP of my server is 192.168.140.30. So e.g. when I ssh to my server I use that IP. Now I've mapped the port of my container on the port of my server (with -p 3306:3306 which means: map the port 3306 of my container to port 3306 of my server. (so I can use the server IP to connect).
If you need use client to connect to MySQL in Docker container you need do port mapping with the host machine.
I see you MySQL container not use mapping the container port to host port so you can not use MySQL client to connect the MySQL server.
You can use -p parameter specify the port mapping. for example if you need use port 3306 to connect to MySQL server, use following command to create the MySQL container. PS: you need change some setting for following command.
docker run --name container-name -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=database-name -e MYSQL_USER=user -e MYSQL_PASSWORD=password mysql:5.7