Connect to Docker container running mysql on Windows 10 - mysql

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/

Related

Can't connect to Docker container running MySQL Server

The Docker run command is:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=flurpgraSS -d mysql:5.7
Docker Desktop shows the container:
I can access the server from the CLI launched by Docker Desktop and the root account appears to be OK:
I can't access the server using MySQL Workbench
We might need to expose port from docker by -p parameter, otherwise, we can't access it outside.
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Danger42! -d mysql:5.7
There is some description from Container networking
-p 3306:3306:Map TCP port 3306 in the container to port 3306 on the Docker host.

How to connect with MySQL Docker image from host?

I am Running a MySQL Docker image and creating database and table. The container is lauched with the command:
docker run --network host -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server
Spark is running on my host machine, So i want to write data from Spark into the database running in the container.
But the connection does not succeed and I am getting an Exception:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
You can try this 2 solutions:
Check if your connection URL is not wrong in your java file.
Check if your container accept connections that is not localhost of the container:
Go to your cmd and enter in the container:
docker exec -it containerID /bin/bash
install vim inside container apt install vim
vim /etc/mysql/mysql.conf.d/mysqld.cnf
comment bind-adress or make it listen to 0.0.0.0
exit from container
restart
Hope this help.
As Schwarz54 mentioned in his answer, it could be host issue.
I usually pass it at the time of initialising the container using the MYSQL_HOST parameter.
docker run --name mysql_5.7 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
I think you don't have to map the ports if you are running it on the host network.
docker run --name mysql_5.7 -d --network host -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
You can mount a local directory to the local container if you want the data to be persisted across container restarts using -v flag:
mkdir -p /Users/projects/data/mysql5.7
And mount this directory in the container at /var/lib/mysql
docker run --name mysql_5.7 -d --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost -v /Users/projects/data/mysql5.7:/var/lib/mysql mysql:5.7

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.

Basic MySQL Docker Container

I just created a mysql docker container using
docker run -p 3310:3310 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
I am able to connect to my database using docker exec -ti ipca-mysql bash and mysql -u root -p
But when I tried to connect to my database at localhost:3310 using mysql workbench I get:
lost connection to mysql server at 'reading initial communication
packet' system error 0
Any idea what is this issue?
MySQL container start using this command it's worked.
docker run -p 3310:3306 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
You could use this docker command:
docker run --name mysql-tacs -e MYSQL_ROOT_PASSWORD=root -d -p 3310:3306 mysql
In the link below there is a video which shows step by step the creation of mysql server with docker until the connection in its server using MySQL Workbench.
https://www.youtube.com/watch?v=ZZwvhMvUJiw

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