1

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

docker ps

5
  • localhost:3306 or the port u mapped with host port. another option docker inspect db and find the container IP and contact with that IP Commented Feb 23, 2019 at 7:58
  • i find the container IP by "docker inspect container-name" it returns "IPAddress": "172.17.0.2" ... and many other properties.. i try to connect work bench with this ip and port 3306 but it didn't connect and stack for a minute and says failed to connected Commented Feb 23, 2019 at 8:05
  • if i write localhost as host name it connect to my default instance the old one that i use with xampp Commented Feb 23, 2019 at 8:07
  • make sure your port mapping is correct docker run -d --name consenter name -p 3306:3306 , in this case other application out side docker can see mysql Commented Feb 23, 2019 at 8:19
  • now i make a separate dir for this container file as mentioned at docs.docker.com/samples/library/mysql docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag and use docker exec -it mysql bash and create a new user and grant all previllages now i connect this with workbench and it is now connected and showing a test database , but... it now its now creating new schema as permission denied for use ''@localhost Commented Feb 23, 2019 at 8:44

4 Answers 4

5

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.

enter image description here


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:

  1. Connect to your docker image via bash:

    docker exec -it docker-mysql bash

  2. Log into mysql as root

    mysql --user=root --password

  3. Enter the password for root (Default is 'root', but 'abc123' in this example)

  4. Finally Run:

    ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'abc123';

Sign up to request clarification or add additional context in comments.

12 Comments

when i use 3306:3306 docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql (764873ef2b1633e3e20ae2ab91a4c45fbba5913ba3821c5d7795db3cc72ea44c): Error starting userland proxy: Bind for 0.0.0.0:3306 failed: port is already allocated.
if u try other ports like 33512:3306 no docker process start docker ps show nothing
it try this by localhost and 3306 it connects successfully as you share screenshot but its showing my old connection databases which i used with xampp
Ok. That means something else is running on that port. Do you have any other MySQL servers running on that port, maybe an local instance? Anyhow, you can try to run the docker on a different port like this: docker run -p 3308:3306 --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
now i make a separate dir for this container file as mentioned at docs.docker.com/samples/library/mysql docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
|
1

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.

4 Comments

for what -dit flag is used for
docker run -dit -P --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 mysql:latest . doesn't start the docker process and nothing shows in docker ps
You try to use docker ps -a and docker logs docker-mysql
I think this article can help you: hackernoon.com/…
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.