How to connect MySQL client with server running in Docker - mysql

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

Related

Spring boot dockerized cannot reach MySQL

I dockerized a MySQL database by doing:
docker pull mysql
docker run --name=container_name -p 3307:3307 -e MYSQL_ROOT_PASSWORD=password -d mysql
Now the container <container_name> is the only one I have in Docker, and its network is set by default to "bridge".
Using: docker exec -it container_name mysql -u root -p (then typing the password) I created a brand new database.
I tried reaching that database from my SQL client, using the specified credentials and database url:
server: localhost or container_name, port: 3307, user: root, password: password.
The client doesn't reach the database and I get the following errors:
Connection refused: connect
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
Obviously I get the same error while running my spring boot application (not dockerized yet), but the problem is something in MySQL container since the SQL client isn't working as well.
I've been reading countless questions same as mine, but the solutions that they offer, never work for me.
What's weird is the fact that in the past projects it worked fine.
I'm running Docker on Windows 10 Pro with Hyper-V.
If you're trying to connect from outside of Docker, your docker run -p option is wrong: the second port number always needs to be the standard port number of its service. For MySQL, that second port number needs to be 3306, always. The two port numbers need to match.
docker run -d -p 3307:3306 ... mysql
# ^^^^
# must be the standard port number
If you're trying to connect from inside Docker, then
You must docker network create a network, and docker run --net all involved containers on that network
You must use the other container's name as a host name, not localhost
You must use the standard port number, again 3306; docker run -p options are ignored (and aren't required)
docker network create some-net
docker run -d --net some-net --name database \
-p ... -e ... -v ... \
mysql
docker run -d --net some-net --name application \
-e MYSQL_HOST=database \
-e MYSQL_PORT=3306 \
my/application
If you're running this under Docker Compose, it automatically creates the network for you, and you can use the Compose service name as the host name. You do not need to manually set up networks: or override the container_name:.
version: '3.8'
services:
database:
image: mysql
# ports: [...]
# environment: [...]
# volumes: [...]
application:
build: .
environment:
MYSQL_HOST: database
MYSQL_PORT: '3306'

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

Unable to connect Docker PhpMyAdmin to MySQL Server Mac OS, error #2002

Description
I'm attempting to run a container with PhpMyAdmin that connects to the MySQL Community Server I installed on my Mac OS.
As can be seen below, MySQL is running.
As can be seen below, I can connect via terminal.
Using the following command:
mysql --host=localhost --port=3306 --user=root --password="o_oGLZDI<1-t"
Problem
I am unable to connect to MySQL properly with PhpMyAdmin from docker. I've tried the following command lines:
docker run --name myadmin -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
docker run --name myadmin -d -e PMA_HOST=localhost -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
They generate these errors, when I attempt login:
127.0.0.1 version
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
localhost version
#2002 - No such file or directory — The server is not responding (or the local server's socket is not correctly configured).
Question
What is the correct command line required to run docker with the correct configurations to connect to my MySQL Server Mac OS ?
Your command:
docker run --name myadmin -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
This will point to localhost but to the localhost inside the phpmyadmin container and not to the localhost of your machine (where mysql is running).
You can run your container on your host network which will disable container networking. This means you will be able to reach your mysql container using localhost:
docker run --name myadmin --network=host -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 phpmyadmin/phpmyadmin
You can access your phpmyadmin on port 80 (not 8080) because the container network is not used when you specify --network=host. (It could be you need to adapt your firewall to allow docker0)
Another option (a better one), especially for MacOS (since Docker version 17.06) is to use docker.for.mac.localhost as PMA_HOST. This should resolve to your mac internal address (but I was not able to test it for now).
docker run --name myadmin -d -e PMA_HOST=docker.for.mac.localhost -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin

Connect to Docker container running mysql on Windows 10

I am using Docker for Windows, on Windows 10 Enterprise. I am trying to connect to a container that is running mysql. I followed the instruction here https://hub.docker.com/_/mysql/ and I used this command to start the container docker run --name memories -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6
if I type docker ps I get
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
300248b56399 mysql:5.6 "docker-entrypoint.sh" About an hour ago Up About an hour 3306/tcp memories
However I cannot figure out how to connect to this container from the host. I have tried localhost and 127.0.0.1. Each time I get an error like this
/* Connecting to 127.0.0.1 via MySQL (TCP/IP), username root, using password: Yes ... */
/* Can't connect to MySQL server on '127.0.0.1' (10061) */
Any suggestions?
I guess it was more simple than I thought. I had to publish port 3306
docker run -p 3306:3306 --name memories -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6
Docker MySQL and phpMyAdmin containers on Windows 10
Persist MySQL volume in “C:\mysql” folder.
Set username and password for MySQL database. Create database.
docker run --name mysql-server_v2 -d --restart=unless-stopped -v C:\mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=rootp -e MYSQL_USER=user1 -e MYSQL_PASSWORD=user1p -e MYSQL_DATABASE=projectX mysql:5.7.29
Connect phpMyAdmin and accessing it with localhost:8080
docker run --name phpmyadmin_v2 -d --link mysql-server_v2:db -p 8080:80 phpmyadmin/phpmyadmin
src: https://devwl.pl/docker-setup-mysql-with-phpmyadmin-on-windows10-11/