What are we trying to do ?

  • Run MySQL as a docker container in Local
  • Connecting to it from command line.
  • Connecting to it from DBeaver.

What should we have already ?

How can I start ?

RUNNING MYSQL IMAGE IN LOCAL

Run the following command (only after reading the explanation below and substituting it with your own values)

$ docker run -p 3306:3306 --name adt-mysql-db -e MYSQL_ROOT_PASSWORD=adevtalks -d mysql

Lets breakdown this command into parts

  • docker :- The command to invoke docker
  • run :- To create a docker container uses the mysql at the end to determine which docker image it should run. if mysql image is not available in local the latest will be downloaded from docker hub.
  • -p :- Tells to map the port 3306 of localhost to 3306 port of the container
  • –name :- Sets the name of the container
  • -e :- Sets the environment variable. In this case sets the MYSQL_ROOT_PASSWORD
  • -d :- Tells to run docker as detcached (not attached to the current terminal but in background)
Running the docker command
CHECK IF IT IS RUNNING IN LOCAL

You can see a list of running containers using the following command

$ docker ps
List of running docker containers
CONNECT TO MYSQL IN CONTAINER VIA COMMAND LINE

1. Open the command line inside the container using the following command.
docker exec -it <containername> /bin/sh

docker exec -it adt-mysql-db /bin/sh

2. Type the following commands to login to the instance

mysql -u root -p
Logging in using command line

3. Enter the password to login

4. If every thing went well we will see that the cursor is mysql>

5. Type some commands to test it.

CONNECT TO MYSQL IN CONTAINER WITH DBEAVER

1. Open Dbeaver

2. Add a new Connection

Adding a new DB Connection

3. Select MySQL from the list

Selecting MySql 8+

4. Click Next and fill in the password, you can test if everything works fine using the Test Connection option.

Fill in the password and Test connection

5. If everything went well the test connection will succeed and you can click finish. On doing that you will be able to see the connection in the Database Navigator pane.

New connection ready to use in the Database Navigator pane

6. Sometimes you get an error public key retrieval is not allowed, in this case go Driver properties tab and in that tab you will be able to see a allowPublicKeyRetrieval property, set that to true. ( Note that AllowPublicKeyRetrieval=True could allow a malicious proxy to perform a MITM attack to get the plaintext password, so it is False by default and must be explicitly enabled.)

START, RESTART, STOP CONTAINERS

Start a docker container using docker start <containername>

docker start adt-mysql-db

Stop a docker container using docker stop <containername>

docker stop adt-mysql-db

Restart a docker container using docker restart <containername>

docker restart adt-mysql-db

Remove a stopped docker container using docker container rm <containername>

docker container rm adt-mysql-db

Feel free to write your thoughts as comments or email

writer@adevtalks.com
Ajay Bhasy

By Ajay Bhasy

7 Years of experience in developing applications with .Net (C#). 2 Years of Experience with Angular. Strong advocate of Continuous Integration and Delivery practices with experience in using Bamboo,Octopus Deploy and docker. Engineering professional with a Bachelor’s Degree focused in Information Technology from Dr. NNCE. Recently started learning technologies like MongoDB and Node Js

Leave a Reply

Your email address will not be published. Required fields are marked *